diff options
| author | Syndamia <kamen@syndamia.com> | 2026-03-15 12:23:21 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2026-03-15 12:23:21 +0200 |
| commit | 2202e87c466803eeaddd974006aa9950d8e0d067 (patch) | |
| tree | 55cac9843e5789ac6ff29345ecd4fc44775c6b31 /RGBImage.c | |
| parent | 47e28b44615b9b068776f61b8741ea8692461c1d (diff) | |
| download | ppm_graphics-master.tar ppm_graphics-master.tar.gz ppm_graphics-master.zip | |
Diffstat (limited to 'RGBImage.c')
| -rw-r--r-- | RGBImage.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/RGBImage.c b/RGBImage.c new file mode 100644 index 0000000..2fe37ba --- /dev/null +++ b/RGBImage.c @@ -0,0 +1,76 @@ +#include "RGBImage.h" +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> + +void +ARGB_set(ARGB* rgb, byte4 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, 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; +} |
