add helper function to compute pixel indices
This commit is contained in:
parent
ba11946be5
commit
86f8f4fec9
|
@ -16,4 +16,8 @@ struct image {
|
|||
struct image *create_image(size_t w, size_t h);
|
||||
void destroy_image(struct image *image);
|
||||
|
||||
static inline size_t to_index(size_t h, size_t w, const struct image *image) {
|
||||
return h * image->w + w;
|
||||
}
|
||||
|
||||
#endif /* !IMAGE_H */
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
static void fill_image(struct image *image) {
|
||||
for (size_t i = 0; i < image->h; ++i) {
|
||||
for (size_t j = 0; j < image->w; ++j) {
|
||||
struct pixel *p = &image->buf[i * image->w + j];
|
||||
struct pixel *p = &image->buf[to_index(i, j, image)];
|
||||
p->r = 255 * i / image->h;
|
||||
p->g = 255 * j / image->w;
|
||||
p->g = 255 * (i + j) / (image->h + image->w);
|
||||
|
|
|
@ -14,7 +14,7 @@ void print_ppm(const struct image *image, FILE *f) {
|
|||
for (size_t j = 0; j < image->w; ++j) {
|
||||
if (j != 0)
|
||||
fputc(' ', f);
|
||||
const struct pixel *p = &image->buf[i * image->w + j];
|
||||
const struct pixel *p = &image->buf[to_index(i, j, image)];
|
||||
fprintf(f, "%hhu %hhu %hhu", p->r, p->g, p->b);
|
||||
}
|
||||
fputc('\n', f);
|
||||
|
|
Loading…
Reference in a new issue