Add Image data structure
This commit is contained in:
		
							
								
								
									
										30
									
								
								src/image.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/image.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| #include "image.h" | ||||
| #include "mem_allocator.h" | ||||
| #include "mem_libc.h" | ||||
| #include <stddef.h> | ||||
| #include <string.h> | ||||
|  | ||||
| Image *create_image(u64 width, u64 height, Pixel *data, | ||||
|                     const Allocator *allocator) { | ||||
|   Allocator alloc; | ||||
|   if (!allocator) { | ||||
|     alloc = wapp_mem_libc_allocator(); | ||||
|   } else { | ||||
|     alloc = *allocator; | ||||
|   } | ||||
|  | ||||
|   u64 buf_length = width * height; | ||||
|   u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length; | ||||
|  | ||||
|   Image *img = wapp_mem_allocator_alloc(&alloc, total_size); | ||||
|   if (!img) { | ||||
|     return NULL; | ||||
|   } | ||||
|  | ||||
|   img->width = width; | ||||
|   img->height = height; | ||||
|   img->buf_length = buf_length; | ||||
|   memcpy(img->data, data, buf_length); | ||||
|  | ||||
|   return img; | ||||
| } | ||||
							
								
								
									
										34
									
								
								src/image.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/image.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| #ifndef IMAGE_H | ||||
| #define IMAGE_H | ||||
|  | ||||
| #include "mem_allocator.h" | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif // __cplusplus | ||||
|  | ||||
| #include "aliases.h" | ||||
|  | ||||
| typedef struct pixel Pixel; | ||||
| struct pixel { | ||||
|   u8 r; | ||||
|   u8 g; | ||||
|   u8 b; | ||||
|   u8 a; | ||||
| }; | ||||
|  | ||||
| typedef struct image Image; | ||||
| struct image { | ||||
|   u64 width; | ||||
|   u64 height; | ||||
|   u64 buf_length; | ||||
|   Pixel data[]; | ||||
| }; | ||||
|  | ||||
| Image *create_image(u64 width, u64 height, Pixel *data, | ||||
|                     const Allocator *allocator); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif // __cplusplus | ||||
|  | ||||
| #endif // !IMAGE_H | ||||
		Reference in New Issue
	
	Block a user