Code Bitmap
Code Bitmap
cpp 1
1 #pragma pack (1)
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 //*******************************************************************
6 typedef struct
7 {
8 unsigned short bfType;
9 unsigned long bfSize;
10 unsigned short bfReserved1;
11 unsigned short bfReserved2;
12 unsigned long bfOffBits;
13 } BITMAPFILEHEADER;
14
15 typedef struct
16 {
17 unsigned long biSize;
18 unsigned long biWidth;
19 unsigned long biHeight;
20 unsigned short biPlanes;
21 unsigned short biBitCount;
22 unsigned long biCompression;
23 unsigned long biSizeImage;
24 unsigned long biXPelsPerMeter;
25 unsigned long biYPelsPerMeter;
26 unsigned long biClrUsed;
27 unsigned long biClrImportant;
28 } BITMAPINFOHEADER;
29
30 //***********************************************************
31 void giaiphong14 (char **pixelp, char *palettep, int height);
32 void giaiphong824 (int **pixelp, char *palettep, int height);
33 void docbitmap (FILE *fp, BITMAPFILEHEADER &fheader, BITMAPINFOHEADER &inheader, char *
&palettep, int &bytepp, int &sizepad, char **&pixelp14, char **&pixelptmp, int **&
pixelp824);
34 void xuatbitmap14 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char
*palettep, int bytepp, char **pixelp );
35 void xuatbitmap824 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char
*palettep, char bytepp, char sizepad, int **pixelp824 );
36 char getbit (int h, int w, char **pixelp, char bitcount);
37 void biton (int h, int w, char **pixelp);
38 void bitoff(int h, int w, char **pixelp);
39 void crop14 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char *
palettep, char **pixelp, int h, int w);
40 void crop824 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char *
palettep, char bytepp, char sizepad, int **pixelp824, int h, int w);
41 //***********************************************************
42 char main (char argc, char *argv[])
43 {
44 if (argc != 2)
45 {
46 printf ("Syntax error: %s <filename>\n", argv[0]);
47 return 1;
48 }
49
50 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++
51
52 FILE *fpin = fopen (argv[1], "rb");
53 if (fpin == NULL)
54 {
55 printf ("Can't open file %s\n", argv[1]);
56 return 1;
57 }
58 FILE *fpout = fopen ("Output.bmp", "wb");
59 if (fpout == NULL)
d:\KY THUAT LAP TRINH\bai tap bitmap\code bitmap\code_bitmap.cpp 2
60 {
61 printf ("Can't open file Output.bmp\n");
62 return 1;
63 }
64
65 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++
66
67 BITMAPFILEHEADER fheader; //Doc
file bitmap
68 BITMAPINFOHEADER inheader;
69 char *palettep, **pixelp14, **pixelptmp;
70 int sizepad, bytepp, **pixelp824;
71 docbitmap (fpin, fheader, inheader, palettep, bytepp, sizepad, pixelp14, pixelptmp,
pixelp824);
72
73 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++
74
75 if (inheader.biBitCount < 8) //copy
ra file Output.bmp
76 xuatbitmap14 (fpout, fheader, inheader, palettep, bytepp, pixelptmp);
77 else
78 {
79 xuatbitmap824 (fpout, fheader, inheader, palettep, bytepp, sizepad, pixelp824);
80 }
81
82 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++
83 int h, w; //Tao file Convert.bmp
84 printf ("Nhap height, width:\n");
85 scanf ("%d%d", &h, &w);
86 FILE * fpconv = fopen ("Convert.bmp", "wb");
87 if (fpconv == NULL)
88 {
89 printf("Can't open file Convert.bmp\n");
90 if (inheader.biBitCount < 8)
91 giaiphong14 (pixelp14, palettep, inheader.biHeight);
92 else
93 giaiphong824 (pixelp824, palettep, inheader.biHeight);
94 return 1;
95 }
96 if (inheader.biBitCount < 8)
97 crop14 (fpconv, fheader, inheader, palettep, pixelp14, h, w);
98 else
99 crop824 (fpconv, fheader, inheader, palettep, bytepp, sizepad, pixelp824, h, w)
;
100
101 if (inheader.biBitCount < 8)
102 giaiphong14 (pixelp14, palettep, inheader.biHeight);
103 else
104 giaiphong824 (pixelp824, palettep, inheader.biHeight);
105
106 return 0;
107 }
108
109 //**********************************************************
110 void giaiphong14 (char **pixelp, char *palettep, int height)
111 {
112 for (int i = 0; i < height; i++)
113 free (pixelp[i]);
114 free (pixelp);
115 free (palettep);
116 }
117
118 //*********************************************************
d:\KY THUAT LAP TRINH\bai tap bitmap\code bitmap\code_bitmap.cpp 3
119 void giaiphong824 (int **pixelp, char *palettep, int height)
120 {
121 for (int i = 0; i < height; i++)
122 free (pixelp[i]);
123 free (pixelp);
124 free (palettep);
125 }
126
127 //**********************************************************
128 void docbitmap (FILE *fp, BITMAPFILEHEADER &fheader, BITMAPINFOHEADER &inheader, char *
&palettep, int &bytepp, int &sizepad, char **&pixelp14, char **&pixelptmp,int **&
pixelp824)
129 {
130 fread (&fheader, sizeof (fheader), 1, fp); //Doc BITMAPFILEHEADER
131 fread (&inheader, sizeof (inheader), 1, fp); //Doc BITMAPINFOHEADER
132
133 palettep = (char *) malloc (sizeof (char) * (fheader.bfOffBits - 54));
134 fread (palettep, fheader.bfOffBits - 54, 1, fp); //Doc PALETTE
135
136 if (inheader.biBitCount < 8)
137 {
138 bytepp = (inheader.biSizeImage / inheader.biHeight);
139 pixelp14 = (char **) malloc (sizeof (char *) * inheader.biHeight);
140 for (int i = 0; i < inheader.biHeight; i++)
141 pixelp14[i] = (char *) malloc (sizeof (char) * bytepp);
142 for (int i = 0; i < inheader.biHeight; i++) //Doc mang cac pixel
143 for (int j = 0; j < bytepp; j++)
144 fread (&pixelp14[i][j], 1 , 1, fp);
145
146 pixelptmp = (char **) malloc (sizeof (char *) * inheader.biHeight);
147 for (int i = 0; i < inheader.biHeight; i++)
148 pixelptmp[i] = (char *) malloc (sizeof (char) * inheader.biWidth);
149 for (int i = 0; i < inheader.biHeight; i++)
150 for (int j = 0; j < inheader.biWidth; j++)
151 pixelptmp[i][j] = getbit (i, j, pixelp14, inheader.biBitCount);
152
153 char **tmp = pixelp14;
154 pixelp14 = pixelptmp;
155 pixelptmp = tmp;
156 }
157 else
158 {
159 bytepp = inheader.biBitCount / 8; //So byte tren moi pixel
160 sizepad = (inheader.biSizeImage / inheader.biHeight) - (inheader.biWidth *
bytepp); //so byte cua padding
161 pixelp824 = (int **) malloc (sizeof (int *) * inheader.biHeight);
162 for (int i = 0; i < inheader.biHeight; i++)
163 pixelp824[i] = (int *) malloc (sizeof (int) * inheader.biWidth);
164 int tmp;
165 for (int i = 0; i < inheader.biHeight; i++) //Doc mang cac pixel
166 {
167 for (int j = 0; j < inheader.biWidth; j++)
168 fread (&pixelp824[i][j], bytepp, 1, fp);
169 fread (&tmp, sizepad, 1, fp);
170 }
171 }
172 fclose (fp);
173 }
174
175 //***********************************************************
176 void xuatbitmap14 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char
*palettep, int bytepp, char **pixelptmp )
177 {
178 int tmp = 0x00000000;
179 fwrite (&fheader, sizeof (fheader), 1, fp);
180 fwrite (&inheader, sizeof (inheader), 1, fp);
d:\KY THUAT LAP TRINH\bai tap bitmap\code bitmap\code_bitmap.cpp 4
181 fwrite (palettep, fheader.bfOffBits - 54, 1, fp);
182
183 for (int i = 0; i < inheader.biHeight; i++)
184 for (int j = 0; j < bytepp; j++)
185 fwrite (&pixelptmp[i][j],1, 1, fp);
186
187 for (int i = 0; i < inheader.biHeight; i++)
188 free (pixelptmp[i]);
189 free (pixelptmp);
190
191 fclose (fp);
192 }
193
194 //***********************************************************
195 void xuatbitmap824 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char
*palettep, char bytepp, char sizepad, int **pixelp824 )
196 {
197 int tmp = 0x00000000;
198 fwrite (&fheader, sizeof (fheader), 1, fp);
199 fwrite (&inheader, sizeof (inheader), 1, fp);
200 fwrite (palettep, fheader.bfOffBits - 54, 1, fp);
201 for (int i = 0; i < inheader.biHeight; i++)
202 {
203 for (int j = 0; j < inheader.biWidth; j++)
204 fwrite (&pixelp824[i][j], bytepp, 1, fp);
205 fwrite (&tmp, sizepad, 1, fp);
206 }
207 fclose (fp);
208 }
209
210 //***********************************************************
211 char getbit (int h, int w, char **pixelp, char bitcount)
212 {
213 int t = 8 / bitcount;
214 int vtmax = t - 1;
215 int tw = w / t;
216 int vt = vtmax - (w % t);
217 int tmp;
218 if (bitcount == 1)
219 tmp = 0x1;
220 else
221 tmp = 0xf;
222 return ((pixelp[h][tw] >> (vt * bitcount)) & tmp );
223 }
224
225 //***********************************************************
226 void biton (int h, int w, char **pixelp)
227 {
228 int tw = w / 8;
229 int vt = 7 - (w % 8);
230 pixelp[h][tw] = pixelp[h][tw] | (0x1 << vt);
231 }
232
233 //**********************************************************
234 void bitoff(int h, int w, char **pixelp)
235 {
236 int tw = w / 8;
237 int vt = 7 - (w % 8);
238 pixelp[h][tw] = pixelp[h][tw] & (~(0x1 << vt));
239 }
240
241 //*************************************************************
242 void crop14 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char *
palettep, char **pixelp14, int h, int w)
243 {
244 int t = (h - inheader.biHeight);
d:\KY THUAT LAP TRINH\bai tap bitmap\code bitmap\code_bitmap.cpp 5
245 int th = abs(t) / 2;
246 if (h < inheader.biHeight)
247 th = -th;
248
249 t = (w - inheader.biWidth);
250 int tw = abs(t) / 2;
251 if (w < inheader.biWidth)
252 tw = -tw;
253
254 int sh = inheader.biHeight;
255 int sw = inheader.biWidth;
256
257 inheader.biHeight = h;
258 inheader.biWidth = w;
259
260 int sizepad = (32 - (inheader.biWidth * inheader.biBitCount) % 32) % 32;
261 int bytepw = (inheader.biWidth * inheader.biBitCount + sizepad ) / 8;
262 inheader.biSizeImage = bytepw * inheader.biHeight;
263
264 char **pixelptmp = (char **) malloc (sizeof (char *) * inheader.biHeight);
265 for (int i = 0; i < inheader.biHeight; i++)
266 pixelptmp[i] = (char *) calloc (bytepw, sizeof (char));
267
268 fwrite (&fheader, sizeof (fheader), 1, fp);
269 fwrite (&inheader, sizeof (inheader), 1, fp);
270 fwrite (palettep, fheader.bfOffBits - 54, 1, fp);
271 for (int i = 0; i < inheader.biHeight; i++)
272 {
273 for (int j = 0; j < inheader.biWidth; j++)
274 if (!( i < th || j < tw || i >= (th + sh) || j >= (tw + sw)))
275 {
276 if (inheader.biBitCount == 1)
277 {
278 if (pixelp14[i-th][j-tw] == 0)
279 bitoff (i, j, pixelptmp);
280 else
281 biton (i, j, pixelptmp);
282 }
283 else
284 {
285 int k = 1 - j % 2;
286 pixelptmp[i][j / 2] = pixelptmp[i][j / 2] | (pixelp14[i-th][j-tw] <
< (k * 4));
287 }
288 }
289 }
290 for (int i = 0; i < inheader.biHeight; i++)
291 for (int j = 0; j < bytepw; j++)
292 fwrite (&pixelptmp[i][j],1, 1, fp);
293
294 for (int i = 0; i < inheader.biHeight; i++)
295 free (pixelptmp[i]);
296 free (pixelptmp);
297 fclose (fp);
298 }
299
300 //*******************************************************************
301 void crop824 (FILE *fp, BITMAPFILEHEADER fheader, BITMAPINFOHEADER inheader, char *
palettep, char bytepp, char sizepad, int **pixelp824, int h, int w)
302 {
303 int t = (h - inheader.biHeight);
304 int th = abs(t) / 2;
305 if (h < inheader.biHeight)
306 th = -th;
307
308 t = (w - inheader.biWidth);
d:\KY THUAT LAP TRINH\bai tap bitmap\code bitmap\code_bitmap.cpp 6
309 int tw = abs(t) / 2;
310 if (w < inheader.biWidth)
311 tw = -tw;
312 int sh = inheader.biHeight;
313 int sw = inheader.biWidth;
314 inheader.biHeight = h;
315 inheader.biWidth = w;
316 sizepad = (4 - (inheader.biWidth * bytepp) % 4) % 4;
317 inheader.biSizeImage = (inheader.biWidth * bytepp + sizepad) * inheader.biHeight;
318 fheader.bfSize = inheader.biSizeImage + fheader.bfOffBits;
319 int black = 0x00000000;
320 fwrite (&fheader, sizeof (fheader), 1, fp);
321 fwrite (&inheader, sizeof (inheader), 1, fp);
322 fwrite (palettep, fheader.bfOffBits - 54, 1, fp);
323 for (int i = 0; i < inheader.biHeight; i++)
324 {
325 for (int j = 0; j < inheader.biWidth; j++)
326 if ( i < th || j < tw || i >= (th + sh) || j >= (tw + sw) )
327 fwrite (&black, bytepp, 1, fp);
328 else
329 fwrite (&pixelp824[i-th][j-tw], bytepp, 1, fp);
330 fwrite (&black, sizepad, 1, fp);
331 }
332 fclose (fp);
333 }
334
335
336