#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "image.h"

unsigned char * new_image(int w, int h) {
    unsigned char * px = malloc(3 * w * h);
    assert(px != NULL);
    for (unsigned char * p = px; p < px + 3 * w * h; p++) {
        *p = 0;
    }
    return px;
}

void put_pixel(unsigned char ** p, int r, int g, int b) {
    **p = r;
    *(*p + 1) = g;
    *(*p + 2) = b;
    *p += 3;
}

void write_ppm(unsigned char * px, int w, int h) {
    printf("P6\n%d %d\n255\n", w, h);
    fwrite(px, 1, 3 * w * h, stdout);
}

unsigned char * read_ppm(int * w, int * h) {
    int max;
    int n = scanf("P6 %d %d %d", w, h, &max);
    assert(n == 3);
    getchar();
    unsigned char * px = new_image(*w, *h);
    size_t got = fread(px, 1, 3 * *w * *h, stdin);
    assert(got == (size_t) (3 * *w * *h));
    return px;
}
