PatchworkOS
Loading...
Searching...
No Matches
image.c
Go to the documentation of this file.
1#include "internal.h"
2
3#include <stdlib.h>
4
5#define FBMP_MAGIC 0x706D6266
6
8{
9 image_t* image = malloc(sizeof(image_t));
10 if (image == NULL)
11 {
12 return NULL;
13 }
15 image->draw.disp = disp;
16 image->draw.stride = width;
17 image->draw.buffer = malloc(width * height * sizeof(pixel_t));
18 image->draw.contentRect = RECT_INIT_DIM(0, 0, width, height);
20
21 mtx_lock(&disp->mutex);
22 list_push(&disp->images, &image->entry);
23 mtx_unlock(&disp->mutex);
24 return image;
25}
26
27image_t* image_new(display_t* disp, const char* path)
28{
29 fd_t file = open(path);
30 if (file == ERR)
31 {
32 return NULL;
33 }
34
35 struct
36 {
37 uint32_t magic;
38 uint32_t width;
39 uint32_t height;
40 } header;
41 if (read(file, &header, sizeof(header)) == ERR)
42 {
43 close(file);
44 return NULL;
45 }
46
47 uint64_t fileSize = seek(file, 0, SEEK_END);
48 seek(file, sizeof(header), SEEK_SET);
49
50 if (fileSize != header.width * header.height * sizeof(pixel_t) + sizeof(header) || header.magic != FBMP_MAGIC)
51 {
52 close(file);
53 return NULL;
54 }
55
56 image_t* image = image_new_blank(disp, header.width, header.height);
57 if (image == NULL)
58 {
59 close(file);
60 return NULL;
61 }
62
63 if (read(file, image->draw.buffer, header.width * header.height * sizeof(pixel_t)) == ERR)
64 {
66 close(file);
67 return NULL;
68 }
69 close(file);
70
71 return image;
72}
73
83
85{
86 return &image->draw;
87}
88
90{
91 *rect = image->draw.contentRect;
92}
93
98
#define SEEK_SET
Definition SEEK.h:4
#define SEEK_END
Definition SEEK.h:6
fd_t open(const char *path)
System call for opening files.
Definition open.c:9
uint64_t close(fd_t fd)
System call for closing files.
Definition close.c:9
uint64_t seek(fd_t fd, int64_t offset, seek_origin_t origin)
System call for changing the file offset.
Definition seek.c:9
uint64_t read(fd_t fd, void *buffer, uint64_t count)
System call for reading from files.
Definition read.c:9
static void list_remove(list_t *list, list_entry_t *entry)
Removes a list entry from its current list.
Definition list.h:317
static void list_push(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
Definition list.h:345
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
Definition list.h:184
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ fd_t
A file descriptor.
Definition fd_t.h:12
void image_free(image_t *image)
Definition image.c:74
uint64_t image_height(image_t *image)
Definition image.c:99
#define FBMP_MAGIC
Definition image.c:5
void image_rect(image_t *image, rect_t *rect)
Definition image.c:89
uint64_t image_width(image_t *image)
Definition image.c:94
image_t * image_new(display_t *disp, const char *path)
Definition image.c:27
image_t * image_new_blank(display_t *disp, uint64_t width, uint64_t height)
Definition image.c:7
drawable_t * image_draw(image_t *image)
Definition image.c:84
static dentry_t * file
Definition log_file.c:17
uint32_t pixel_t
Definition pixel.h:11
static image_t * image
Definition main.c:5
#define RECT_INIT_DIM(x, y, width, height)
Definition rect.h:32
#define RECT_HEIGHT(rect)
Definition rect.h:39
#define RECT_WIDTH(rect)
Definition rect.h:38
__UINT32_TYPE__ uint32_t
Definition stdint.h:15
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC void * malloc(size_t size)
Definition malloc.c:5
_PUBLIC void free(void *ptr)
Definition free.c:11
Opaque display structure.
Definition internal.h:61
mtx_t mutex
Definition internal.h:73
list_t images
Definition internal.h:71
Drawable structure.
Definition drawable.h:35
pixel_t * buffer
Definition drawable.h:38
uint32_t stride
Definition drawable.h:37
rect_t contentRect
Definition drawable.h:39
display_t * disp
Definition drawable.h:36
rect_t invalidRect
Definition drawable.h:40
drawable_t draw
Definition internal.h:12
list_entry_t entry
Definition internal.h:11
Definition rect.h:13
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10