Dib.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * imaging display object for Windows
  6. *
  7. * history:
  8. * 1996-05-12 fl Created
  9. * 1996-05-17 fl Up and running
  10. * 1996-05-21 fl Added palette stuff
  11. * 1996-05-26 fl Added query palette and mode inquery
  12. * 1997-09-21 fl Added draw primitive
  13. * 1998-01-20 fl Use StretchDIBits instead of StretchBlt
  14. * 1998-12-30 fl Plugged a resource leak in DeleteDIB (from Roger Burnham)
  15. *
  16. * Copyright (c) Secret Labs AB 1997-2001.
  17. * Copyright (c) Fredrik Lundh 1996.
  18. *
  19. * See the README file for information on usage and redistribution.
  20. */
  21. #include "Imaging.h"
  22. #ifdef _WIN32
  23. #include "ImDib.h"
  24. char *
  25. ImagingGetModeDIB(int size_out[2]) {
  26. /* Get device characteristics */
  27. HDC dc;
  28. char *mode;
  29. dc = CreateCompatibleDC(NULL);
  30. mode = "P";
  31. if (!(GetDeviceCaps(dc, RASTERCAPS) & RC_PALETTE)) {
  32. mode = "RGB";
  33. if (GetDeviceCaps(dc, BITSPIXEL) == 1) {
  34. mode = "1";
  35. }
  36. }
  37. if (size_out) {
  38. size_out[0] = GetDeviceCaps(dc, HORZRES);
  39. size_out[1] = GetDeviceCaps(dc, VERTRES);
  40. }
  41. DeleteDC(dc);
  42. return mode;
  43. }
  44. ImagingDIB
  45. ImagingNewDIB(const char *mode, int xsize, int ysize) {
  46. /* Create a Windows bitmap */
  47. ImagingDIB dib;
  48. RGBQUAD *palette;
  49. int i;
  50. /* Check mode */
  51. if (strcmp(mode, "1") != 0 && strcmp(mode, "L") != 0 && strcmp(mode, "RGB") != 0) {
  52. return (ImagingDIB)ImagingError_ModeError();
  53. }
  54. /* Create DIB context and info header */
  55. /* malloc check ok, small constant allocation */
  56. dib = (ImagingDIB)malloc(sizeof(*dib));
  57. if (!dib) {
  58. return (ImagingDIB)ImagingError_MemoryError();
  59. }
  60. /* malloc check ok, small constant allocation */
  61. dib->info = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
  62. if (!dib->info) {
  63. free(dib);
  64. return (ImagingDIB)ImagingError_MemoryError();
  65. }
  66. memset(dib->info, 0, sizeof(BITMAPINFOHEADER));
  67. dib->info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  68. dib->info->bmiHeader.biWidth = xsize;
  69. dib->info->bmiHeader.biHeight = ysize;
  70. dib->info->bmiHeader.biPlanes = 1;
  71. dib->info->bmiHeader.biBitCount = strlen(mode) * 8;
  72. dib->info->bmiHeader.biCompression = BI_RGB;
  73. /* Create DIB */
  74. dib->dc = CreateCompatibleDC(NULL);
  75. if (!dib->dc) {
  76. free(dib->info);
  77. free(dib);
  78. return (ImagingDIB)ImagingError_MemoryError();
  79. }
  80. dib->bitmap =
  81. CreateDIBSection(dib->dc, dib->info, DIB_RGB_COLORS, &dib->bits, NULL, 0);
  82. if (!dib->bitmap) {
  83. free(dib->info);
  84. free(dib);
  85. return (ImagingDIB)ImagingError_MemoryError();
  86. }
  87. strcpy(dib->mode, mode);
  88. dib->xsize = xsize;
  89. dib->ysize = ysize;
  90. dib->pixelsize = strlen(mode);
  91. dib->linesize = (xsize * dib->pixelsize + 3) & -4;
  92. if (dib->pixelsize == 1) {
  93. dib->pack = dib->unpack = (ImagingShuffler)memcpy;
  94. } else {
  95. dib->pack = ImagingPackBGR;
  96. dib->unpack = ImagingPackBGR;
  97. }
  98. /* Bind the DIB to the device context */
  99. dib->old_bitmap = SelectObject(dib->dc, dib->bitmap);
  100. palette = dib->info->bmiColors;
  101. /* Bind a palette to it as well (only required for 8-bit DIBs) */
  102. if (dib->pixelsize == 1) {
  103. for (i = 0; i < 256; i++) {
  104. palette[i].rgbRed = palette[i].rgbGreen = palette[i].rgbBlue = i;
  105. palette[i].rgbReserved = 0;
  106. }
  107. SetDIBColorTable(dib->dc, 0, 256, palette);
  108. }
  109. /* Create an associated palette (for 8-bit displays only) */
  110. if (strcmp(ImagingGetModeDIB(NULL), "P") == 0) {
  111. char palbuf[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
  112. LPLOGPALETTE pal = (LPLOGPALETTE)palbuf;
  113. int i, r, g, b;
  114. /* Load system palette */
  115. pal->palVersion = 0x300;
  116. pal->palNumEntries = 256;
  117. GetSystemPaletteEntries(dib->dc, 0, 256, pal->palPalEntry);
  118. if (strcmp(mode, "L") == 0) {
  119. /* Grayscale DIB. Fill all 236 slots with a grayscale ramp
  120. * (this is usually overkill on Windows since VGA only offers
  121. * 6 bits grayscale resolution). Ignore the slots already
  122. * allocated by Windows */
  123. i = 10;
  124. for (r = 0; r < 236; r++) {
  125. pal->palPalEntry[i].peRed = pal->palPalEntry[i].peGreen =
  126. pal->palPalEntry[i].peBlue = i;
  127. i++;
  128. }
  129. dib->palette = CreatePalette(pal);
  130. } else if (strcmp(mode, "RGB") == 0) {
  131. #ifdef CUBE216
  132. /* Colour DIB. Create a 6x6x6 colour cube (216 entries) and
  133. * add 20 extra graylevels for best result with grayscale
  134. * images. */
  135. i = 10;
  136. for (r = 0; r < 256; r += 51) {
  137. for (g = 0; g < 256; g += 51) {
  138. for (b = 0; b < 256; b += 51) {
  139. pal->palPalEntry[i].peRed = r;
  140. pal->palPalEntry[i].peGreen = g;
  141. pal->palPalEntry[i].peBlue = b;
  142. i++;
  143. }
  144. }
  145. }
  146. for (r = 1; r < 22 - 1; r++) {
  147. /* Black and white are already provided by the cube. */
  148. pal->palPalEntry[i].peRed = pal->palPalEntry[i].peGreen =
  149. pal->palPalEntry[i].peBlue = r * 255 / (22 - 1);
  150. i++;
  151. }
  152. #else
  153. /* Colour DIB. Alternate palette. */
  154. i = 10;
  155. for (r = 0; r < 256; r += 37) {
  156. for (g = 0; g < 256; g += 32) {
  157. for (b = 0; b < 256; b += 64) {
  158. pal->palPalEntry[i].peRed = r;
  159. pal->palPalEntry[i].peGreen = g;
  160. pal->palPalEntry[i].peBlue = b;
  161. i++;
  162. }
  163. }
  164. }
  165. #endif
  166. dib->palette = CreatePalette(pal);
  167. }
  168. }
  169. return dib;
  170. }
  171. void
  172. ImagingPasteDIB(ImagingDIB dib, Imaging im, int xy[4]) {
  173. /* Paste image data into a bitmap */
  174. /* FIXME: check size! */
  175. int y;
  176. for (y = 0; y < im->ysize; y++) {
  177. dib->pack(
  178. dib->bits + dib->linesize * (dib->ysize - (xy[1] + y) - 1) +
  179. xy[0] * dib->pixelsize,
  180. im->image[y],
  181. im->xsize);
  182. }
  183. }
  184. void
  185. ImagingExposeDIB(ImagingDIB dib, void *dc) {
  186. /* Copy bitmap to display */
  187. if (dib->palette != 0) {
  188. SelectPalette((HDC)dc, dib->palette, FALSE);
  189. }
  190. BitBlt((HDC)dc, 0, 0, dib->xsize, dib->ysize, dib->dc, 0, 0, SRCCOPY);
  191. }
  192. void
  193. ImagingDrawDIB(ImagingDIB dib, void *dc, int dst[4], int src[4]) {
  194. /* Copy bitmap to printer/display */
  195. if (GetDeviceCaps((HDC)dc, RASTERCAPS) & RC_STRETCHDIB) {
  196. /* stretchdib (printers) */
  197. StretchDIBits(
  198. (HDC)dc,
  199. dst[0],
  200. dst[1],
  201. dst[2] - dst[0],
  202. dst[3] - dst[1],
  203. src[0],
  204. src[1],
  205. src[2] - src[0],
  206. src[3] - src[1],
  207. dib->bits,
  208. dib->info,
  209. DIB_RGB_COLORS,
  210. SRCCOPY);
  211. } else {
  212. /* stretchblt (displays) */
  213. if (dib->palette != 0) {
  214. SelectPalette((HDC)dc, dib->palette, FALSE);
  215. }
  216. StretchBlt(
  217. (HDC)dc,
  218. dst[0],
  219. dst[1],
  220. dst[2] - dst[0],
  221. dst[3] - dst[1],
  222. dib->dc,
  223. src[0],
  224. src[1],
  225. src[2] - src[0],
  226. src[3] - src[1],
  227. SRCCOPY);
  228. }
  229. }
  230. int
  231. ImagingQueryPaletteDIB(ImagingDIB dib, void *dc) {
  232. /* Install bitmap palette */
  233. int n;
  234. if (dib->palette != 0) {
  235. /* Realize associated palette */
  236. HPALETTE now = SelectPalette((HDC)dc, dib->palette, FALSE);
  237. n = RealizePalette((HDC)dc);
  238. /* Restore palette */
  239. SelectPalette((HDC)dc, now, FALSE);
  240. } else {
  241. n = 0;
  242. }
  243. return n; /* number of colours that was changed */
  244. }
  245. void
  246. ImagingDeleteDIB(ImagingDIB dib) {
  247. /* Clean up */
  248. if (dib->palette) {
  249. DeleteObject(dib->palette);
  250. }
  251. if (dib->bitmap) {
  252. SelectObject(dib->dc, dib->old_bitmap);
  253. DeleteObject(dib->bitmap);
  254. }
  255. if (dib->dc) {
  256. DeleteDC(dib->dc);
  257. }
  258. free(dib->info);
  259. }
  260. #endif /* _WIN32 */