Loading lang_c_tr_03...
printf("\n~~~~ %s() ~~~~\n", __func__);
->(flèche).
//----------------------------------------------------------------------------
#ifndef PIXEL_H #define PIXEL_H
#include <stdint.h>
typedef struct { uint8_t r, g, b; } Pixel;
inline static Pixel Pixel_rgb(uint8_t r, uint8_t g, uint8_t b) { Pixel p={r, g, b}; return p; }
inline static Pixel Pixel_grey(uint8_t g) { return Pixel_rgb(g, g, g); }
inline static uint8_t Pixel_asGrey(Pixel p) { return (uint8_t)((p.r+p.g+p.b)/3); }
inline static Pixel Pixel_inverse(Pixel p) { return Pixel_rgb((uint8_t)~p.r, (uint8_t)~p.g, (uint8_t)~p.b); }
#endif // PIXEL_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifndef IMAGE_H #define IMAGE_H
#include <stdbool.h> #include "pixel.h"
typedef struct { int width, height; Pixel *pixels; } Image;
Image Image_create(int width, int height, Pixel color);
void Image_destroy(Image *img);
void Image_greyify(Image *img);
void Image_invert(Image *img);
inline static void Image_set(Image *img, int x, int y, Pixel color) { int width=img->width; int height=img->height; if((x>=0)&&(x<width)&&(y>=0)&&(y<height)) { img->pixels[y*width+x]=color; } }
inline static Pixel Image_get(const Image *img, int x, int y) { int width=img->width; int height=img->height; return (x>=0)&&(x<width)&&(y>=0)&&(y<height) ? img->pixels[y*width+x] : Pixel_grey(0); }
bool // success Image_saveP3(const Image *img, const char *fileName);
bool // success Image_saveP6(const Image *img, const char *fileName);
Image Image_loadP3(const char *fileName);
Image Image_loadP6(const char *fileName);
#endif // IMAGE_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "image.h" #include <stdlib.h> #include <stdio.h>
Image Image_create(int width, int height, Pixel color) { int pixelCount=width*height; Pixel *pixels=(Pixel *)malloc(pixelCount*sizeof(Pixel)); if(pixels==NULL) { abort(); } for(int i=0; i<pixelCount; ++i) { pixels[i]=color; } Image img={width, height, pixels}; return img; }
void Image_destroy(Image *img) { free(img->pixels); img->width=0; img->height=0; img->pixels=NULL; }
void Image_greyify(Image *img) { Pixel *pixels=img->pixels; int pixelCount=img->width*img->height; for(int i=0; i<pixelCount; ++i) { pixels[i]=Pixel_grey(Pixel_asGrey(pixels[i])); } }
void Image_invert(Image *img) { Pixel *pixels=img->pixels; int pixelCount=img->width*img->height; for(int i=0; i<pixelCount; ++i) { pixels[i]=Pixel_inverse(pixels[i]); } }
bool // success Image_saveP3(const Image *img, const char *fileName) { FILE *f=fopen(fileName,"w"); if(f==NULL) { return false; } fprintf(f, "P3\n%d %d\n255\n", img->width, img->height); int pixelCount=img->width*img->height; Pixel *pixels=img->pixels; for(int i=0; i<pixelCount; ++i) { Pixel p=pixels[i]; fprintf(f, "%d %d %d ", p.r, p.g, p.b); } fprintf(f, "\n"); fclose(f); return true; }
bool // success Image_saveP6(const Image *img, const char *fileName) { FILE *f=fopen(fileName, "wb"); if(f==NULL) { return false; } fprintf(f, "P6\n%d %d\n255\n", img->width, img->height); int pixelCount=img->width*img->height; fwrite(img->pixels, pixelCount*sizeof(Pixel), 1, f); fclose(f); return true; }
Image Image_loadP3(const char *fileName) { Image img={0, 0, NULL}; FILE *f=fopen(fileName, "r"); if(f==NULL) { return img; } int width,height,range; if((fscanf(f, "P3 %d %d %d", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return img; } int pixelCount=width*height; Pixel *pixels=(Pixel *)malloc(pixelCount*sizeof(Pixel)); if(pixels==NULL) { abort(); } for(int i=0; i<pixelCount; ++i) { int r, g, b; if(fscanf(f, "%d %d %d", &r, &g, &b)!=3) { free(pixels); fclose(f); return img; } pixels[i]=Pixel_rgb((uint8_t)r, (uint8_t)g, (uint8_t)b); } fclose(f); img.width=width; img.height=height; img.pixels=pixels; return img; }
Image Image_loadP6(const char *fileName) { Image img={0, 0, NULL}; FILE *f=fopen(fileName, "rb"); if(f==NULL) { return img; } int width,height,range; if((fscanf(f, "P6 %d %d %d\n", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return img; } int pixelCount=width*height; Pixel *pixels=(Pixel *)malloc(pixelCount*sizeof(Pixel)); if(pixels==NULL) { abort(); } if(fread(pixels, pixelCount*sizeof(Pixel), 1, f)!=1) { free(pixels); fclose(f); return img; } fclose(f); img.width=width; img.height=height; img.pixels=pixels; return img; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include "image.h"
void test_Image_saveP3(void) { printf("\n~~~~ %s() ~~~~\n",__func__); Image img=Image_create(320, 200, Pixel_rgb(50, 100, 150)); for(int y=0; y<img.height; ++y) { for(int x=0; x<img.width; ++x) { Pixel p=Image_get(&img, x, y); if(x&(1<<5)) { p.r=255; } if((~y)&(1<<3)) { p.g=255; } Image_set(&img, x, y, p); } } if(!Image_saveP3(&img, "output_colour_P3.ppm")) { fprintf(stderr, "Image_saveP3() failure\n"); } Image_destroy(&img); }
void test_Image_saveP6(void) { printf("\n~~~~ %s() ~~~~\n",__func__); Image img=Image_create(320, 200, Pixel_rgb(150, 100, 50)); for(int y=0; y<img.height; ++y) { for(int x=0; x<img.width; ++x) { Pixel p=Image_get(&img, x, y); if((~y)&(1<<5)) { p.g=255; } if(x&(1<<3)) { p.b=255; } Image_set(&img, x, y, p); } } if(!Image_saveP6(&img, "output_colour_P6.ppm")) { fprintf(stderr, "Image_saveP6() failure\n"); } Image_destroy(&img); }
void test_Image_loadP3(void) { printf("\n~~~~ %s() ~~~~\n",__func__); const char *input="input_P3.ppm"; Image img=Image_loadP3(input); if(img.pixels==NULL) { fprintf(stderr, "cannot load %s\n", input); return; } Image_greyify(&img); if(!Image_saveP3(&img, "output_grey_P3.ppm")) { fprintf(stderr, "Image_saveP3() failure\n"); } Image_destroy(&img); }
void test_Image_loadP6(void) { printf("\n~~~~ %s() ~~~~\n",__func__); const char *input="input_P6.ppm"; Image img=Image_loadP6(input); if(img.pixels==NULL) { fprintf(stderr, "cannot load %s\n", input); return; } Image_invert(&img); if(!Image_saveP6(&img, "output_inverse_P6.ppm")) { fprintf(stderr, "Image_saveP6() failure\n"); } Image_destroy(&img); }
int main(void) { test_Image_saveP3(); test_Image_saveP6(); test_Image_loadP3(); test_Image_loadP6(); return 0; }
//----------------------------------------------------------------------------