Read and Write a PPM file.

Read and Write a PPM file from scratch

See version :

Pas de dépendances

Download :

Plusieurs fichiers :
01_09_01_testppm.c
01_09_01_quickimage.h
01_09_01_quickimage.c


01_09_01_testppm.c

#include "01_09_01_quickimage.h" int main() { int i,j; Image* I = NouvelleImage(256,256); for(i=0;i<256;i++) { for(j=0;j<256;j++) { Pixel p; p.r = i; p.g = j; p.b = 0; SetPixel(I,i,j,p); } } //Image* I = Charger("test.ppm"); Sauver(I,"test.ppm"); DelImage(I); return 0; }


01_09_01_quickimage.h

#ifndef _QUICKIMAGE_H #define _QUICKIMAGE_H typedef struct Pixel { unsigned char r,g,b; } Pixel; typedef struct Image { int w,h; Pixel* dat; } Image; Image* Charger(const char* fichier); int Sauver(Image*,const char* fichier); Image* NouvelleImage(int w,int h); Image* CopieImage(Image*); void SetPixel(Image*,int i,int j,Pixel p); Pixel GetPixel(Image*,int i,int j); void DelImage(Image*); #endif


01_09_01_quickimage.c

#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include "01_09_01_quickimage.h" Image* NouvelleImage(int w,int h) { Image* I = malloc(sizeof(Image)); I->w = w; I->h = h; I->dat = calloc(1,w*h*sizeof(Pixel*)); return I; } Image* CopieImage(Image* I) { Image* res; if (!I) return NULL; res = NouvelleImage(I->w,I->h); memcpy(res->dat,I->dat,I->w*I->h*sizeof(Pixel)); return res; } void DelImage(Image* I) { if (I) { free(I->dat); free(I); } } void SetPixel(Image* I,int i,int j,Pixel p) { assert(I && i>=0 && i<I->w && j>=0 && j<I->h); I->dat[I->w*j+i] = p; } Pixel GetPixel(Image* I,int i,int j) { assert(I && i>=0 && i<I->w && j>=0 && j<I->h); return I->dat[I->w*j+i]; } Image* Charger(const char* fichier) { int i,j,max; char buf[10]; Image* I; FILE* F = fopen(fichier,"r"); if (!F) return NULL; fscanf(F,"%s %d %d %d",buf,&i,&j,&max); I = NouvelleImage(i,j); for(i=0;i<I->w*I->h;i++) { int r,g,b; fscanf(F,"%d %d %d",&r,&g,&b); I->dat[i].r = (unsigned char)r; I->dat[i].g = (unsigned char)g; I->dat[i].b = (unsigned char)b; } fclose(F); return I; } int Sauver(Image* I,const char* fichier) { int i; FILE* F = fopen(fichier,"w"); if (!F) return -1; fprintf(F,"P3\n%d %d\n255\n",I->w,I->h); for(i=0;i<I->w*I->h;i++) fprintf(F,"%d %d %d ",I->dat[i].r,I->dat[i].g,I->dat[i].b); fclose(F); return 0; }


Explanations

	No explanations yet.