| 1 |
#include "cc3_img_writer.h" |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
int cc3_img_write_file_create( cc3_image_t *img) |
|---|
| 9 |
{ |
|---|
| 10 |
char filename[32]; |
|---|
| 11 |
uint8_t ppm,val; |
|---|
| 12 |
FILE *f; |
|---|
| 13 |
static uint32_t ppm_cnt=0; |
|---|
| 14 |
if(img->channels==CC3_CHANNEL_ALL ) ppm=1; |
|---|
| 15 |
else ppm=0; |
|---|
| 16 |
do { |
|---|
| 17 |
#ifdef VIRTUAL_CAM |
|---|
| 18 |
if(ppm) |
|---|
| 19 |
sprintf(filename, "img%.5d.ppm", ppm_cnt); |
|---|
| 20 |
else sprintf(filename, "img%.5d.pgm", ppm_cnt); |
|---|
| 21 |
#else |
|---|
| 22 |
if(ppm) |
|---|
| 23 |
sprintf(filename, "c:/img%.5d.ppm", ppm_cnt); |
|---|
| 24 |
else sprintf(filename, "c:/img%.5d.pgm", ppm_cnt); |
|---|
| 25 |
#endif |
|---|
| 26 |
f = fopen(filename, "r"); |
|---|
| 27 |
if(f!=NULL ) { |
|---|
| 28 |
printf( "%s already exists...\n",filename ); |
|---|
| 29 |
ppm_cnt++; |
|---|
| 30 |
fclose(f); |
|---|
| 31 |
} |
|---|
| 32 |
} while(f!=NULL); |
|---|
| 33 |
ppm_cnt++; |
|---|
| 34 |
if(ppm) val= cc3_ppm_img_write(img, filename); |
|---|
| 35 |
else val= cc3_pgm_img_write(img, filename); |
|---|
| 36 |
return val; |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
int cc3_ppm_img_write(cc3_image_t *img, char *filename) |
|---|
| 40 |
{ |
|---|
| 41 |
FILE *fp; |
|---|
| 42 |
uint32_t size_x, size_y,x,y; |
|---|
| 43 |
cc3_pixel_t p; |
|---|
| 44 |
|
|---|
| 45 |
size_x = img->width; |
|---|
| 46 |
size_y = img->height; |
|---|
| 47 |
if(img->channels!=CC3_CHANNEL_ALL) |
|---|
| 48 |
{ |
|---|
| 49 |
printf( "ppm_write only works with 3 channel images\n" ); |
|---|
| 50 |
return 0; |
|---|
| 51 |
} |
|---|
| 52 |
fp = fopen(filename, "w"); |
|---|
| 53 |
if(fp==NULL ) |
|---|
| 54 |
{ |
|---|
| 55 |
fprintf(stderr,"Can't open file %s\r\n", filename); |
|---|
| 56 |
return 0; |
|---|
| 57 |
} |
|---|
| 58 |
fprintf(fp,"P3\n%d %d\n255\n",size_x,size_y ); |
|---|
| 59 |
|
|---|
| 60 |
for (y = 0; y < size_y; y++) { |
|---|
| 61 |
for (x = 0; x < size_x * 3; x++) { |
|---|
| 62 |
cc3_get_pixel(img,x,y,&p); |
|---|
| 63 |
fprintf(fp,"%d %d %d ",p.channel[0],p.channel[1],p.channel[2]); |
|---|
| 64 |
} |
|---|
| 65 |
fprintf(fp,"\n"); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
fclose(fp); |
|---|
| 69 |
return 1; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
int cc3_pgm_img_write(cc3_image_t *img, char *filename) |
|---|
| 74 |
{ |
|---|
| 75 |
FILE *fp; |
|---|
| 76 |
uint32_t size_x, size_y,x,y; |
|---|
| 77 |
cc3_pixel_t p; |
|---|
| 78 |
|
|---|
| 79 |
size_x = img->width; |
|---|
| 80 |
size_y = img->height; |
|---|
| 81 |
fp = fopen(filename, "w"); |
|---|
| 82 |
if(fp==NULL ) |
|---|
| 83 |
{ |
|---|
| 84 |
fprintf(stderr,"Can't open file %s\r\n", filename); |
|---|
| 85 |
return 0; |
|---|
| 86 |
} |
|---|
| 87 |
fprintf(fp,"P5\n%d %d\n255\n",size_x,size_y ); |
|---|
| 88 |
|
|---|
| 89 |
for (y = 0; y < size_y; y++) { |
|---|
| 90 |
for (x = 0; x < size_x; x++) { |
|---|
| 91 |
cc3_get_pixel(img,x,y,&p); |
|---|
| 92 |
fprintf(fp,"%c",p.channel[0]); |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
fclose(fp); |
|---|
| 97 |
return 1; |
|---|
| 98 |
} |
|---|