Debian Codegraph @

src/platform/win32/agg_win32_bmp.cpp

raw
1 //----------------------------------------------------------------------------
2 //
3 //----------------------------------------------------------------------------
4 // Contact: [email protected]
5 //----------------------------------------------------------------------------
6 //
7 // class pixel_map
8 //
9 //----------------------------------------------------------------------------
10
11 #include <cstring>
12 #include <cstdio>
13 #include "platform/win32/agg_win32_bmp.h"
14 #include "agg_basics.h"
15
16 namespace agg
17 {
18
19 //------------------------------------------------------------------------
20 pixel_map::~pixel_map()
21 {
22 destroy();
23 }
24
25
26 //------------------------------------------------------------------------
27 pixel_map::pixel_map() :
28 m_bmp(0),
29 m_buf(0),
30 m_bpp(0),
31 m_is_internal(false),
32 m_img_size(0),
33 m_full_size(0)
34
35 {
36 }
37
38
39 //------------------------------------------------------------------------
40 void pixel_map::destroy()
41 {
42 if(m_bmp && m_is_internal) delete [] (unsigned char*)m_bmp;
43 m_bmp = 0;
44 m_is_internal = false;
45 m_buf = 0;
46 }
47
48
49 //------------------------------------------------------------------------
50 void pixel_map::create(unsigned width,
51 unsigned height,
52 org_e org,
53 unsigned clear_val)
54 {
55 destroy();
56 if(width == 0) width = 1;
57 if(height == 0) height = 1;
58 m_bpp = org;
59 create_from_bmp(create_bitmap_info(width, height, m_bpp));
60 create_gray_scale_palette(m_bmp);
61 m_is_internal = true;
62 if(clear_val <= 255)
63 {
64 std::memset(m_buf, clear_val, m_img_size);
65 }
66 }
67
68
69 //------------------------------------------------------------------------
70 HBITMAP pixel_map::create_dib_section(HDC h_dc,
71 unsigned width,
72 unsigned height,
73 org_e org,
74 unsigned clear_val)
75 {
76 destroy();
77 if(width == 0) width = 1;
78 if(height == 0) height = 1;
79 m_bpp = org;
80 HBITMAP h_bitmap = create_dib_section_from_args(h_dc, width, height, m_bpp);
81 create_gray_scale_palette(m_bmp);
82 m_is_internal = true;
83 if(clear_val <= 255)
84 {
85 std::memset(m_buf, clear_val, m_img_size);
86 }
87 return h_bitmap;
88 }
89
90
91
92 //------------------------------------------------------------------------
93 void pixel_map::clear(unsigned clear_val)
94 {
95 if(m_buf) std::memset(m_buf, clear_val, m_img_size);
96 }
97
98
99 //------------------------------------------------------------------------
100 void pixel_map::attach_to_bmp(BITMAPINFO *bmp)
101 {
102 if(bmp)
103 {
104 destroy();
105 create_from_bmp(bmp);
106 m_is_internal = false;
107 }
108 }
109
110
111
112 //static
113 //------------------------------------------------------------------------
114 unsigned pixel_map::calc_full_size(BITMAPINFO *bmp)
115 {
116 if(bmp == 0) return 0;
117
118 return sizeof(BITMAPINFOHEADER) +
119 sizeof(RGBQUAD) * calc_palette_size(bmp) +
120 bmp->bmiHeader.biSizeImage;
121 }
122
123 //static
124 //------------------------------------------------------------------------
125 unsigned pixel_map::calc_header_size(BITMAPINFO *bmp)
126 {
127 if(bmp == 0) return 0;
128 return sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * calc_palette_size(bmp);
129 }
130
131
132 //static
133 //------------------------------------------------------------------------
134 unsigned pixel_map::calc_palette_size(unsigned clr_used, unsigned bits_per_pixel)
135 {
136 int palette_size = 0;
137
138 if(bits_per_pixel <= 8)
139 {
140 palette_size = clr_used;
141 if(palette_size == 0)
142 {
143 palette_size = 1 << bits_per_pixel;
144 }
145 }
146 return palette_size;
147 }
148
149 //static
150 //------------------------------------------------------------------------
151 unsigned pixel_map::calc_palette_size(BITMAPINFO *bmp)
152 {
153 if(bmp == 0) return 0;
154 return calc_palette_size(bmp->bmiHeader.biClrUsed, bmp->bmiHeader.biBitCount);
155 }
156
157
158 //static
159 //------------------------------------------------------------------------
160 unsigned char * pixel_map::calc_img_ptr(BITMAPINFO *bmp)
161 {
162 if(bmp == 0) return 0;
163 return ((unsigned char*)bmp) + calc_header_size(bmp);
164 }
165
166 //static
167 //------------------------------------------------------------------------
168 BITMAPINFO* pixel_map::create_bitmap_info(unsigned width,
169 unsigned height,
170 unsigned bits_per_pixel)
171 {
172 unsigned line_len = calc_row_len(width, bits_per_pixel);
173 unsigned img_size = line_len * height;
174 unsigned rgb_size = calc_palette_size(0, bits_per_pixel) * sizeof(RGBQUAD);
175 unsigned full_size = sizeof(BITMAPINFOHEADER) + rgb_size + img_size;
176
177 BITMAPINFO *bmp = (BITMAPINFO *) new unsigned char[full_size];
178
179 bmp->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
180 bmp->bmiHeader.biWidth = width;
181 bmp->bmiHeader.biHeight = height;
182 bmp->bmiHeader.biPlanes = 1;
183 bmp->bmiHeader.biBitCount = (unsigned short)bits_per_pixel;
184 bmp->bmiHeader.biCompression = 0;
185 bmp->bmiHeader.biSizeImage = img_size;
186 bmp->bmiHeader.biXPelsPerMeter = 0;
187 bmp->bmiHeader.biYPelsPerMeter = 0;
188 bmp->bmiHeader.biClrUsed = 0;
189 bmp->bmiHeader.biClrImportant = 0;
190
191 return bmp;
192 }
193
194
195 //static
196 //------------------------------------------------------------------------
197 void pixel_map::create_gray_scale_palette(BITMAPINFO *bmp)
198 {
199 if(bmp == 0) return;
200
201 unsigned rgb_size = calc_palette_size(bmp);
202 RGBQUAD *rgb = (RGBQUAD*)(((unsigned char*)bmp) + sizeof(BITMAPINFOHEADER));
203 unsigned brightness;
204 unsigned i;
205
206 for(i = 0; i < rgb_size; i++)
207 {
208 brightness = (255 * i) / (rgb_size - 1);
209 rgb->rgbBlue =
210 rgb->rgbGreen =
211 rgb->rgbRed = (unsigned char)brightness;
212 rgb->rgbReserved = 0;
213 rgb++;
214 }
215 }
216
217
218
219 //static
220 //------------------------------------------------------------------------
221 unsigned pixel_map::calc_row_len(unsigned width, unsigned bits_per_pixel)
222 {
223 unsigned n = width;
224 unsigned k;
225
226 switch(bits_per_pixel)
227 {
228 case 1: k = n;
229 n = n >> 3;
230 if(k & 7) n++;
231 break;
232
233 case 4: k = n;
234 n = n >> 1;
235 if(k & 3) n++;
236 break;
237
238 case 8:
239 break;
240
241 case 16: n *= 2;
242 break;
243
244 case 24: n *= 3;
245 break;
246
247 case 32: n *= 4;
248 break;
249
250 case 48: n *= 6;
251 break;
252
253 case 64: n *= 8;
254 break;
255
256 case 96: n *= 12;
257 break;
258
259 case 128: n *= 16;
260 break;
261
262 default: n = 0;
263 break;
264 }
265 return ((n + 3) >> 2) << 2;
266 }
267
268
269
270
271
272 //------------------------------------------------------------------------
273 void pixel_map::draw(HDC h_dc, const RECT *device_rect, const RECT *bmp_rect) const
274 {
275 if(m_bmp == 0 || m_buf == 0) return;
276
277 unsigned bmp_x = 0;
278 unsigned bmp_y = 0;
279 unsigned bmp_width = m_bmp->bmiHeader.biWidth;
280 unsigned bmp_height = m_bmp->bmiHeader.biHeight;
281 unsigned dvc_x = 0;
282 unsigned dvc_y = 0;
283 unsigned dvc_width = m_bmp->bmiHeader.biWidth;
284 unsigned dvc_height = m_bmp->bmiHeader.biHeight;
285
286 if(bmp_rect)
287 {
288 bmp_x = bmp_rect->left;
289 bmp_y = bmp_rect->top;
290 bmp_width = bmp_rect->right - bmp_rect->left;
291 bmp_height = bmp_rect->bottom - bmp_rect->top;
292 }
293
294 dvc_x = bmp_x;
295 dvc_y = bmp_y;
296 dvc_width = bmp_width;
297 dvc_height = bmp_height;
298
299 if(device_rect)
300 {
301 dvc_x = device_rect->left;
302 dvc_y = device_rect->top;
303 dvc_width = device_rect->right - device_rect->left;
304 dvc_height = device_rect->bottom - device_rect->top;
305 }
306
307 if(dvc_width != bmp_width || dvc_height != bmp_height)
308 {
309 ::SetStretchBltMode(h_dc, COLORONCOLOR);
310 ::StretchDIBits(
311 h_dc, // handle of device context
312 dvc_x, // x-coordinate of upper-left corner of source rect.
313 dvc_y, // y-coordinate of upper-left corner of source rect.
314 dvc_width, // width of source rectangle
315 dvc_height, // height of source rectangle
316 bmp_x,
317 bmp_y, // x, y -coordinates of upper-left corner of dest. rect.
318 bmp_width, // width of destination rectangle
319 bmp_height, // height of destination rectangle
320 m_buf, // address of bitmap bits
321 m_bmp, // address of bitmap data
322 DIB_RGB_COLORS, // usage
323 SRCCOPY // raster operation code
324 );
325 }
326 else
327 {
328 ::SetDIBitsToDevice(
329 h_dc, // handle to device context
330 dvc_x, // x-coordinate of upper-left corner of
331 dvc_y, // y-coordinate of upper-left corner of
332 dvc_width, // source rectangle width
333 dvc_height, // source rectangle height
334 bmp_x, // x-coordinate of lower-left corner of
335 bmp_y, // y-coordinate of lower-left corner of
336 0, // first scan line in array
337 bmp_height, // number of scan lines
338 m_buf, // address of array with DIB bits
339 m_bmp, // address of structure with bitmap info.
340 DIB_RGB_COLORS // RGB or palette indexes
341 );
342 }
343 }
344
345
346 //------------------------------------------------------------------------
347 void pixel_map::draw(HDC h_dc, int x, int y, double scale) const
348 {
349 if(m_bmp == 0 || m_buf == 0) return;
350
351 unsigned width = unsigned(m_bmp->bmiHeader.biWidth * scale);
352 unsigned height = unsigned(m_bmp->bmiHeader.biHeight * scale);
353 RECT rect;
354 rect.left = x;
355 rect.top = y;
356 rect.right = x + width;
357 rect.bottom = y + height;
358 draw(h_dc, &rect);
359 }
360
361
362
363
364 //------------------------------------------------------------------------
365 void pixel_map::blend(HDC h_dc, const RECT *device_rect, const RECT *bmp_rect) const
366 {
367 #if !defined(AGG_BMP_ALPHA_BLEND)
368 draw(h_dc, device_rect, bmp_rect);
369 return;
370 #else
371 if(m_bpp != 32)
372 {
373 draw(h_dc, device_rect, bmp_rect);
374 return;
375 }
376
377 if(m_bmp == 0 || m_buf == 0) return;
378
379 unsigned bmp_x = 0;
380 unsigned bmp_y = 0;
381 unsigned bmp_width = m_bmp->bmiHeader.biWidth;
382 unsigned bmp_height = m_bmp->bmiHeader.biHeight;
383 unsigned dvc_x = 0;
384 unsigned dvc_y = 0;
385 unsigned dvc_width = m_bmp->bmiHeader.biWidth;
386 unsigned dvc_height = m_bmp->bmiHeader.biHeight;
387
388 if(bmp_rect)
389 {
390 bmp_x = bmp_rect->left;
391 bmp_y = bmp_rect->top;
392 bmp_width = bmp_rect->right - bmp_rect->left;
393 bmp_height = bmp_rect->bottom - bmp_rect->top;
394 }
395
396 dvc_x = bmp_x;
397 dvc_y = bmp_y;
398 dvc_width = bmp_width;
399 dvc_height = bmp_height;
400
401 if(device_rect)
402 {
403 dvc_x = device_rect->left;
404 dvc_y = device_rect->top;
405 dvc_width = device_rect->right - device_rect->left;
406 dvc_height = device_rect->bottom - device_rect->top;
407 }
408
409 HDC mem_dc = ::CreateCompatibleDC(h_dc);
410 void* buf = 0;
411 HBITMAP bmp = ::CreateDIBSection(
412 mem_dc,
413 m_bmp,
414 DIB_RGB_COLORS,
415 &buf,
416 0,
417 0
418 );
419 memcpy(buf, m_buf, m_bmp->bmiHeader.biSizeImage);
420
421 HBITMAP temp = (HBITMAP)::SelectObject(mem_dc, bmp);
422
423 BLENDFUNCTION blend;
424 blend.BlendOp = AC_SRC_OVER;
425 blend.BlendFlags = 0;
426
427 #if defined(AC_SRC_ALPHA)
428 blend.AlphaFormat = AC_SRC_ALPHA;
429 //#elif defined(AC_SRC_NO_PREMULT_ALPHA)
430 // blend.AlphaFormat = AC_SRC_NO_PREMULT_ALPHA;
431 #else
432 #error "No appropriate constant for alpha format. Check version of wingdi.h, There must be AC_SRC_ALPHA or AC_SRC_NO_PREMULT_ALPHA"
433 #endif
434
435 blend.SourceConstantAlpha = 255;
436 ::AlphaBlend(
437 h_dc,
438 dvc_x,
439 dvc_y,
440 dvc_width,
441 dvc_height,
442 mem_dc,
443 bmp_x,
444 bmp_y,
445 bmp_width,
446 bmp_height,
447 blend
448 );
449
450 ::SelectObject(mem_dc, temp);
451 ::DeleteObject(bmp);
452 ::DeleteObject(mem_dc);
453 #endif //defined(AGG_BMP_ALPHA_BLEND)
454 }
455
456
457 //------------------------------------------------------------------------
458 void pixel_map::blend(HDC h_dc, int x, int y, double scale) const
459 {
460 if(m_bmp == 0 || m_buf == 0) return;
461 unsigned width = unsigned(m_bmp->bmiHeader.biWidth * scale);
462 unsigned height = unsigned(m_bmp->bmiHeader.biHeight * scale);
463 RECT rect;
464 rect.left = x;
465 rect.top = y;
466 rect.right = x + width;
467 rect.bottom = y + height;
468 blend(h_dc, &rect);
469 }
470
471
472 //------------------------------------------------------------------------
473 bool pixel_map::load_from_bmp(FILE *fd)
474 {
475 BITMAPFILEHEADER bmf;
476 BITMAPINFO *bmi = 0;
477 unsigned bmp_size;
478
479 if (std::fread(&bmf, sizeof(bmf), 1, fd) != 1) goto bmperr;
480 if(bmf.bfType != 0x4D42) goto bmperr;
481
482 bmp_size = bmf.bfSize - sizeof(BITMAPFILEHEADER);
483
484 bmi = (BITMAPINFO*) new unsigned char [bmp_size];
485 if(std::fread(bmi, 1, bmp_size, fd) != bmp_size) goto bmperr;
486 destroy();
487 m_bpp = bmi->bmiHeader.biBitCount;
488 create_from_bmp(bmi);
489 m_is_internal = 1;
490 return true;
491
492 bmperr:
493 if(bmi) delete [] (unsigned char*) bmi;
494 return false;
495 }
496
497
498
499 //------------------------------------------------------------------------
500 bool pixel_map::load_from_bmp(const char *filename)
501 {
502 FILE *fd = std::fopen(filename, "rb");
503 bool ret = false;
504 if(fd)
505 {
506 ret = load_from_bmp(fd);
507 std::fclose(fd);
508 }
509 return ret;
510 }
511
512
513
514 //------------------------------------------------------------------------
515 bool pixel_map::save_as_bmp(FILE *fd) const
516 {
517 if(m_bmp == 0) return 0;
518
519 BITMAPFILEHEADER bmf;
520
521 bmf.bfType = 0x4D42;
522 bmf.bfOffBits = calc_header_size(m_bmp) + sizeof(bmf);
523 bmf.bfSize = bmf.bfOffBits + m_img_size;
524 bmf.bfReserved1 = 0;
525 bmf.bfReserved2 = 0;
526
527 std::fwrite(&bmf, sizeof(bmf), 1, fd);
528 std::fwrite(m_bmp, m_full_size, 1, fd);
529 return true;
530 }
531
532
533
534 //------------------------------------------------------------------------
535 bool pixel_map::save_as_bmp(const char *filename) const
536 {
537 FILE *fd = std::fopen(filename, "wb");
538 bool ret = false;
539 if(fd)
540 {
541 ret = save_as_bmp(fd);
542 std::fclose(fd);
543 }
544 return ret;
545 }
546
547
548 //------------------------------------------------------------------------
549 unsigned char* pixel_map::buf()
550 {
551 return m_buf;
552 }
553
554 //------------------------------------------------------------------------
555 unsigned pixel_map::width() const
556 {
557 return m_bmp->bmiHeader.biWidth;
558 }
559
560 //------------------------------------------------------------------------
561 unsigned pixel_map::height() const
562 {
563 return m_bmp->bmiHeader.biHeight;
564 }
565
566 //------------------------------------------------------------------------
567 int pixel_map::stride() const
568 {
569 return calc_row_len(m_bmp->bmiHeader.biWidth,
570 m_bmp->bmiHeader.biBitCount);
571 }
572
573
574 //private
575 //------------------------------------------------------------------------
576 void pixel_map::create_from_bmp(BITMAPINFO *bmp)
577 {
578 if(bmp)
579 {
580 m_img_size = calc_row_len(bmp->bmiHeader.biWidth,
581 bmp->bmiHeader.biBitCount) *
582 bmp->bmiHeader.biHeight;
583
584 m_full_size = calc_full_size(bmp);
585 m_bmp = bmp;
586 m_buf = calc_img_ptr(bmp);
587 }
588 }
589
590
591 //private
592 //------------------------------------------------------------------------
593 HBITMAP pixel_map::create_dib_section_from_args(HDC h_dc,
594 unsigned width,
595 unsigned height,
596 unsigned bits_per_pixel)
597 {
598 unsigned line_len = calc_row_len(width, bits_per_pixel);
599 unsigned img_size = line_len * height;
600 unsigned rgb_size = calc_palette_size(0, bits_per_pixel) * sizeof(RGBQUAD);
601 unsigned full_size = sizeof(BITMAPINFOHEADER) + rgb_size;
602
603 BITMAPINFO *bmp = (BITMAPINFO *) new unsigned char[full_size];
604
605 bmp->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
606 bmp->bmiHeader.biWidth = width;
607 bmp->bmiHeader.biHeight = height;
608 bmp->bmiHeader.biPlanes = 1;
609 bmp->bmiHeader.biBitCount = (unsigned short)bits_per_pixel;
610 bmp->bmiHeader.biCompression = 0;
611 bmp->bmiHeader.biSizeImage = img_size;
612 bmp->bmiHeader.biXPelsPerMeter = 0;
613 bmp->bmiHeader.biYPelsPerMeter = 0;
614 bmp->bmiHeader.biClrUsed = 0;
615 bmp->bmiHeader.biClrImportant = 0;
616
617 void* img_ptr = 0;
618 HBITMAP h_bitmap = ::CreateDIBSection(h_dc, bmp, DIB_RGB_COLORS, &img_ptr, NULL, 0);
619
620 if(img_ptr)
621 {
622 m_img_size = calc_row_len(width, bits_per_pixel) * height;
623 m_full_size = 0;
624 m_bmp = bmp;
625 m_buf = (unsigned char *) img_ptr;
626 }
627
628 return h_bitmap;
629 }
630 }
631
632
633