gdevmswn.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /* Copyright (C) 1989, 1995, 1996, 1998, 1999, 2000, 2001 Aladdin Enterprises. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gdevmswn.c,v 1.12 2004/09/20 22:14:59 dan Exp $ */
  14. /*
  15. * Microsoft Windows 3.n driver for Ghostscript.
  16. *
  17. * Original version by Russell Lang and Maurice Castro with help from
  18. * Programming Windows, 2nd Ed., Charles Petzold, Microsoft Press;
  19. * created from gdevbgi.c and gnuplot/term/win.trm 5th June 1992.
  20. * Extensively modified by L. Peter Deutsch, Aladdin Enterprises.
  21. */
  22. #include "gdevmswn.h"
  23. #include "gp.h"
  24. #include "gpcheck.h"
  25. #include "gsparam.h"
  26. #include "gdevpccm.h"
  27. #include "gsdll.h"
  28. /* Forward references */
  29. private int win_set_bits_per_pixel(gx_device_win *, int);
  30. #define TIMER_ID 1
  31. /* Open the win driver */
  32. int
  33. win_open(gx_device * dev)
  34. {
  35. HDC hdc;
  36. int code;
  37. if (dev->width == INITIAL_WIDTH)
  38. dev->width = (int)(8.5 * dev->x_pixels_per_inch);
  39. if (dev->height == INITIAL_HEIGHT)
  40. dev->height = (int)(11.0 * dev->y_pixels_per_inch);
  41. if (wdev->BitsPerPixel == 0) {
  42. int depth;
  43. /* Set parameters that were unknown before opening device */
  44. /* Find out if the device supports color */
  45. /* We recognize 1, 4, 8, 16, 24 bit/pixel devices */
  46. hdc = GetDC(NULL); /* get hdc for desktop */
  47. depth = GetDeviceCaps(hdc, PLANES) * GetDeviceCaps(hdc, BITSPIXEL);
  48. if (depth > 16) {
  49. wdev->BitsPerPixel = 24;
  50. } else if (depth > 8) {
  51. wdev->BitsPerPixel = 16;
  52. } else if (depth >= 8) {
  53. wdev->BitsPerPixel = 8;
  54. } else if (depth >= 4) {
  55. wdev->BitsPerPixel = 4;
  56. } else {
  57. wdev->BitsPerPixel = 1;
  58. }
  59. ReleaseDC(NULL, hdc);
  60. wdev->mapped_color_flags = 0;
  61. }
  62. if ((code = win_set_bits_per_pixel(wdev, wdev->BitsPerPixel)) < 0)
  63. return code;
  64. if (wdev->nColors > 0) {
  65. /* create palette for display */
  66. if ((wdev->limgpalette = win_makepalette(wdev))
  67. == (LPLOGPALETTE) NULL)
  68. return win_nomemory();
  69. wdev->himgpalette = CreatePalette(wdev->limgpalette);
  70. }
  71. return 0;
  72. }
  73. /* Make the output appear on the screen. */
  74. int
  75. win_sync_output(gx_device * dev)
  76. {
  77. if (pgsdll_callback)
  78. (*pgsdll_callback) (GSDLL_SYNC, (unsigned char *)wdev, 0);
  79. return (0);
  80. }
  81. /* Make the window visible, and display the output. */
  82. int
  83. win_output_page(gx_device * dev, int copies, int flush)
  84. {
  85. if (pgsdll_callback)
  86. (*pgsdll_callback) (GSDLL_PAGE, (unsigned char *)wdev, 0);
  87. return gx_finish_output_page(dev, copies, flush);;
  88. }
  89. /* Close the win driver */
  90. int
  91. win_close(gx_device * dev)
  92. {
  93. /* Free resources */
  94. if (wdev->nColors > 0) {
  95. gs_free(dev->memory,
  96. wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
  97. DeleteObject(wdev->himgpalette);
  98. gs_free(dev->memory,
  99. (char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) +
  100. (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  101. "win_close");
  102. }
  103. return (0);
  104. }
  105. /* Map a r-g-b color to the colors available under Windows */
  106. gx_color_index
  107. win_map_rgb_color(gx_device * dev, const gx_color_value cv[])
  108. {
  109. gx_color_value r = cv[0];
  110. gx_color_value g = cv[1];
  111. gx_color_value b = cv[2];
  112. switch (wdev->BitsPerPixel) {
  113. case 24:
  114. return (((unsigned long)b >> (gx_color_value_bits - 8)) << 16) +
  115. (((unsigned long)g >> (gx_color_value_bits - 8)) << 8) +
  116. (((unsigned long)r >> (gx_color_value_bits - 8)));
  117. case 16:{
  118. gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 11) +
  119. ((g >> (gx_color_value_bits - 6)) << 5) +
  120. (b >> (gx_color_value_bits - 5));
  121. #if arch_is_big_endian
  122. ushort color16 = (ushort)color;
  123. #else
  124. ushort color16 = (ushort)((color << 8) | (color >> 8));
  125. #endif
  126. return color16;
  127. }
  128. case 15:{
  129. gx_color_index color = ((r >> (gx_color_value_bits - 5)) << 10) +
  130. ((g >> (gx_color_value_bits - 5)) << 5) +
  131. (b >> (gx_color_value_bits - 5));
  132. #if arch_is_big_endian
  133. ushort color15 = (ushort)color;
  134. #else
  135. ushort color15 = (ushort)((color << 8) | (color >> 8));
  136. #endif
  137. return color15;
  138. }
  139. case 8:{
  140. int i;
  141. LPLOGPALETTE lpal = wdev->limgpalette;
  142. PALETTEENTRY *pep;
  143. byte cr, cg, cb;
  144. int mc_index;
  145. byte mc_mask;
  146. /* Check for a color in the palette of 64. */
  147. {
  148. static const byte pal64[32] =
  149. {
  150. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  151. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  152. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 1
  154. };
  155. if (pal64[r >> (gx_color_value_bits - 5)] &&
  156. pal64[g >> (gx_color_value_bits - 5)] &&
  157. pal64[b >> (gx_color_value_bits - 5)]
  158. )
  159. return (gx_color_index) (
  160. ((r >> (gx_color_value_bits - 2)) << 4) +
  161. ((g >> (gx_color_value_bits - 2)) << 2) +
  162. (b >> (gx_color_value_bits - 2))
  163. );
  164. }
  165. /* map colors to 0->255 in 32 steps */
  166. cr = win_color_value(r);
  167. cg = win_color_value(g);
  168. cb = win_color_value(b);
  169. /* Search in palette, skipping the first 64. */
  170. mc_index = ((cr >> 3) << 7) + ((cg >> 3) << 2) + (cb >> 6);
  171. mc_mask = 0x80 >> ((cb >> 3) & 7);
  172. if (wdev->mapped_color_flags[mc_index] & mc_mask)
  173. for (i = wdev->nColors, pep = &lpal->palPalEntry[i];
  174. --pep, --i >= 64;
  175. ) {
  176. if (cr == pep->peRed &&
  177. cg == pep->peGreen &&
  178. cb == pep->peBlue
  179. )
  180. return ((gx_color_index) i); /* found it */
  181. }
  182. /* next try adding it to palette */
  183. i = wdev->nColors;
  184. if (i < 220) { /* allow 36 for windows and other apps */
  185. LPLOGPALETTE lipal = wdev->limgpalette;
  186. wdev->nColors = i + 1;
  187. DeleteObject(wdev->himgpalette);
  188. lipal->palPalEntry[i].peFlags = 0;
  189. lipal->palPalEntry[i].peRed = cr;
  190. lipal->palPalEntry[i].peGreen = cg;
  191. lipal->palPalEntry[i].peBlue = cb;
  192. lipal->palNumEntries = wdev->nColors;
  193. wdev->himgpalette = CreatePalette(lipal);
  194. wdev->mapped_color_flags[mc_index] |= mc_mask;
  195. return ((gx_color_index) i); /* return new palette index */
  196. }
  197. return (gx_no_color_index); /* not found - dither instead */
  198. }
  199. case 4:
  200. return pc_4bit_map_rgb_color(dev, cv);
  201. }
  202. return (gx_default_map_rgb_color(dev, cv));
  203. }
  204. /* Map a color code to r-g-b. */
  205. int
  206. win_map_color_rgb(gx_device * dev, gx_color_index color,
  207. gx_color_value prgb[3])
  208. {
  209. gx_color_value one;
  210. ushort value;
  211. switch (wdev->BitsPerPixel) {
  212. case 24:
  213. one = (gx_color_value) (gx_max_color_value / 255);
  214. prgb[0] = ((color) & 255) * one;
  215. prgb[1] = ((color >> 8) & 255) * one;
  216. prgb[2] = ((color >> 16) & 255) * one;
  217. break;
  218. case 16:
  219. value = (color >> 11) & 0x1f;
  220. prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  221. value = (color >> 5) & 0x3f;
  222. prgb[1] = ((value << 10) + (value << 4) + (value >> 2)) >> (16 - gx_color_value_bits);
  223. value = (color) & 0x1f;
  224. prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  225. break;
  226. case 15:
  227. value = (color >> 10) & 0x1f;
  228. prgb[0] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  229. value = (color >> 5) & 0x1f;
  230. prgb[1] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  231. value = (color) & 0x1f;
  232. prgb[2] = ((value << 11) + (value << 6) + (value << 1) + (value >> 4)) >> (16 - gx_color_value_bits);
  233. break;
  234. case 8:
  235. if (!dev->is_open)
  236. return -1;
  237. one = (gx_color_value) (gx_max_color_value / 255);
  238. prgb[0] = wdev->limgpalette->palPalEntry[(int)color].peRed * one;
  239. prgb[1] = wdev->limgpalette->palPalEntry[(int)color].peGreen * one;
  240. prgb[2] = wdev->limgpalette->palPalEntry[(int)color].peBlue * one;
  241. break;
  242. case 4:
  243. pc_4bit_map_color_rgb(dev, color, prgb);
  244. break;
  245. default:
  246. prgb[0] = prgb[1] = prgb[2] =
  247. (int)color ? gx_max_color_value : 0;
  248. }
  249. return 0;
  250. }
  251. /* Get Win parameters */
  252. int
  253. win_get_params(gx_device * dev, gs_param_list * plist)
  254. {
  255. int code = gx_default_get_params(dev, plist);
  256. return code;
  257. }
  258. /* Put parameters. */
  259. /* Set window parameters -- size and resolution. */
  260. /* We implement this ourselves so that we can do it without */
  261. /* closing and opening the device. */
  262. int
  263. win_put_params(gx_device * dev, gs_param_list * plist)
  264. {
  265. int ecode = 0, code;
  266. bool is_open = dev->is_open;
  267. int width = dev->width;
  268. int height = dev->height;
  269. int old_bpp = dev->color_info.depth;
  270. int bpp = old_bpp;
  271. byte *old_flags = wdev->mapped_color_flags;
  272. /* Handle extra parameters */
  273. switch (code = param_read_int(plist, "BitsPerPixel", &bpp)) {
  274. case 0:
  275. if (dev->is_open && bpp != old_bpp)
  276. ecode = gs_error_rangecheck;
  277. else { /* Don't release existing mapped_color_flags. */
  278. if (bpp != 8)
  279. wdev->mapped_color_flags = 0;
  280. code = win_set_bits_per_pixel(wdev, bpp);
  281. if (code < 0)
  282. ecode = code;
  283. else
  284. break;
  285. }
  286. goto bppe;
  287. default:
  288. ecode = code;
  289. bppe:param_signal_error(plist, "BitsPerPixel", ecode);
  290. case 1:
  291. break;
  292. }
  293. if (ecode >= 0) { /* Prevent gx_default_put_params from closing the device. */
  294. dev->is_open = false;
  295. ecode = gx_default_put_params(dev, plist);
  296. dev->is_open = is_open;
  297. }
  298. if (ecode < 0) { /* If we allocated mapped_color_flags, release it. */
  299. if (wdev->mapped_color_flags != 0 && old_flags == 0)
  300. gs_free(wdev->memory,
  301. wdev->mapped_color_flags, 4096, 1,
  302. "win_put_params");
  303. wdev->mapped_color_flags = old_flags;
  304. if (bpp != old_bpp)
  305. win_set_bits_per_pixel(wdev, old_bpp);
  306. return ecode;
  307. }
  308. if (wdev->mapped_color_flags == 0 && old_flags != 0) { /* Release old mapped_color_flags. */
  309. gs_free(dev->memory,
  310. old_flags, 4096, 1, "win_put_params");
  311. }
  312. /* Hand off the change to the implementation. */
  313. if (is_open && (bpp != old_bpp ||
  314. dev->width != width || dev->height != height)
  315. ) {
  316. int ccode;
  317. (*wdev->free_bitmap) (wdev);
  318. ccode = (*wdev->alloc_bitmap) (wdev, (gx_device *) wdev);
  319. if (ccode < 0) { /* Bad news! Some of the other device parameters */
  320. /* may have changed. We don't handle this. */
  321. /* This is ****** WRONG ******. */
  322. dev->width = width;
  323. dev->height = height;
  324. win_set_bits_per_pixel(wdev, old_bpp);
  325. (*wdev->alloc_bitmap) (wdev, dev);
  326. return ccode;
  327. }
  328. }
  329. return 0;
  330. }
  331. /* ------ Internal routines ------ */
  332. #undef wdev
  333. /* out of memory error message box */
  334. int
  335. win_nomemory(void)
  336. {
  337. MessageBox((HWND) NULL, (LPSTR) "Not enough memory", (LPSTR) szAppName, MB_ICONSTOP);
  338. return gs_error_limitcheck;
  339. }
  340. LPLOGPALETTE
  341. win_makepalette(gx_device_win * wdev)
  342. {
  343. int i, val;
  344. LPLOGPALETTE logpalette;
  345. logpalette = (LPLOGPALETTE) gs_malloc(wdev->memory, 1, sizeof(LOGPALETTE) +
  346. (1 << (wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  347. "win_makepalette");
  348. if (logpalette == (LPLOGPALETTE) NULL)
  349. return (0);
  350. logpalette->palVersion = 0x300;
  351. logpalette->palNumEntries = wdev->nColors;
  352. for (i = 0; i < wdev->nColors; i++) {
  353. logpalette->palPalEntry[i].peFlags = 0;
  354. switch (wdev->nColors) {
  355. case 64:
  356. /* colors are rrggbb */
  357. logpalette->palPalEntry[i].peRed = ((i & 0x30) >> 4) * 85;
  358. logpalette->palPalEntry[i].peGreen = ((i & 0xC) >> 2) * 85;
  359. logpalette->palPalEntry[i].peBlue = (i & 3) * 85;
  360. break;
  361. case 16:
  362. /* colors are irgb */
  363. val = (i & 8 ? 255 : 128);
  364. logpalette->palPalEntry[i].peRed = i & 4 ? val : 0;
  365. logpalette->palPalEntry[i].peGreen = i & 2 ? val : 0;
  366. logpalette->palPalEntry[i].peBlue = i & 1 ? val : 0;
  367. if (i == 8) { /* light gray */
  368. logpalette->palPalEntry[i].peRed =
  369. logpalette->palPalEntry[i].peGreen =
  370. logpalette->palPalEntry[i].peBlue = 192;
  371. }
  372. break;
  373. case 2:
  374. logpalette->palPalEntry[i].peRed =
  375. logpalette->palPalEntry[i].peGreen =
  376. logpalette->palPalEntry[i].peBlue = (i ? 255 : 0);
  377. break;
  378. }
  379. }
  380. return (logpalette);
  381. }
  382. private int
  383. win_set_bits_per_pixel(gx_device_win * wdev, int bpp)
  384. {
  385. static const gx_device_color_info win_24bit_color = dci_color(24, 255, 255);
  386. static const gx_device_color_info win_16bit_color = dci_color(16, 255, 255);
  387. static const gx_device_color_info win_8bit_color = dci_color(8, 31, 4);
  388. static const gx_device_color_info win_ega_color = dci_pc_4bit;
  389. static const gx_device_color_info win_vga_color = dci_pc_4bit;
  390. static const gx_device_color_info win_mono_color = dci_black_and_white;
  391. /* remember old anti_alias info */
  392. gx_device_anti_alias_info anti_alias = wdev->color_info.anti_alias;
  393. HDC hdc;
  394. switch (bpp) {
  395. case 24:
  396. wdev->color_info = win_24bit_color;
  397. wdev->nColors = -1;
  398. break;
  399. case 16:
  400. case 15:
  401. wdev->color_info = win_16bit_color;
  402. wdev->nColors = -1;
  403. break;
  404. case 8:
  405. /* use 64 static colors and 166 dynamic colors from 8 planes */
  406. wdev->color_info = win_8bit_color;
  407. wdev->nColors = 64;
  408. break;
  409. case 4:
  410. hdc = GetDC(NULL);
  411. if (GetDeviceCaps(hdc, VERTRES) <= 350)
  412. wdev->color_info = win_ega_color;
  413. else
  414. wdev->color_info = win_vga_color;
  415. ReleaseDC(NULL, hdc);
  416. wdev->nColors = 16;
  417. break;
  418. case 1:
  419. wdev->color_info = win_mono_color;
  420. wdev->nColors = 2;
  421. break;
  422. default:
  423. return (gs_error_rangecheck);
  424. }
  425. wdev->BitsPerPixel = bpp;
  426. /* If necessary, allocate and clear the mapped color flags. */
  427. if (bpp == 8) {
  428. if (wdev->mapped_color_flags == 0) {
  429. wdev->mapped_color_flags = gs_malloc(wdev->memory,
  430. 4096, 1, "win_set_bits_per_pixel");
  431. if (wdev->mapped_color_flags == 0)
  432. return_error(gs_error_VMerror);
  433. }
  434. memset(wdev->mapped_color_flags, 0, 4096);
  435. } else {
  436. gs_free(wdev->memory,
  437. wdev->mapped_color_flags, 4096, 1, "win_set_bits_per_pixel");
  438. wdev->mapped_color_flags = 0;
  439. }
  440. /* copy encode/decode procedures */
  441. wdev->procs.encode_color = wdev->procs.map_rgb_color;
  442. wdev->procs.decode_color = wdev->procs.map_color_rgb;
  443. if (bpp == 1) {
  444. wdev->procs.get_color_mapping_procs =
  445. gx_default_DevGray_get_color_mapping_procs;
  446. wdev->procs.get_color_comp_index =
  447. gx_default_DevGray_get_color_comp_index;
  448. }
  449. else {
  450. wdev->procs.get_color_mapping_procs =
  451. gx_default_DevRGB_get_color_mapping_procs;
  452. wdev->procs.get_color_comp_index =
  453. gx_default_DevRGB_get_color_comp_index;
  454. }
  455. /* restore old anti_alias info */
  456. wdev->color_info.anti_alias = anti_alias;
  457. return 0;
  458. }