#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;
}
