-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmpRW.c
221 lines (175 loc) · 5.32 KB
/
bmpRW.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<stdio.h>
#include<stdlib.h>
#include"bmpRW.h"
void bmpRFunc(char *fileN,struct bmpD **pointer)
{
int i,j;
FILE* f;
struct bmpD *data;
f=fopen(fileN,"rb");
if( f == NULL)
printf("can not open the file : \"%s\"",fileN);
data=(struct bmpD*)malloc(sizeof(struct bmpD));
fread(data,1,2,f);
fread(&(data->sizeT),1,52,f);
if(data->UsedColors!=0)
{
data->Colors = malloc(sizeof(char)*4*data->UsedColors);
fseek(f, 54, SEEK_SET);
fread(data->Colors,sizeof(char),data->UsedColors*4,f);
data->C.RY=malloc(sizeof(char)*data->Width*4*abs(data->Height));
fseek(f, data->offsetRGB, SEEK_SET);
for(i=0;i<abs(data->Height);i++)
{
for(j=0;j<abs(data->Width);j++)
{
fread(&(data->C.RY[i*data->Width+j]),1,1,f);
}
//skip zero padding
fseek(f, (4-data->Width*3%4)%4, SEEK_CUR);
}
}
else
{
data->C.RY=malloc(sizeof(char)*data->Width*abs(data->Height));
data->C.GU=malloc(sizeof(char)*data->Width*abs(data->Height));
data->C.BV=malloc(sizeof(char)*data->Width*abs(data->Height));
if(data->BPP==32)//have alpha
data->Alpha=malloc(sizeof(char)*data->Width*abs(data->Height));
fseek(f, data->offsetRGB, SEEK_SET);
for(i=0;i<abs(data->Height);i++)
{
for(j=0;j<abs(data->Width);j++)
{
fread(&(data->C.BV[i*data->Width+j]),1,1,f);
fread(&(data->C.GU[i*data->Width+j]),1,1,f);
fread(&(data->C.RY[i*data->Width+j]),1,1,f);
if(data->BPP==32)//have alpha
fread(&(data->Alpha[i*data->Width+j]),1,1,f);
}
//skip zero padding
if(data->BPP==32)
fseek(f, (4-data->Width*4%4)%4, SEEK_CUR);
else
fseek(f, (4-data->Width*3%4)%4, SEEK_CUR);
}
}
headerPrint(data);
*pointer=data;
}
//dir is the bit map horizontal direction
void bmpWFunc(char *fileN, struct bmpD* data, bool dir)
{
int i,j;
char tmp=0;
FILE *f;
f=fopen(fileN,"wb");
if( f == NULL)
printf("can not open the file : \"%s\"\n",fileN);
else
printf("open the file : \"%s\" success!\n",fileN);
if(data->UsedColors==0)
data->sizeT = 54 + (data->Width+(4-data->Width%4)%4)*abs(data->Height)*data->BPP/8;
else
data->sizeT = 54 + data->UsedColors*4 + data->Width*abs(data->Height);
data->offsetRGB = 54 + data->UsedColors*4;
data->sizeD = (data->Width+(4-data->Width%4)%4)*abs(data->Height)*data->BPP/8;
if(dir==0)
data->Height=abs(data->Height);
else
data->Height=-abs(data->Height);
printf("\noutput at : %s\n",fileN);
headerPrint(data);
fwrite(data,1,2,f);
fwrite(&(data->sizeT),1,52,f);
if(data->UsedColors!=0)
{
fwrite(data->Colors, 1, data->UsedColors*4, f);
for(i=0;i<abs(data->Height);i++)
{
for(j=0;j<abs(data->Width);j++)
fwrite(&(data->C.RY[i*data->Width+j]),1,1,f);
for(j=0;j<(4-data->Width*3%4)%4;j++)
fwrite(&tmp,1,1,f);
}
}
else
for(i=0;i<abs(data->Height);i++)
{
for(j=0;j<abs(data->Width);j++)
{
fwrite(&(data->C.BV[i*data->Width+j]),1,1,f);
fwrite(&(data->C.GU[i*data->Width+j]),1,1,f);
fwrite(&(data->C.RY[i*data->Width+j]),1,1,f);
if(data->BPP==32)
fwrite(&(data->Alpha[i*data->Width+j]),1,1,f);
}
if(data->BPP==32)
for(j=0;j<(4-data->Width*4%4)%4;j++)
fwrite(&tmp,1,1,f);
else
for(j=0;j<(4-data->Width*3%4)%4;j++)
fwrite(&tmp,1,1,f);
}
//unsigned char k=0x8c;
//fwrite(&k,1,1,f);
}
void headerPrint(struct bmpD* data)
{
int i;
printf("Image info :\n");
printf("%c%c\n",data->ID[0],data->ID[1]);
printf("File size : %d\n",data->sizeT);
printf("BMP data offset : %d\n",data->offsetRGB);
printf("BMP header size : %d\n",data->sizeInfo);
printf("Width=%d Height=%d\n",data->Width,data->Height);
printf("number of color plans : %d\n",data->planes);
printf("numbers of bit per pixel : %d\n",data->BPP);
printf("compression method used : %d\n",data->compression);
printf("raw RGB data size : %d\n",data->sizeD);
printf("horizontal resolution : %d\n",data->Hresolution);
printf("vertical resolution : %d\n",data->Vresolution);
printf("numbers of color in color plane : %d\n",data->UsedColors);
printf("Number of important colors : %d\n",data->ImportColors);
if(data->UsedColors == 0)
printf("no color plane data\n");
else
{
printf("color palette data :\n");
for(i=0;i<data->UsedColors/2;i++)
{
printf("R=%3u, G=%3u, B=%3u, A=%3u | ",data->Colors[i*8],data->Colors[i*8+1],data->Colors[i*8+2],data->Colors[i*8+3]);
printf("R=%3u, G=%3u, B=%3u, A=%3u\n",data->Colors[i*8+4],data->Colors[i*8+5],data->Colors[i*8+6],data->Colors[i*8+7]);
}
}
}
void YUV420RFunc(char *fileN, struct charcontainer_3 *data, unsigned int Width, unsigned int Height, unsigned int fn)
{
int i,j;
char *U,*V;
FILE *f;
f=fopen(fileN,"rb");
if( f == NULL)
printf("can not open the file : \"%s\"\n",fileN);
else
printf("open the file : \"%s\" success!\n",fileN);
//printf("%10ld\n",ftell(f));
fseek(f, fn*Width*Height*3/2, SEEK_SET);
//printf("%10ld, seek : %d, %d\n",ftell(f), fn,Width*Height*3/2);
fread(data->RY,Width*Height,1,f);
U = malloc(sizeof(unsigned char)*Width*Height/4);
fread(U,Width*Height/4,1,f);
V = malloc(sizeof(unsigned char)*Width*Height/4);
fread(V,Width*Height/4,1,f);
for(i=0;i<Height;i++)
for(j=0;j<Width;j++)
{
//printf("start:(%3d,%3d)=(%3d,%3d)\n",j,i,j/2,i/2);
data->GU[i*Width+j] = U[i/2*(Width/2)+j/2];
data->BV[i*Width+j] = V[i/2*(Width/2)+j/2];
//printf("end:(%3d,%3d)=(%3d,%3d)\n",j,i,j/2,i/2);
}
free(U);
free(V);
fclose(f);
}