diff options
Diffstat (limited to 'RGBImage')
| -rw-r--r-- | RGBImage/RGBImage.c | 83 | ||||
| -rw-r--r-- | RGBImage/RGBImage.h | 35 |
2 files changed, 118 insertions, 0 deletions
diff --git a/RGBImage/RGBImage.c b/RGBImage/RGBImage.c new file mode 100644 index 0000000..2896d72 --- /dev/null +++ b/RGBImage/RGBImage.c @@ -0,0 +1,83 @@ +#include "RGBImage.h" +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> + +void +ARGB_set(ARGB* rgb, color color) { + rgb->a = (0xFF000000 & color) >> 24; + rgb->r = (0x00FF0000 & color) >> 16; + rgb->g = (0x0000FF00 & color) >> 8; + rgb->b = (0x000000FF & color); +} + +void +ARGB_merge(ARGB* bottom, color top) { + ARGB temp; + ARGB_set(&temp, top); + ARGB_mergeARGB(bottom, temp); +} + +void +ARGB_mergeARGB(ARGB* bottom, ARGB top) { + bottom->a = 255; + double perc = top.a / 255.0; + + bottom->r = (bottom->r * (1.0 - perc)) + (top.r * perc); + bottom->g = (bottom->g * (1.0 - perc)) + (top.g * perc); + bottom->b = (bottom->b * (1.0 - perc)) + (top.b * perc); +} + +RGBImage +RGBImage_new(u32 width, u32 height) { + return (RGBImage){ + .width = width, + .height = height, + .img = malloc(sizeof(ARGB) * width * height) + }; +} + +void +RGBImage_free(RGBImage img) { + free(img.img); +} + +void +RGBImage_delete(RGBImage* img) { + RGBImage_free(*img); + img->width = img->height = 0; + img->img = NULL; +} + +ARGB* +RGBImage_at(RGBImage img, int row, int col) { + return img.img + (row % img.height) * img.width + col % img.width; +} + +int +ppm6_write(FILE* f, RGBImage img) { + fprintf(f, "P6\n%d %d\n255\n", img.width, img.height); + size_t size = img.width * img.height; + for (size_t i = 0; i < size; ++i) { + fputc(img.img[i].r, f); + fputc(img.img[i].g, f); + fputc(img.img[i].b, f); + } + return fflush(f); +} + +RGBImage +ppm_read(FILE* f) { + u32 type, width, height, colors; + fscanf(f, "P%d\n%d %d\n%d\n", &type, &width, &height, &colors); + + RGBImage img = RGBImage_new(width, height); + ARGB* pixel = img.img; + for (size_t i = img.width * img.height; i > 0; --i, ++pixel) { + if (type == 3) { + pixel->a = 0xFF; + fscanf(f, "%d %d %d ", &pixel->r, &pixel->g, &pixel->b); + } + } + return img; +} diff --git a/RGBImage/RGBImage.h b/RGBImage/RGBImage.h new file mode 100644 index 0000000..8980f7b --- /dev/null +++ b/RGBImage/RGBImage.h @@ -0,0 +1,35 @@ +#ifndef _RGBIMAGE +#define _RGBIMAGE + +#include "../global.h" +#include <stdio.h> + +typedef uint8_t byte; +typedef uint32_t color; + +typedef struct ARGB { + byte a; + byte r; + byte g; + byte b; +} ARGB; + +void ARGB_merge(ARGB* bottom, color top); +void ARGB_mergeARGB(ARGB* bottom, ARGB top); +void ARGB_set(ARGB* rgb, color color); + +typedef struct RGBImage { + u32 width; + u32 height; + ARGB* img; +} RGBImage; + +RGBImage RGBImage_new(u32 width, u32 height); +void RGBImage_free(RGBImage img); +void RGBImage_delete(RGBImage* img); +ARGB* RGBImage_at(RGBImage img, int row, int col); + +int ppm6_write(FILE* f, RGBImage img); +RGBImage ppm_read(FILE* f); + +#endif /* _RGBIMAGE */ |
