-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.c
171 lines (138 loc) · 3.89 KB
/
image.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* Modulo: Implementação do TAD: Imagem
*
* Curso: Computação Gráfica - 2000
* Ultima alteração: 06mar2000
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "image.h"
struct Image_impl{
int h; /* número de linhas da Image */
int w; /* número de colunas da Image */
int color; /* [0,1] 0 - gray 1 - color image */
unsigned char *red; /* matriz das componentes vermelha */
unsigned char *green; /* matriz das componentes verde */
unsigned char *blue; /* matriz das componentes azul */
};
Image *imgCreate(int _w, int _h){
Image *img;
if ( _w<=0 || _h<=0) return NULL;
img=(Image*) malloc(sizeof(Image));
if (img!=NULL ){
img->w=_w;
img->h=_h;
img->red = (unsigned char *) malloc (_w * _h * sizeof(unsigned char) );
img->green = (unsigned char *) malloc (_w * _h * sizeof(unsigned char) );
img->blue = (unsigned char *) malloc (_w * _h * sizeof(unsigned char) );
if (img->red==NULL || img->green==NULL || img->blue==NULL ) {
if (img->red !=NULL) free(img->red);
if (img->green!=NULL) free(img->green);
if (img->blue !=NULL) free(img->blue);
free (img);
return NULL;
}
}
img->color = 1;
return img;
}
void imgDestroy(Image * img){
if (img !=NULL) {
if (img->red) free (img->red);
if (img->green) free (img->green);
if (img->blue) free (img->blue);
free(img);
}
}
unsigned char imgGetPixelRed(Image *img, int x, int y) {
return img->red[y*img->w+x];
}
unsigned char imgGetPixelGreen(Image *img, int x, int y) {
return img->green[y*img->w+x];
}
unsigned char imgGetPixelBlue(Image *img, int x, int y) {
return img->blue[y*img->w+x];
}
void imgSetPixel(Image *img, int x, int y, unsigned char r, unsigned char g, unsigned char b){
img->red[y*img->w+x] = r;
img->green[y*img->w+x] = g;
img->blue[y*img->w+x] = b;
return;
}
int imgGetWidth(Image *img) {
return img->w;
}
int imgGetHeight(Image *img) {
return img->h;
}
unsigned char *imgGetRedChannel(Image *img) {
return img->red;
}
unsigned char *imgGetGreenChannel(Image *img) {
return img->green;
}
unsigned char *imgGetBlueChannel(Image *img) {
return img->blue;
}
Image * imgReadPPM(char *filename) {
Image *img=NULL; /* imagem criada */
FILE *fp;
char line[80];
unsigned char pixel[3];
int x,y;
int w,h,maxcolor;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("\n[%s:%d] unable to load image file '%s'.\n", __FILE__, __LINE__, filename);
free(img);
return img;
}
fgets(line,80,fp);
if(strcmp(line,"P6\n")) {
return img;
}
while (fscanf( fp, " %d ", &w ) != 1)
fgets(line, 80, fp);
while (fscanf( fp, " %d ", &h ) != 1)
fgets(line, 80, fp);
while (fscanf( fp, " %d", &maxcolor ) != 1)
fgets(line, 80, fp);
fgetc(fp);
img = imgCreate(w,h);
/* le invertendo as linhas */
for ( y=imgGetHeight(img)-1; y>=0; y-- ) {
for ( x=0; x<imgGetWidth(img); x++ ) {
fread( pixel, 3, 1, fp );
imgSetPixel(img, x, y, pixel[0], pixel[1], pixel[2]);
}
}
fclose(fp);
return img;
}
int imgWritePPM(Image * img, char * filename)
{
FILE * fp;
int x,y;
int maxcolor=255;
unsigned char red, green, blue;
if ((fp = fopen(filename, "wb")) == NULL)
{
printf("\n[%s:%d] unable to write image file '%s'\n",
__FILE__, __LINE__, filename);
return 0;
}
/*cabeçalho de um arquivo .ppm */
fprintf(fp,"P6\n%d %d\n%d\n", img->w, img->h, maxcolor);
/* gravando invertendo o eixo y */
for ( y=imgGetHeight(img)-1; y>=0; y-- )
for ( x=0; x<imgGetWidth(img); x++ )
{
red = imgGetPixelRed(img, x, y);
green = imgGetPixelGreen(img, x, y);
blue = imgGetPixelBlue(img, x, y);
fprintf(fp,"%c%c%c", red, green, blue);
}
printf(" %s was written successfully.\n",filename);
fclose(fp);
return 1;
}