gdevdsp.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. /* Copyright (C) 2001-2005, Ghostgum Software Pty Ltd. 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. /* gdevdsp.c */
  14. /* $Id: gdevdsp.c,v 1.35 2005/09/04 05:44:43 dan Exp $ */
  15. /*
  16. * DLL based display device driver.
  17. *
  18. * by Russell Lang, Ghostgum Software Pty Ltd
  19. *
  20. * This device is intended to be used for displays when
  21. * Ghostscript is loaded as a DLL/shared library/static library.
  22. * It is intended to work for Windows, OS/2, Linux, Mac OS 9 and
  23. * hopefully others.
  24. *
  25. * Before this device is opened, the address of a structure must
  26. * be provided using gsapi_set_display_callback(minst, callback);
  27. * This structure contains callback functions to notify the
  28. * caller when the device is opened, closed, resized, showpage etc.
  29. * The structure is defined in gdevdsp.h.
  30. *
  31. * Not all combinations of display formats have been tested.
  32. * At the end of this file is some example code showing which
  33. * formats have been tested.
  34. */
  35. #include "string_.h"
  36. #include "gx.h"
  37. #include "gserrors.h"
  38. #include "gsdevice.h" /* for gs_copydevice */
  39. #include "gxdevice.h"
  40. #include "gp.h"
  41. #include "gpcheck.h"
  42. #include "gsparam.h"
  43. #include "gdevpccm.h" /* 4-bit PC color */
  44. #include "gxdevmem.h"
  45. #include "gdevdevn.h"
  46. #include "gsequivc.h"
  47. #include "gdevdsp.h"
  48. #include "gdevdsp2.h"
  49. /* Initial values for width and height */
  50. #define INITIAL_RESOLUTION 96
  51. #define INITIAL_WIDTH ((INITIAL_RESOLUTION * 85 + 5) / 10)
  52. #define INITIAL_HEIGHT ((INITIAL_RESOLUTION * 110 + 5) / 10)
  53. /* Device procedures */
  54. /* See gxdevice.h for the definitions of the procedures. */
  55. private dev_proc_open_device(display_open);
  56. private dev_proc_get_initial_matrix(display_get_initial_matrix);
  57. private dev_proc_sync_output(display_sync_output);
  58. private dev_proc_output_page(display_output_page);
  59. private dev_proc_close_device(display_close);
  60. private dev_proc_map_rgb_color(display_map_rgb_color_device4);
  61. private dev_proc_map_color_rgb(display_map_color_rgb_device4);
  62. private dev_proc_encode_color(display_encode_color_device8);
  63. private dev_proc_decode_color(display_decode_color_device8);
  64. private dev_proc_map_rgb_color(display_map_rgb_color_device16);
  65. private dev_proc_map_color_rgb(display_map_color_rgb_device16);
  66. private dev_proc_map_rgb_color(display_map_rgb_color_rgb);
  67. private dev_proc_map_color_rgb(display_map_color_rgb_rgb);
  68. private dev_proc_map_rgb_color(display_map_rgb_color_bgr24);
  69. private dev_proc_map_color_rgb(display_map_color_rgb_bgr24);
  70. private dev_proc_fill_rectangle(display_fill_rectangle);
  71. private dev_proc_copy_mono(display_copy_mono);
  72. private dev_proc_copy_color(display_copy_color);
  73. private dev_proc_get_bits(display_get_bits);
  74. private dev_proc_get_params(display_get_params);
  75. private dev_proc_put_params(display_put_params);
  76. private dev_proc_finish_copydevice(display_finish_copydevice);
  77. private dev_proc_get_color_mapping_procs(display_separation_get_color_mapping_procs);
  78. private dev_proc_get_color_comp_index(display_separation_get_color_comp_index);
  79. private dev_proc_encode_color(display_separation_encode_color);
  80. private dev_proc_decode_color(display_separation_decode_color);
  81. private dev_proc_update_spot_equivalent_colors(display_update_spot_equivalent_colors);
  82. private const gx_device_procs display_procs =
  83. {
  84. display_open,
  85. display_get_initial_matrix,
  86. display_sync_output,
  87. display_output_page,
  88. display_close,
  89. gx_default_w_b_map_rgb_color,
  90. gx_default_w_b_map_color_rgb,
  91. display_fill_rectangle,
  92. NULL, /* tile rectangle */
  93. display_copy_mono,
  94. display_copy_color,
  95. NULL, /* draw line */
  96. display_get_bits,
  97. display_get_params,
  98. display_put_params,
  99. gx_default_cmyk_map_cmyk_color, /* map_cmyk_color */
  100. gx_default_get_xfont_procs,
  101. NULL, /* get_xfont_device */
  102. NULL, /* map_rgb_alpha_color */
  103. gx_page_device_get_page_device,
  104. /* extra entries */
  105. NULL, /* get_alpha_bits */
  106. NULL, /* copy_alpha */
  107. NULL, /* get_band */
  108. NULL, /* copy_rop */
  109. NULL, /* fill_path */
  110. NULL, /* stroke_path */
  111. NULL, /* fill_mask */
  112. NULL, /* fill_trapezoid */
  113. NULL, /* fill_parallelogram */
  114. NULL, /* fill_triangle */
  115. NULL, /* draw_thin_line */
  116. NULL, /* begin_image */
  117. NULL, /* image_data */
  118. NULL, /* end_image */
  119. NULL, /* strip_tile_rectangle */
  120. NULL, /* strip_copy_rop */
  121. NULL, /* get_clipping_box */
  122. NULL, /* begin_typed_image */
  123. NULL, /* get_bits_rectangle */
  124. NULL, /* map_color_rgb_alpha */
  125. NULL, /* create_compositor */
  126. NULL, /* get_hardware_params */
  127. NULL, /* text_begin */
  128. display_finish_copydevice, /* finish_copydevice */
  129. NULL, /* begin_transparency_group */
  130. NULL, /* end_transparency_group */
  131. NULL, /* begin_transparency_mask */
  132. NULL, /* end_transparency_mask */
  133. NULL, /* discard_transparency_layer */
  134. NULL, /* get_color_mapping_procs */
  135. NULL, /* get_color_comp_index */
  136. NULL, /* encode_color */
  137. NULL, /* decode_color */
  138. NULL, /* pattern_manage */
  139. NULL, /* fill_rectangle_hl_color */\
  140. NULL, /* include_color_space */\
  141. NULL, /* fill_linear_color_scanline */\
  142. NULL, /* fill_linear_color_trapezoid */\
  143. NULL, /* fill_linear_color_triangle */\
  144. display_update_spot_equivalent_colors /* update_spot_equivalent_colors */
  145. };
  146. /* GC descriptor */
  147. public_st_device_display();
  148. private
  149. ENUM_PTRS_WITH(display_enum_ptrs, gx_device_display *ddev)
  150. if (index == 0) {
  151. if (ddev->mdev) {
  152. return ENUM_OBJ(gx_device_enum_ptr((gx_device *)ddev->mdev));
  153. }
  154. return 0;
  155. }
  156. else if (index-1 < ddev->devn_params.separations.num_separations)
  157. ENUM_RETURN(ddev->devn_params.separations.names[index-1].data);
  158. else
  159. return 0;
  160. ENUM_PTRS_END
  161. private
  162. RELOC_PTRS_WITH(display_reloc_ptrs, gx_device_display *ddev)
  163. if (ddev->mdev) {
  164. ddev->mdev = (gx_device_memory *)
  165. gx_device_reloc_ptr((gx_device *)ddev->mdev, gcst);
  166. }
  167. { int i;
  168. for (i = 0; i < ddev->devn_params.separations.num_separations; ++i) {
  169. RELOC_PTR(gx_device_display, devn_params.separations.names[i].data);
  170. }
  171. }
  172. RELOC_PTRS_END
  173. const gx_device_display gs_display_device =
  174. {
  175. std_device_std_body_type(gx_device_display, &display_procs, "display",
  176. &st_device_display,
  177. INITIAL_WIDTH, INITIAL_HEIGHT,
  178. INITIAL_RESOLUTION, INITIAL_RESOLUTION),
  179. {0}, /* std_procs */
  180. NULL, /* mdev */
  181. NULL, /* callback */
  182. NULL, /* pHandle */
  183. 0, /* nFormat */
  184. NULL, /* pBitmap */
  185. 0, /* ulBitmapSize */
  186. 0, /* HWResolution_set */
  187. { /* devn_params specific parameters */
  188. 8, /* Bits per color - must match ncomp, depth, etc. */
  189. DeviceCMYKComponents, /* Names of color model colorants */
  190. 4, /* Number of colorants for CMYK */
  191. 0, /* MaxSeparations has not been specified */
  192. {0}, /* SeparationNames */
  193. {0}, /* SeparationOrder names */
  194. {0, 1, 2, 3, 4, 5, 6, 7 } /* Initial component SeparationOrder */
  195. },
  196. { true } /* equivalent CMYK colors for spot colors */
  197. };
  198. /* prototypes for internal procedures */
  199. private int display_check_structure(gx_device_display *dev);
  200. private void display_free_bitmap(gx_device_display * dev);
  201. private int display_alloc_bitmap(gx_device_display *, gx_device *);
  202. private int display_set_color_format(gx_device_display *dev, int nFormat);
  203. private int display_set_separations(gx_device_display *dev);
  204. private int display_raster(gx_device_display *dev);
  205. /* Open the display driver. */
  206. private int
  207. display_open(gx_device * dev)
  208. {
  209. gx_device_display *ddev = (gx_device_display *) dev;
  210. int ccode;
  211. /* Erase these, in case we are opening a copied device. */
  212. ddev->mdev = NULL;
  213. ddev->pBitmap = NULL;
  214. ddev->ulBitmapSize = 0;
  215. /* Allow device to be opened "disabled" without a callback. */
  216. /* The callback will be set later and the device re-opened. */
  217. if (ddev->callback == NULL)
  218. return 0;
  219. /* Make sure we have been passed a valid callback structure. */
  220. if ((ccode = display_check_structure(ddev)) < 0)
  221. return_error(ccode);
  222. /* set color info */
  223. if ((ccode = display_set_color_format(ddev, ddev->nFormat)) < 0)
  224. return_error(ccode);
  225. /* Tell caller that the device is open. */
  226. /* This is always the first callback */
  227. ccode = (*(ddev->callback->display_open))(ddev->pHandle, dev);
  228. if (ccode < 0)
  229. return_error(ccode);
  230. /* Tell caller the proposed device parameters */
  231. ccode = (*(ddev->callback->display_presize)) (ddev->pHandle, dev,
  232. dev->width, dev->height, display_raster(ddev), ddev->nFormat);
  233. if (ccode < 0) {
  234. (*(ddev->callback->display_close))(ddev->pHandle, dev);
  235. return_error(ccode);
  236. }
  237. /* allocate the image */
  238. ccode = display_alloc_bitmap(ddev, dev);
  239. if (ccode < 0) {
  240. (*(ddev->callback->display_close))(ddev->pHandle, dev);
  241. return_error(ccode);
  242. }
  243. /* Tell caller the device parameters */
  244. ccode = (*(ddev->callback->display_size)) (ddev->pHandle, dev,
  245. dev->width, dev->height, display_raster(ddev), ddev->nFormat,
  246. ddev->mdev->base);
  247. if (ccode < 0) {
  248. display_free_bitmap(ddev);
  249. (*(ddev->callback->display_close))(ddev->pHandle, dev);
  250. return_error(ccode);
  251. }
  252. return 0;
  253. }
  254. private void
  255. display_get_initial_matrix(gx_device * dev, gs_matrix * pmat)
  256. {
  257. gx_device_display *ddev = (gx_device_display *) dev;
  258. if ((ddev->nFormat & DISPLAY_FIRSTROW_MASK) == DISPLAY_TOPFIRST)
  259. gx_default_get_initial_matrix(dev, pmat);
  260. else
  261. gx_upright_get_initial_matrix(dev, pmat); /* Windows / OS/2 */
  262. }
  263. /* Update the display. */
  264. int
  265. display_sync_output(gx_device * dev)
  266. {
  267. gx_device_display *ddev = (gx_device_display *) dev;
  268. if (ddev->callback == NULL)
  269. return 0;
  270. display_set_separations(ddev);
  271. (*(ddev->callback->display_sync))(ddev->pHandle, dev);
  272. return (0);
  273. }
  274. /* Update the display, bring to foreground. */
  275. /* If you want to pause on showpage, delay your return from callback */
  276. int
  277. display_output_page(gx_device * dev, int copies, int flush)
  278. {
  279. gx_device_display *ddev = (gx_device_display *) dev;
  280. int code;
  281. if (ddev->callback == NULL)
  282. return 0;
  283. display_set_separations(ddev);
  284. code = (*(ddev->callback->display_page))
  285. (ddev->pHandle, dev, copies, flush);
  286. if (code >= 0)
  287. code = gx_finish_output_page(dev, copies, flush);
  288. return code;
  289. }
  290. /* Close the display driver */
  291. private int
  292. display_close(gx_device * dev)
  293. {
  294. gx_device_display *ddev = (gx_device_display *) dev;
  295. if (ddev->callback == NULL)
  296. return 0;
  297. /* Tell caller that device is about to be closed. */
  298. (*(ddev->callback->display_preclose))(ddev->pHandle, dev);
  299. /* Release memory. */
  300. display_free_bitmap(ddev);
  301. /* Tell caller that device is closed. */
  302. /* This is always the last callback */
  303. (*(ddev->callback->display_close))(ddev->pHandle, dev);
  304. return 0;
  305. }
  306. /*
  307. * This routine will encode a 1 Black on white color.
  308. */
  309. private gx_color_index
  310. gx_b_w_gray_encode(gx_device * dev, const gx_color_value cv[])
  311. {
  312. return 1 - (cv[0] >> (gx_color_value_bits - 1));
  313. }
  314. /* DISPLAY_COLORS_NATIVE, 4bit/pixel */
  315. /* Map a r-g-b color to a color code */
  316. private gx_color_index
  317. display_map_rgb_color_device4(gx_device * dev, const gx_color_value cv[])
  318. {
  319. return pc_4bit_map_rgb_color(dev, cv);
  320. }
  321. /* Map a color code to r-g-b. */
  322. private int
  323. display_map_color_rgb_device4(gx_device * dev, gx_color_index color,
  324. gx_color_value prgb[3])
  325. {
  326. pc_4bit_map_color_rgb(dev, color, prgb);
  327. return 0;
  328. }
  329. /* DISPLAY_COLORS_NATIVE, 8bit/pixel */
  330. /* Map a r-g-b-k color to a color code */
  331. private gx_color_index
  332. display_encode_color_device8(gx_device * dev, const gx_color_value cv[])
  333. {
  334. /* palette of 96 colors */
  335. /* 0->63 = 00RRGGBB, 64->95 = 010YYYYY */
  336. gx_color_value r = cv[0];
  337. gx_color_value g = cv[1];
  338. gx_color_value b = cv[2];
  339. gx_color_value k = cv[3]; /* 0 = black */
  340. if ((r == 0) && (g == 0) && (b == 0)) {
  341. k = ((k >> (gx_color_value_bits - 6)) + 1) >> 1;
  342. if (k > 0x1f)
  343. k = 0x1f;
  344. return (k + 0x40);
  345. }
  346. if (k > 0) {
  347. /* The RGB->RGBK color mapping shouldn't generate this. */
  348. r = ((r+k) > gx_max_color_value) ? gx_max_color_value :
  349. (gx_color_value)(r+k);
  350. g = ((g+k) > gx_max_color_value) ? gx_max_color_value :
  351. (gx_color_value)(g+k);
  352. b = ((b+k) > gx_max_color_value) ? gx_max_color_value :
  353. (gx_color_value)(b+k);
  354. }
  355. r = ((r >> (gx_color_value_bits - 3)) + 1) >> 1;
  356. if (r > 0x3)
  357. r = 0x3;
  358. g = ((g >> (gx_color_value_bits - 3)) + 1) >> 1;
  359. if (g > 0x3)
  360. g = 0x3;
  361. b = ((b >> (gx_color_value_bits - 3)) + 1) >> 1;
  362. if (b > 0x3)
  363. b = 0x3;
  364. return (r << 4) + (g << 2) + b;
  365. }
  366. /* Map a color code to r-g-b-k. */
  367. private int
  368. display_decode_color_device8(gx_device * dev, gx_color_index color,
  369. gx_color_value prgb[4])
  370. {
  371. gx_color_value one;
  372. /* palette of 96 colors */
  373. /* 0->63 = 00RRGGBB, 64->95 = 010YYYYY */
  374. if (color < 64) {
  375. one = (gx_color_value) (gx_max_color_value / 3);
  376. prgb[0] = (gx_color_value) (((color >> 4) & 3) * one);
  377. prgb[1] = (gx_color_value) (((color >> 2) & 3) * one);
  378. prgb[2] = (gx_color_value) (((color) & 3) * one);
  379. prgb[3] = 0;
  380. }
  381. else if (color < 96) {
  382. one = (gx_color_value) (gx_max_color_value / 31);
  383. prgb[0] = prgb[1] = prgb[2] = 0;
  384. prgb[3] = (gx_color_value) ((color & 0x1f) * one);
  385. }
  386. else {
  387. prgb[0] = prgb[1] = prgb[2] = prgb[3] = 0;
  388. }
  389. return 0;
  390. }
  391. /* DISPLAY_COLORS_NATIVE, 16bit/pixel */
  392. /* Map a r-g-b color to a color code */
  393. private gx_color_index
  394. display_map_rgb_color_device16(gx_device * dev, const gx_color_value cv[])
  395. {
  396. gx_device_display *ddev = (gx_device_display *) dev;
  397. gx_color_value r = cv[0];
  398. gx_color_value g = cv[1];
  399. gx_color_value b = cv[2];
  400. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN) {
  401. if ((ddev->nFormat & DISPLAY_555_MASK) == DISPLAY_NATIVE_555)
  402. /* byte0=0RRRRRGG byte1=GGGBBBBB */
  403. return ((r >> (gx_color_value_bits - 5)) << 10) +
  404. ((g >> (gx_color_value_bits - 5)) << 5) +
  405. (b >> (gx_color_value_bits - 5));
  406. else
  407. /* byte0=RRRRRGGG byte1=GGGBBBBB */
  408. return ((r >> (gx_color_value_bits - 5)) << 11) +
  409. ((g >> (gx_color_value_bits - 6)) << 5) +
  410. (b >> (gx_color_value_bits - 5));
  411. }
  412. if ((ddev->nFormat & DISPLAY_555_MASK) == DISPLAY_NATIVE_555)
  413. /* byte0=GGGBBBBB byte1=0RRRRRGG */
  414. return ((r >> (gx_color_value_bits - 5)) << 2) +
  415. (((g >> (gx_color_value_bits - 5)) & 0x7) << 13) +
  416. (((g >> (gx_color_value_bits - 5)) & 0x18) >> 3) +
  417. ((b >> (gx_color_value_bits - 5)) << 8);
  418. /* byte0=GGGBBBBB byte1=RRRRRGGG */
  419. return ((r >> (gx_color_value_bits - 5)) << 3) +
  420. (((g >> (gx_color_value_bits - 6)) & 0x7) << 13) +
  421. (((g >> (gx_color_value_bits - 6)) & 0x38) >> 3) +
  422. ((b >> (gx_color_value_bits - 5)) << 8);
  423. }
  424. /* Map a color code to r-g-b. */
  425. private int
  426. display_map_color_rgb_device16(gx_device * dev, gx_color_index color,
  427. gx_color_value prgb[3])
  428. {
  429. gx_device_display *ddev = (gx_device_display *) dev;
  430. ushort value;
  431. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN) {
  432. if ((ddev->nFormat & DISPLAY_555_MASK) == DISPLAY_NATIVE_555) {
  433. /* byte0=0RRRRRGG byte1=GGGBBBBB */
  434. value = (ushort) (color >> 10);
  435. prgb[0] = (gx_color_value)
  436. (((value << 11) + (value << 6) + (value << 1) +
  437. (value >> 4)) >> (16 - gx_color_value_bits));
  438. value = (ushort) ((color >> 5) & 0x1f);
  439. prgb[1] = (gx_color_value)
  440. (((value << 11) + (value << 6) + (value << 1) +
  441. (value >> 4)) >> (16 - gx_color_value_bits));
  442. value = (ushort) (color & 0x1f);
  443. prgb[2] = (gx_color_value)
  444. (((value << 11) + (value << 6) + (value << 1) +
  445. (value >> 4)) >> (16 - gx_color_value_bits));
  446. }
  447. else {
  448. /* byte0=RRRRRGGG byte1=GGGBBBBB */
  449. value = (ushort) (color >> 11);
  450. prgb[0] = ((value << 11) + (value << 6) + (value << 1) +
  451. (value >> 4)) >> (16 - gx_color_value_bits);
  452. value = (ushort) ((color >> 5) & 0x3f);
  453. prgb[1] = (gx_color_value)
  454. ((value << 10) + (value << 4) + (value >> 2))
  455. >> (16 - gx_color_value_bits);
  456. value = (ushort) (color & 0x1f);
  457. prgb[2] = (gx_color_value)
  458. ((value << 11) + (value << 6) + (value << 1) +
  459. (value >> 4)) >> (16 - gx_color_value_bits);
  460. }
  461. }
  462. else {
  463. if ((ddev->nFormat & DISPLAY_555_MASK) == DISPLAY_NATIVE_555) {
  464. /* byte0=GGGBBBBB byte1=0RRRRRGG */
  465. value = (ushort) ((color >> 2) & 0x1f);
  466. prgb[0] = (gx_color_value)
  467. ((value << 11) + (value << 6) + (value << 1) +
  468. (value >> 4)) >> (16 - gx_color_value_bits);
  469. value = (ushort)
  470. (((color << 3) & 0x18) + ((color >> 13) & 0x7));
  471. prgb[1] = (gx_color_value)
  472. ((value << 11) + (value << 6) + (value << 1) +
  473. (value >> 4)) >> (16 - gx_color_value_bits);
  474. value = (ushort) ((color >> 8) & 0x1f);
  475. prgb[2] = (gx_color_value)
  476. ((value << 11) + (value << 6) + (value << 1) +
  477. (value >> 4)) >> (16 - gx_color_value_bits);
  478. }
  479. else {
  480. /* byte0=GGGBBBBB byte1=RRRRRGGG */
  481. value = (ushort) ((color >> 3) & 0x1f);
  482. prgb[0] = (gx_color_value)
  483. (((value << 11) + (value << 6) + (value << 1) +
  484. (value >> 4)) >> (16 - gx_color_value_bits));
  485. value = (ushort)
  486. (((color << 3) & 0x38) + ((color >> 13) & 0x7));
  487. prgb[1] = (gx_color_value)
  488. (((value << 10) + (value << 4) + (value >> 2))
  489. >> (16 - gx_color_value_bits));
  490. value = (ushort) ((color >> 8) & 0x1f);
  491. prgb[2] = (gx_color_value)
  492. (((value << 11) + (value << 6) + (value << 1) +
  493. (value >> 4)) >> (16 - gx_color_value_bits));
  494. }
  495. }
  496. return 0;
  497. }
  498. /* Map a r-g-b color to a color code */
  499. private gx_color_index
  500. display_map_rgb_color_rgb(gx_device * dev, const gx_color_value cv[])
  501. {
  502. gx_device_display *ddev = (gx_device_display *) dev;
  503. gx_color_value r = cv[0];
  504. gx_color_value g = cv[1];
  505. gx_color_value b = cv[2];
  506. int drop = gx_color_value_bits - 8;
  507. gx_color_value red, green, blue;
  508. red = r >> drop;
  509. green = g >> drop;
  510. blue = b >> drop;
  511. switch (ddev->nFormat & DISPLAY_ALPHA_MASK) {
  512. case DISPLAY_ALPHA_NONE:
  513. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN) {
  514. gx_color_value rgb[3];
  515. rgb[0] = r; rgb[1] = g; rgb[2] = b;
  516. return gx_default_rgb_map_rgb_color(dev, rgb); /* RGB */
  517. }
  518. else
  519. return (blue<<16) + (green<<8) + red; /* BGR */
  520. case DISPLAY_ALPHA_FIRST:
  521. case DISPLAY_UNUSED_FIRST:
  522. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN)
  523. return ((gx_color_index)red<<16) + (green<<8) + blue; /* xRGB */
  524. else
  525. return ((gx_color_index)blue<<16) + (green<<8) + red; /* xBGR */
  526. case DISPLAY_ALPHA_LAST:
  527. case DISPLAY_UNUSED_LAST:
  528. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN)
  529. return ((gx_color_index)red<<24) + (green<<16) + (blue<<8); /* RGBx */
  530. else
  531. return ((gx_color_index)blue<<24) + (green<<16) + (red<<8); /* BGRx */
  532. }
  533. return 0;
  534. }
  535. /* Map a color code to r-g-b. */
  536. private int
  537. display_map_color_rgb_rgb(gx_device * dev, gx_color_index color,
  538. gx_color_value prgb[3])
  539. {
  540. gx_device_display *ddev = (gx_device_display *) dev;
  541. uint bits_per_color = 8;
  542. uint color_mask;
  543. color_mask = (1 << bits_per_color) - 1;
  544. switch (ddev->nFormat & DISPLAY_ALPHA_MASK) {
  545. case DISPLAY_ALPHA_NONE:
  546. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN)
  547. return gx_default_rgb_map_color_rgb(dev, color, prgb); /* RGB */
  548. else {
  549. /* BGR */
  550. prgb[0] = (gx_color_value) (((color) & color_mask) *
  551. (ulong) gx_max_color_value / color_mask);
  552. prgb[1] = (gx_color_value)
  553. (((color >> bits_per_color) & color_mask) *
  554. (ulong) gx_max_color_value / color_mask);
  555. prgb[2] = (gx_color_value)
  556. (((color >> 2*bits_per_color) & color_mask) *
  557. (ulong) gx_max_color_value / color_mask);
  558. }
  559. break;
  560. case DISPLAY_ALPHA_FIRST:
  561. case DISPLAY_UNUSED_FIRST:
  562. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN) {
  563. /* xRGB */
  564. prgb[0] = (gx_color_value)
  565. (((color >> 2*bits_per_color) & color_mask) *
  566. (ulong) gx_max_color_value / color_mask);
  567. prgb[1] = (gx_color_value)
  568. (((color >> bits_per_color) & color_mask) *
  569. (ulong) gx_max_color_value / color_mask);
  570. prgb[2] = (gx_color_value) (((color) & color_mask) *
  571. (ulong) gx_max_color_value / color_mask);
  572. }
  573. else {
  574. /* xBGR */
  575. prgb[0] = (gx_color_value)
  576. (((color) & color_mask) *
  577. (ulong) gx_max_color_value / color_mask);
  578. prgb[1] = (gx_color_value)
  579. (((color >> bits_per_color) & color_mask) *
  580. (ulong) gx_max_color_value / color_mask);
  581. prgb[2] = (gx_color_value)
  582. (((color >> 2*bits_per_color) & color_mask) *
  583. (ulong) gx_max_color_value / color_mask);
  584. }
  585. break;
  586. case DISPLAY_ALPHA_LAST:
  587. case DISPLAY_UNUSED_LAST:
  588. if ((ddev->nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN) {
  589. /* RGBx */
  590. prgb[0] = (gx_color_value)
  591. (((color >> 3*bits_per_color) & color_mask) *
  592. (ulong) gx_max_color_value / color_mask);
  593. prgb[1] = (gx_color_value)
  594. (((color >> 2*bits_per_color) & color_mask) *
  595. (ulong) gx_max_color_value / color_mask);
  596. prgb[2] = (gx_color_value)
  597. (((color >> bits_per_color) & color_mask) *
  598. (ulong) gx_max_color_value / color_mask);
  599. }
  600. else {
  601. /* BGRx */
  602. prgb[0] = (gx_color_value)
  603. (((color >> bits_per_color) & color_mask) *
  604. (ulong) gx_max_color_value / color_mask);
  605. prgb[1] = (gx_color_value)
  606. (((color >> 2*bits_per_color) & color_mask) *
  607. (ulong) gx_max_color_value / color_mask);
  608. prgb[2] = (gx_color_value)
  609. (((color >> 3*bits_per_color) & color_mask) *
  610. (ulong) gx_max_color_value / color_mask);
  611. }
  612. }
  613. return 0;
  614. }
  615. /* Map a r-g-b color to a color code */
  616. private gx_color_index
  617. display_map_rgb_color_bgr24(gx_device * dev, const gx_color_value cv[])
  618. {
  619. gx_color_value r = cv[0];
  620. gx_color_value g = cv[1];
  621. gx_color_value b = cv[2];
  622. return (gx_color_value_to_byte(b)<<16) +
  623. (gx_color_value_to_byte(g)<<8) +
  624. gx_color_value_to_byte(r);
  625. }
  626. /* Map a color code to r-g-b. */
  627. private int
  628. display_map_color_rgb_bgr24(gx_device * dev, gx_color_index color,
  629. gx_color_value prgb[3])
  630. {
  631. prgb[0] = gx_color_value_from_byte(color & 0xff);
  632. prgb[1] = gx_color_value_from_byte((color >> 8) & 0xff);
  633. prgb[2] = gx_color_value_from_byte((color >> 16) & 0xff);
  634. return 0;
  635. }
  636. /* Fill a rectangle */
  637. private int
  638. display_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  639. gx_color_index color)
  640. {
  641. gx_device_display *ddev = (gx_device_display *) dev;
  642. if (ddev->callback == NULL)
  643. return 0;
  644. dev_proc(ddev->mdev, fill_rectangle)((gx_device *)ddev->mdev,
  645. x, y, w, h, color);
  646. if (ddev->callback->display_update)
  647. (*(ddev->callback->display_update))(ddev->pHandle, dev, x, y, w, h);
  648. return 0;
  649. }
  650. /* Copy a monochrome bitmap */
  651. private int
  652. display_copy_mono(gx_device * dev,
  653. const byte * base, int sourcex, int raster, gx_bitmap_id id,
  654. int x, int y, int w, int h,
  655. gx_color_index zero, gx_color_index one)
  656. {
  657. gx_device_display *ddev = (gx_device_display *) dev;
  658. if (ddev->callback == NULL)
  659. return 0;
  660. dev_proc(ddev->mdev, copy_mono)((gx_device *)ddev->mdev,
  661. base, sourcex, raster, id, x, y, w, h, zero, one);
  662. if (ddev->callback->display_update)
  663. (*(ddev->callback->display_update))(ddev->pHandle, dev, x, y, w, h);
  664. return 0;
  665. }
  666. /* Copy a color pixel map */
  667. private int
  668. display_copy_color(gx_device * dev,
  669. const byte * base, int sourcex, int raster, gx_bitmap_id id,
  670. int x, int y, int w, int h)
  671. {
  672. gx_device_display *ddev = (gx_device_display *) dev;
  673. if (ddev->callback == NULL)
  674. return 0;
  675. dev_proc(ddev->mdev, copy_color)((gx_device *)ddev->mdev,
  676. base, sourcex, raster, id, x, y, w, h);
  677. if (ddev->callback->display_update)
  678. (*(ddev->callback->display_update))(ddev->pHandle, dev, x, y, w, h);
  679. return 0;
  680. }
  681. private int
  682. display_get_bits(gx_device * dev, int y, byte * str, byte ** actual_data)
  683. {
  684. gx_device_display *ddev = (gx_device_display *) dev;
  685. if (ddev->callback == NULL)
  686. return 0;
  687. return dev_proc(ddev->mdev, get_bits)((gx_device *)ddev->mdev,
  688. y, str, actual_data);
  689. }
  690. private int
  691. display_get_params(gx_device * dev, gs_param_list * plist)
  692. {
  693. gx_device_display *ddev = (gx_device_display *) dev;
  694. int code;
  695. gs_param_string dhandle;
  696. int idx;
  697. int val;
  698. int i = 0;
  699. size_t dptr;
  700. char buf[64];
  701. idx = ((int)sizeof(size_t)) * 8 - 4;
  702. buf[i++] = '1';
  703. buf[i++] = '6';
  704. buf[i++] = '#';
  705. dptr = (size_t)(ddev->pHandle);
  706. while (idx >= 0) {
  707. val = (int)(dptr >> idx) & 0xf;
  708. if (val <= 9)
  709. buf[i++] = '0' + val;
  710. else
  711. buf[i++] = 'a' - 10 + val;
  712. idx -= 4;
  713. }
  714. buf[i] = '\0';
  715. param_string_from_transient_string(dhandle, buf);
  716. code = gx_default_get_params(dev, plist);
  717. (void)(code < 0 ||
  718. (code = param_write_string(plist,
  719. "DisplayHandle", &dhandle)) < 0 ||
  720. (code = param_write_int(plist,
  721. "DisplayFormat", &ddev->nFormat)) < 0 ||
  722. (code = param_write_float(plist,
  723. "DisplayResolution", &ddev->HWResolution[1])) < 0);
  724. if (code >= 0 &&
  725. (ddev->nFormat & DISPLAY_COLORS_MASK) == DISPLAY_COLORS_SEPARATION)
  726. code = devn_get_params(dev, plist, &ddev->devn_params,
  727. &ddev->equiv_cmyk_colors);
  728. return code;
  729. }
  730. /* Put parameters. */
  731. /* The parameters "DisplayHandle" and "DisplayFormat"
  732. * can be changed when the device is closed, but not when open.
  733. * The device width and height can be changed when open.
  734. */
  735. private int
  736. display_put_params(gx_device * dev, gs_param_list * plist)
  737. {
  738. gx_device_display *ddev = (gx_device_display *) dev;
  739. int ecode = 0, code;
  740. bool is_open = dev->is_open;
  741. gs_param_float_array hwra;
  742. float dispres = 0.0;
  743. int old_width = dev->width;
  744. int old_height = dev->height;
  745. int old_format = ddev->nFormat;
  746. void *old_handle = ddev->pHandle;
  747. gs_devn_params *pdevn_params = &ddev->devn_params;
  748. equivalent_cmyk_color_params *pequiv_colors = &ddev->equiv_cmyk_colors;
  749. /* Save current data in case we have a problem */
  750. gs_devn_params saved_devn_params = *pdevn_params;
  751. equivalent_cmyk_color_params saved_equiv_colors = *pequiv_colors;
  752. int format;
  753. void *handle;
  754. int found_string_handle = 0;
  755. gs_param_string dh = { 0 };
  756. /* Handle extra parameters */
  757. switch (code = param_read_int(plist, "DisplayFormat", &format)) {
  758. case 0:
  759. if (dev->is_open) {
  760. if (ddev->nFormat != format)
  761. ecode = gs_error_rangecheck;
  762. else
  763. break;
  764. }
  765. else {
  766. code = display_set_color_format(ddev, format);
  767. if (code < 0)
  768. ecode = code;
  769. else
  770. break;
  771. }
  772. goto cfe;
  773. default:
  774. ecode = code;
  775. cfe:param_signal_error(plist, "DisplayFormat", ecode);
  776. case 1:
  777. break;
  778. }
  779. /* 64-bit systems need to use DisplayHandle as a string */
  780. switch (code = param_read_string(plist, "DisplayHandle", &dh)) {
  781. case 0:
  782. found_string_handle = 1;
  783. break;
  784. default:
  785. if ((code == gs_error_typecheck) && (sizeof(size_t) <= 4)) {
  786. /* 32-bit systems can use the older long type */
  787. switch (code = param_read_long(plist, "DisplayHandle",
  788. (long *)(&handle))) {
  789. case 0:
  790. if (dev->is_open) {
  791. if (ddev->pHandle != handle)
  792. ecode = gs_error_rangecheck;
  793. else
  794. break;
  795. }
  796. else {
  797. ddev->pHandle = handle;
  798. break;
  799. }
  800. goto hdle;
  801. default:
  802. ecode = code;
  803. hdle:param_signal_error(plist, "DisplayHandle", ecode);
  804. case 1:
  805. break;
  806. }
  807. break;
  808. }
  809. ecode = code;
  810. param_signal_error(plist, "DisplayHandle", ecode);
  811. /* fall through */
  812. case 1:
  813. dh.data = 0;
  814. break;
  815. }
  816. if (found_string_handle) {
  817. /*
  818. * Convert from a string to a pointer.
  819. * It is assumed that size_t has the same size as a pointer.
  820. * Allow formats (1234), (10#1234) or (16#04d2).
  821. */
  822. size_t ptr = 0;
  823. int i;
  824. int base = 10;
  825. int val;
  826. code = 0;
  827. for (i=0; i<dh.size; i++) {
  828. val = dh.data[i];
  829. if ((val >= '0') && (val <= '9'))
  830. val = val - '0';
  831. else if ((val >= 'A') && (val <= 'F'))
  832. val = val - 'A' + 10;
  833. else if ((val >= 'a') && (val <= 'f'))
  834. val = val - 'a' + 10;
  835. else if (val == '#') {
  836. base = (int)ptr;
  837. ptr = 0;
  838. if ((base != 10) && (base != 16)) {
  839. code = gs_error_rangecheck;
  840. break;
  841. }
  842. continue;
  843. }
  844. else {
  845. code = gs_error_rangecheck;
  846. break;
  847. }
  848. if (base == 10)
  849. ptr = ptr * 10 + val;
  850. else if (base == 16)
  851. ptr = ptr * 16 + val;
  852. else {
  853. code = gs_error_rangecheck;
  854. break;
  855. }
  856. }
  857. if (code == 0) {
  858. if (dev->is_open) {
  859. if (ddev->pHandle != (void *)ptr)
  860. code = gs_error_rangecheck;
  861. }
  862. else
  863. ddev->pHandle = (void *)ptr;
  864. }
  865. if (code < 0) {
  866. ecode = code;
  867. param_signal_error(plist, "DisplayHandle", ecode);
  868. }
  869. }
  870. /*
  871. * Set the initial display resolution.
  872. * If HWResolution is explicitly set, e.g. using -rDPI on the
  873. * command line, then use that. Otherwise, use DisplayResolution
  874. * which is typically set by the client to the display
  875. * logical resolution. Once either of these have been
  876. * used, ignore all further DisplayResolution parameters.
  877. */
  878. if (param_read_float_array(plist, "HWResolution", &hwra) == 0)
  879. ddev->HWResolution_set = 1;
  880. switch (code = param_read_float(plist, "DisplayResolution", &dispres)) {
  881. case 0:
  882. if (!ddev->HWResolution_set) {
  883. gx_device_set_resolution(dev, dispres, dispres);
  884. ddev->HWResolution_set = 1;
  885. }
  886. break;
  887. default:
  888. ecode = code;
  889. param_signal_error(plist, "DisplayResolution", ecode);
  890. case 1:
  891. break;
  892. }
  893. if (ecode >= 0 &&
  894. (ddev->nFormat & DISPLAY_COLORS_MASK) == DISPLAY_COLORS_SEPARATION) {
  895. /* Use utility routine to handle devn parameters */
  896. ecode = devn_put_params(dev, plist, pdevn_params, pequiv_colors);
  897. /*
  898. * Setting MaxSeparations changes color_info.depth in
  899. * devn_put_params, but we always use 64bpp,
  900. * so reset it to the the correct value.
  901. */
  902. dev->color_info.depth = arch_sizeof_color_index * 8;
  903. }
  904. if (ecode >= 0) {
  905. /* Prevent gx_default_put_params from closing the device. */
  906. dev->is_open = false;
  907. ecode = gx_default_put_params(dev, plist);
  908. dev->is_open = is_open;
  909. }
  910. if (ecode < 0) {
  911. /* If we have an error then restore original data. */
  912. *pdevn_params = saved_devn_params;
  913. *pequiv_colors = saved_equiv_colors;
  914. if (format != old_format)
  915. display_set_color_format(ddev, old_format);
  916. ddev->pHandle = old_handle;
  917. dev->width = old_width;
  918. dev->height = old_height;
  919. return ecode;
  920. }
  921. if ( is_open && ddev->callback &&
  922. ((old_width != dev->width) || (old_height != dev->height)) ) {
  923. /* We can resize this device while it is open, but we cannot
  924. * change the color format or handle.
  925. */
  926. /* Tell caller we are about to change the device parameters */
  927. if ((*ddev->callback->display_presize)(ddev->pHandle, dev,
  928. dev->width, dev->height, display_raster(ddev),
  929. ddev->nFormat) < 0) {
  930. /* caller won't let us change the size */
  931. /* restore parameters then return an error */
  932. *pdevn_params = saved_devn_params;
  933. *pequiv_colors = saved_equiv_colors;
  934. display_set_color_format(ddev, old_format);
  935. ddev->nFormat = old_format;
  936. ddev->pHandle = old_handle;
  937. dev->width = old_width;
  938. dev->height = old_height;
  939. return_error(gs_error_rangecheck);
  940. }
  941. display_free_bitmap(ddev);
  942. code = display_alloc_bitmap(ddev, dev);
  943. if (code < 0) {
  944. /* No bitmap, so tell the caller it is zero size */
  945. (*ddev->callback->display_size)(ddev->pHandle, dev,
  946. 0, 0, 0, ddev->nFormat, NULL);
  947. return_error(code);
  948. }
  949. /* tell caller about the new size */
  950. if ((*ddev->callback->display_size)(ddev->pHandle, dev,
  951. dev->width, dev->height, display_raster(ddev),
  952. ddev->nFormat, ddev->mdev->base) < 0)
  953. return_error(gs_error_rangecheck);
  954. }
  955. return 0;
  956. }
  957. /* Clean up the instance after making a copy. */
  958. int
  959. display_finish_copydevice(gx_device *dev, const gx_device *from_dev)
  960. {
  961. gx_device_display *ddev = (gx_device_display *) dev;
  962. /* Mark the new instance as closed. */
  963. ddev->is_open = false;
  964. /* Clear pointers */
  965. ddev->mdev = NULL;
  966. ddev->pBitmap = NULL;
  967. ddev->ulBitmapSize = 0;
  968. return 0;
  969. }
  970. /*
  971. * The following procedures are used to map the standard color spaces into
  972. * the separation color components for the display device.
  973. */
  974. private void
  975. display_separation_gray_cs_to_cmyk_cm(gx_device * dev, frac gray, frac out[])
  976. {
  977. int * map =
  978. (int *)(&((gx_device_display *) dev)->devn_params.separation_order_map);
  979. gray_cs_to_devn_cm(dev, map, gray, out);
  980. }
  981. private void
  982. display_separation_rgb_cs_to_cmyk_cm(gx_device * dev,
  983. const gs_imager_state *pis, frac r, frac g, frac b, frac out[])
  984. {
  985. int * map =
  986. (int *)(&((gx_device_display *) dev)->devn_params.separation_order_map);
  987. rgb_cs_to_devn_cm(dev, map, pis, r, g, b, out);
  988. }
  989. private void
  990. display_separation_cmyk_cs_to_cmyk_cm(gx_device * dev,
  991. frac c, frac m, frac y, frac k, frac out[])
  992. {
  993. int * map =
  994. (int *)(&((gx_device_display *) dev)->devn_params.separation_order_map);
  995. cmyk_cs_to_devn_cm(dev, map, c, m, y, k, out);
  996. }
  997. private const gx_cm_color_map_procs display_separation_cm_procs = {
  998. display_separation_gray_cs_to_cmyk_cm,
  999. display_separation_rgb_cs_to_cmyk_cm,
  1000. display_separation_cmyk_cs_to_cmyk_cm
  1001. };
  1002. private const gx_cm_color_map_procs *
  1003. display_separation_get_color_mapping_procs(const gx_device * dev)
  1004. {
  1005. return &display_separation_cm_procs;
  1006. }
  1007. /*
  1008. * Encode a list of colorant values into a gx_color_index_value.
  1009. */
  1010. private gx_color_index
  1011. display_separation_encode_color(gx_device *dev, const gx_color_value colors[])
  1012. {
  1013. int bpc = ((gx_device_display *)dev)->devn_params.bitspercomponent;
  1014. int drop = sizeof(gx_color_value) * 8 - bpc;
  1015. gx_color_index color = 0;
  1016. int i = 0;
  1017. int ncomp = dev->color_info.num_components;
  1018. for (; i<ncomp; i++) {
  1019. color <<= bpc;
  1020. color |= (colors[i] >> drop);
  1021. }
  1022. if (bpc*ncomp < arch_sizeof_color_index * 8)
  1023. color <<= (arch_sizeof_color_index * 8 - ncomp * bpc);
  1024. return (color == gx_no_color_index ? color ^ 1 : color);
  1025. }
  1026. /*
  1027. * Decode a gx_color_index value back to a list of colorant values.
  1028. */
  1029. private int
  1030. display_separation_decode_color(gx_device * dev, gx_color_index color,
  1031. gx_color_value * out)
  1032. {
  1033. int bpc = ((gx_device_display *)dev)->devn_params.bitspercomponent;
  1034. int drop = sizeof(gx_color_value) * 8 - bpc;
  1035. int mask = (1 << bpc) - 1;
  1036. int i = 0;
  1037. int ncomp = dev->color_info.num_components;
  1038. if (bpc*ncomp < arch_sizeof_color_index * 8)
  1039. color >>= (arch_sizeof_color_index * 8 - ncomp * bpc);
  1040. for (; i<ncomp; i++) {
  1041. out[ncomp - i - 1] = (gx_color_value) ((color & mask) << drop);
  1042. color >>= bpc;
  1043. }
  1044. return 0;
  1045. }
  1046. /*
  1047. * Device proc for updating the equivalent CMYK color for spot colors.
  1048. */
  1049. private int
  1050. display_update_spot_equivalent_colors(gx_device * dev, const gs_state * pgs)
  1051. {
  1052. gx_device_display * ddev = (gx_device_display *)dev;
  1053. if ((ddev->nFormat & DISPLAY_COLORS_MASK) == DISPLAY_COLORS_SEPARATION)
  1054. update_spot_equivalent_cmyk_colors(dev, pgs,
  1055. &ddev->devn_params, &ddev->equiv_cmyk_colors);
  1056. return 0;
  1057. }
  1058. /*
  1059. * This routine will check to see if the color component name match those
  1060. * that are available amoung the current device's color components.
  1061. *
  1062. * Parameters:
  1063. * dev - pointer to device data structure.
  1064. * pname - pointer to name (zero termination not required)
  1065. * nlength - length of the name
  1066. *
  1067. * This routine returns a positive value (0 to n) which is the device colorant
  1068. * number if the name is found. It returns GX_DEVICE_COLOR_MAX_COMPONENTS if
  1069. * the colorant is not being used due to a SeparationOrder device parameter.
  1070. * It returns a negative value if not found.
  1071. */
  1072. private int
  1073. display_separation_get_color_comp_index(gx_device * dev,
  1074. const char * pname, int name_size, int component_type)
  1075. {
  1076. return devn_get_color_comp_index(dev,
  1077. &(((gx_device_display *)dev)->devn_params),
  1078. &(((gx_device_display *)dev)->equiv_cmyk_colors),
  1079. pname, name_size, component_type, ENABLE_AUTO_SPOT_COLORS);
  1080. }
  1081. /* ------ Internal routines ------ */
  1082. /* Make sure we have been given a valid structure */
  1083. /* Return 0 on success, gs_error_rangecheck on failure */
  1084. private int display_check_structure(gx_device_display *ddev)
  1085. {
  1086. if (ddev->callback == 0)
  1087. return_error(gs_error_rangecheck);
  1088. if (ddev->callback->size == sizeof(struct display_callback_v1_s)) {
  1089. /* Original V1 structure */
  1090. if (ddev->callback->version_major != DISPLAY_VERSION_MAJOR_V1)
  1091. return_error(gs_error_rangecheck);
  1092. /* complain if caller asks for newer features */
  1093. if (ddev->callback->version_minor > DISPLAY_VERSION_MINOR_V1)
  1094. return_error(gs_error_rangecheck);
  1095. }
  1096. else {
  1097. /* V2 structure with added display_separation callback */
  1098. if (ddev->callback->size != sizeof(display_callback))
  1099. return_error(gs_error_rangecheck);
  1100. if (ddev->callback->version_major != DISPLAY_VERSION_MAJOR)
  1101. return_error(gs_error_rangecheck);
  1102. /* complain if caller asks for newer features */
  1103. if (ddev->callback->version_minor > DISPLAY_VERSION_MINOR)
  1104. return_error(gs_error_rangecheck);
  1105. }
  1106. if ((ddev->callback->display_open == NULL) ||
  1107. (ddev->callback->display_close == NULL) ||
  1108. (ddev->callback->display_presize == NULL) ||
  1109. (ddev->callback->display_size == NULL) ||
  1110. (ddev->callback->display_sync == NULL) ||
  1111. (ddev->callback->display_page == NULL))
  1112. return_error(gs_error_rangecheck);
  1113. /* Don't test display_update, display_memalloc or display_memfree
  1114. * since these may be NULL if not provided.
  1115. * Don't test display_separation, since this may be NULL if
  1116. * separation format is not supported.
  1117. */
  1118. return 0;
  1119. }
  1120. private void
  1121. display_free_bitmap(gx_device_display * ddev)
  1122. {
  1123. if (ddev->callback == NULL)
  1124. return;
  1125. if (ddev->pBitmap) {
  1126. if (ddev->callback->display_memalloc
  1127. && ddev->callback->display_memfree
  1128. && ddev->pBitmap) {
  1129. (*ddev->callback->display_memfree)(ddev->pHandle, ddev,
  1130. ddev->pBitmap);
  1131. }
  1132. else {
  1133. gs_free_object(ddev->memory->non_gc_memory,
  1134. ddev->pBitmap, "display_free_bitmap");
  1135. }
  1136. ddev->pBitmap = NULL;
  1137. if (ddev->mdev)
  1138. ddev->mdev->base = NULL;
  1139. }
  1140. if (ddev->mdev) {
  1141. dev_proc(ddev->mdev, close_device)((gx_device *)ddev->mdev);
  1142. gx_device_retain((gx_device *)(ddev->mdev), false);
  1143. ddev->mdev = NULL;
  1144. }
  1145. }
  1146. /* calculate byte length of a row */
  1147. private int
  1148. display_raster(gx_device_display *dev)
  1149. {
  1150. int align = 0;
  1151. int bytewidth = dev->width * dev->color_info.depth/8;
  1152. switch (dev->nFormat & DISPLAY_ROW_ALIGN_MASK) {
  1153. case DISPLAY_ROW_ALIGN_4:
  1154. align = 4;
  1155. break;
  1156. case DISPLAY_ROW_ALIGN_8:
  1157. align = 8;
  1158. break;
  1159. case DISPLAY_ROW_ALIGN_16:
  1160. align = 16;
  1161. break;
  1162. case DISPLAY_ROW_ALIGN_32:
  1163. align = 32;
  1164. break;
  1165. case DISPLAY_ROW_ALIGN_64:
  1166. align = 64;
  1167. break;
  1168. }
  1169. if (align < ARCH_ALIGN_PTR_MOD)
  1170. align = ARCH_ALIGN_PTR_MOD;
  1171. align -= 1;
  1172. bytewidth = (bytewidth + align) & (~align);
  1173. return bytewidth;
  1174. }
  1175. /* Allocate the backing bitmap. */
  1176. private int
  1177. display_alloc_bitmap(gx_device_display * ddev, gx_device * param_dev)
  1178. {
  1179. int ccode;
  1180. const gx_device_memory *mdproto;
  1181. if (ddev->callback == NULL)
  1182. return 0;
  1183. /* free old bitmap (if any) */
  1184. display_free_bitmap(ddev);
  1185. /* allocate a memory device for rendering */
  1186. mdproto = gdev_mem_device_for_bits(ddev->color_info.depth);
  1187. if (mdproto == 0)
  1188. return_error(gs_error_rangecheck);
  1189. ddev->mdev = gs_alloc_struct(gs_memory_stable(ddev->memory),
  1190. gx_device_memory, &st_device_memory, "display_memory_device");
  1191. if (ddev->mdev == 0)
  1192. return_error(gs_error_VMerror);
  1193. gs_make_mem_device(ddev->mdev, mdproto, gs_memory_stable(ddev->memory),
  1194. 0, (gx_device *) NULL);
  1195. check_device_separable((gx_device *)(ddev->mdev));
  1196. gx_device_fill_in_procs((gx_device *)(ddev->mdev));
  1197. /* Mark the memory device as retained. When the bitmap is closed,
  1198. * we will clear this and the memory device will be then be freed.
  1199. */
  1200. gx_device_retain((gx_device *)(ddev->mdev), true);
  1201. /* Memory device width may be larger than device width
  1202. * if row alignment is not 4.
  1203. */
  1204. ddev->mdev->width = param_dev->width;
  1205. ddev->mdev->width = display_raster(ddev) * 8 / ddev->color_info.depth;
  1206. ddev->mdev->height = param_dev->height;
  1207. /* Tell the memory device to allocate the line pointers separately
  1208. * so we can place the bitmap in special memory.
  1209. */
  1210. ddev->mdev->line_pointer_memory = ddev->mdev->memory;
  1211. ddev->ulBitmapSize = gdev_mem_bits_size(ddev->mdev,
  1212. ddev->mdev->width, ddev->mdev->height);
  1213. /* allocate bitmap using an allocator not subject to GC */
  1214. if (ddev->callback->display_memalloc
  1215. && ddev->callback->display_memfree) {
  1216. ddev->pBitmap = (*ddev->callback->display_memalloc)(ddev->pHandle,
  1217. ddev, ddev->ulBitmapSize);
  1218. }
  1219. else {
  1220. ddev->pBitmap = gs_alloc_byte_array_immovable(ddev->memory->non_gc_memory,
  1221. (uint)ddev->ulBitmapSize, 1, "display_alloc_bitmap");
  1222. }
  1223. if (ddev->pBitmap == NULL) {
  1224. ddev->mdev->width = 0;
  1225. ddev->mdev->height = 0;
  1226. return_error(gs_error_VMerror);
  1227. }
  1228. ddev->mdev->base = (byte *) ddev->pBitmap;
  1229. ddev->mdev->foreign_bits = true;
  1230. ccode = dev_proc(ddev->mdev, open_device)((gx_device *)ddev->mdev);
  1231. if (ccode < 0)
  1232. display_free_bitmap(ddev);
  1233. /* erase bitmap - before display gets redrawn */
  1234. if (ccode == 0) {
  1235. int i;
  1236. gx_color_value cv[GX_DEVICE_COLOR_MAX_COMPONENTS];
  1237. for (i=0; i<GX_DEVICE_COLOR_MAX_COMPONENTS; i++)
  1238. cv[i] = (ddev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE)
  1239. ? gx_max_color_value : 0;
  1240. dev_proc(ddev, fill_rectangle)((gx_device *)ddev,
  1241. 0, 0, ddev->width, ddev->height,
  1242. ddev->procs.encode_color((gx_device *)ddev, cv));
  1243. }
  1244. return ccode;
  1245. }
  1246. private int
  1247. display_set_separations(gx_device_display *dev)
  1248. {
  1249. if (((dev->nFormat & DISPLAY_COLORS_MASK) == DISPLAY_COLORS_SEPARATION) &&
  1250. (dev->callback->version_major > DISPLAY_VERSION_MAJOR_V1) &&
  1251. (dev->callback->display_separation != NULL)) {
  1252. /* Tell the client about the separation to composite mapping */
  1253. char name[64];
  1254. int num_spot = dev->devn_params.separations.num_separations;
  1255. int num_std_colorants = dev->devn_params.num_std_colorant_names;
  1256. int num_comp = num_std_colorants + num_spot;
  1257. int comp_map[GX_DEVICE_COLOR_MAX_COMPONENTS];
  1258. int comp_num;
  1259. int sep_num;
  1260. int sep_name_size;
  1261. unsigned int c, m, y, k;
  1262. /* Map the separation numbers to component numbers */
  1263. memset(comp_map, 0, sizeof(comp_map));
  1264. for (sep_num = 0; sep_num < num_comp; sep_num++) {
  1265. comp_num = dev->devn_params.separation_order_map[sep_num];
  1266. if (comp_num >= 0 && comp_num < GX_DEVICE_COLOR_MAX_COMPONENTS)
  1267. comp_map[comp_num] = sep_num;
  1268. }
  1269. /* For each component, tell the client the separation mapping */
  1270. for (comp_num = 0; comp_num < num_comp; comp_num++) {
  1271. c = y = m = k = 0;
  1272. sep_num = comp_map[comp_num];
  1273. /* Get the CMYK equivalent */
  1274. if (sep_num < dev->devn_params.num_std_colorant_names) {
  1275. sep_name_size =
  1276. strlen(dev->devn_params.std_colorant_names[sep_num]);
  1277. if (sep_name_size > sizeof(name)-2)
  1278. sep_name_size = sizeof(name)-1;
  1279. memcpy(name, dev->devn_params.std_colorant_names[sep_num],
  1280. sep_name_size);
  1281. name[sep_name_size] = '\0';
  1282. switch (sep_num) {
  1283. case 0: c = 65535; break;
  1284. case 1: m = 65535; break;
  1285. case 2: y = 65535; break;
  1286. case 3: k = 65535; break;
  1287. }
  1288. }
  1289. else {
  1290. sep_num -= dev->devn_params.num_std_colorant_names;
  1291. sep_name_size =
  1292. dev->devn_params.separations.names[sep_num].size;
  1293. if (sep_name_size > sizeof(name)-2)
  1294. sep_name_size = sizeof(name)-1;
  1295. memcpy(name, dev->devn_params.separations.names[sep_num].data,
  1296. sep_name_size);
  1297. name[sep_name_size] = '\0';
  1298. if (dev->equiv_cmyk_colors.color[sep_num].color_info_valid) {
  1299. c = dev->equiv_cmyk_colors.color[sep_num].c
  1300. * 65535 / frac_1;
  1301. m = dev->equiv_cmyk_colors.color[sep_num].m
  1302. * 65535 / frac_1;
  1303. y = dev->equiv_cmyk_colors.color[sep_num].y
  1304. * 65535 / frac_1;
  1305. k = dev->equiv_cmyk_colors.color[sep_num].k
  1306. * 65535 / frac_1;
  1307. }
  1308. }
  1309. (*dev->callback->display_separation)(dev->pHandle, dev,
  1310. comp_num, name,
  1311. (unsigned short)c, (unsigned short)m,
  1312. (unsigned short)y, (unsigned short)k);
  1313. }
  1314. }
  1315. return 0;
  1316. }
  1317. typedef enum DISPLAY_MODEL_e {
  1318. DISPLAY_MODEL_GRAY=0,
  1319. DISPLAY_MODEL_RGB=1,
  1320. DISPLAY_MODEL_RGBK=2,
  1321. DISPLAY_MODEL_CMYK=3,
  1322. DISPLAY_MODEL_SEP=4
  1323. } DISPLAY_MODEL;
  1324. /*
  1325. * This is a utility routine to build the display device's color_info
  1326. * structure (except for the anti alias info).
  1327. */
  1328. private void
  1329. set_color_info(gx_device_color_info * pdci, DISPLAY_MODEL model,
  1330. int nc, int depth, int maxgray, int maxcolor)
  1331. {
  1332. pdci->num_components = pdci->max_components = nc;
  1333. pdci->depth = depth;
  1334. pdci->gray_index = 0;
  1335. pdci->max_gray = maxgray;
  1336. pdci->max_color = maxcolor;
  1337. pdci->dither_grays = maxgray + 1;
  1338. pdci->dither_colors = maxcolor + 1;
  1339. pdci->separable_and_linear = GX_CINFO_UNKNOWN_SEP_LIN;
  1340. switch (model) {
  1341. case DISPLAY_MODEL_GRAY:
  1342. pdci->polarity = GX_CINFO_POLARITY_ADDITIVE;
  1343. pdci->cm_name = "DeviceGray";
  1344. pdci->gray_index = 0;
  1345. break;
  1346. case DISPLAY_MODEL_RGB:
  1347. pdci->polarity = GX_CINFO_POLARITY_ADDITIVE;
  1348. pdci->cm_name = "DeviceRGB";
  1349. pdci->gray_index = GX_CINFO_COMP_NO_INDEX;
  1350. break;
  1351. case DISPLAY_MODEL_RGBK:
  1352. pdci->polarity = GX_CINFO_POLARITY_ADDITIVE;
  1353. pdci->cm_name = "DeviceRGBK";
  1354. pdci->gray_index = 3;
  1355. break;
  1356. case DISPLAY_MODEL_CMYK:
  1357. pdci->polarity = GX_CINFO_POLARITY_SUBTRACTIVE;
  1358. pdci->cm_name = "DeviceCMYK";
  1359. pdci->gray_index = 3;
  1360. break;
  1361. default:
  1362. case DISPLAY_MODEL_SEP:
  1363. /* Anything else is separations */
  1364. pdci->polarity = GX_CINFO_POLARITY_SUBTRACTIVE;
  1365. pdci->cm_name = "DeviceCMYK";
  1366. pdci->gray_index = GX_CINFO_COMP_NO_INDEX; /* may not have K */
  1367. break;
  1368. }
  1369. }
  1370. /*
  1371. * This is an utility routine to set up the color procs for the display
  1372. * device. The display device can change its setup.
  1373. */
  1374. private void
  1375. set_color_procs(gx_device * pdev,
  1376. dev_t_proc_encode_color((*encode_color), gx_device),
  1377. dev_t_proc_decode_color((*decode_color), gx_device),
  1378. dev_t_proc_get_color_mapping_procs((*get_color_mapping_procs), gx_device),
  1379. dev_t_proc_get_color_comp_index((*get_color_comp_index), gx_device))
  1380. {
  1381. #if 0 /* These procs are no longer used */
  1382. pdev->procs.map_rgb_color = encode_color;
  1383. pdev->procs.map_color_rgb = decode_color;
  1384. #endif
  1385. pdev->procs.get_color_mapping_procs = get_color_mapping_procs;
  1386. pdev->procs.get_color_comp_index = get_color_comp_index;
  1387. pdev->procs.encode_color = encode_color;
  1388. pdev->procs.decode_color = decode_color;
  1389. }
  1390. /*
  1391. * This is an utility routine to set up the color procs for the display
  1392. * device. This routine is used when the display device is Gray.
  1393. */
  1394. private void
  1395. set_gray_color_procs(gx_device * pdev,
  1396. dev_t_proc_encode_color((*encode_color), gx_device),
  1397. dev_t_proc_decode_color((*decode_color), gx_device))
  1398. {
  1399. set_color_procs(pdev, encode_color, decode_color,
  1400. gx_default_DevGray_get_color_mapping_procs,
  1401. gx_default_DevGray_get_color_comp_index);
  1402. }
  1403. /*
  1404. * This is an utility routine to set up the color procs for the display
  1405. * device. This routine is used when the display device is RGB.
  1406. */
  1407. private void
  1408. set_rgb_color_procs(gx_device * pdev,
  1409. dev_t_proc_encode_color((*encode_color), gx_device),
  1410. dev_t_proc_decode_color((*decode_color), gx_device))
  1411. {
  1412. set_color_procs(pdev, encode_color, decode_color,
  1413. gx_default_DevRGB_get_color_mapping_procs,
  1414. gx_default_DevRGB_get_color_comp_index);
  1415. }
  1416. /*
  1417. * This is an utility routine to set up the color procs for the display
  1418. * device. This routine is used when the display device is RGBK.
  1419. */
  1420. private void
  1421. set_rgbk_color_procs(gx_device * pdev,
  1422. dev_t_proc_encode_color((*encode_color), gx_device),
  1423. dev_t_proc_decode_color((*decode_color), gx_device))
  1424. {
  1425. set_color_procs(pdev, encode_color, decode_color,
  1426. gx_default_DevRGBK_get_color_mapping_procs,
  1427. gx_default_DevRGBK_get_color_comp_index);
  1428. }
  1429. /*
  1430. * This is an utility routine to set up the color procs for the display
  1431. * device. This routine is used when the display device is CMYK.
  1432. */
  1433. private void
  1434. set_cmyk_color_procs(gx_device * pdev,
  1435. dev_t_proc_encode_color((*encode_color), gx_device),
  1436. dev_t_proc_decode_color((*decode_color), gx_device))
  1437. {
  1438. set_color_procs(pdev, encode_color, decode_color,
  1439. gx_default_DevCMYK_get_color_mapping_procs,
  1440. gx_default_DevCMYK_get_color_comp_index);
  1441. }
  1442. /* Set the color_info and mapping functions for this instance of the device */
  1443. private int
  1444. display_set_color_format(gx_device_display *ddev, int nFormat)
  1445. {
  1446. gx_device * pdev = (gx_device *) ddev;
  1447. gx_device_color_info dci = ddev->color_info;
  1448. int bpc; /* bits per component */
  1449. int bpp; /* bits per pixel */
  1450. int maxvalue;
  1451. int align;
  1452. switch (nFormat & DISPLAY_DEPTH_MASK) {
  1453. case DISPLAY_DEPTH_1:
  1454. bpc = 1;
  1455. break;
  1456. case DISPLAY_DEPTH_2:
  1457. bpc = 2;
  1458. break;
  1459. case DISPLAY_DEPTH_4:
  1460. bpc = 4;
  1461. break;
  1462. case DISPLAY_DEPTH_8:
  1463. bpc = 8;
  1464. break;
  1465. case DISPLAY_DEPTH_12:
  1466. bpc = 12;
  1467. break;
  1468. case DISPLAY_DEPTH_16:
  1469. bpc = 16;
  1470. break;
  1471. default:
  1472. return_error(gs_error_rangecheck);
  1473. }
  1474. maxvalue = (1 << bpc) - 1;
  1475. ddev->devn_params.bitspercomponent = bpc;
  1476. switch (ddev->nFormat & DISPLAY_ROW_ALIGN_MASK) {
  1477. case DISPLAY_ROW_ALIGN_DEFAULT:
  1478. align = ARCH_ALIGN_PTR_MOD;
  1479. break;
  1480. case DISPLAY_ROW_ALIGN_4:
  1481. align = 4;
  1482. break;
  1483. case DISPLAY_ROW_ALIGN_8:
  1484. align = 8;
  1485. break;
  1486. case DISPLAY_ROW_ALIGN_16:
  1487. align = 16;
  1488. break;
  1489. case DISPLAY_ROW_ALIGN_32:
  1490. align = 32;
  1491. break;
  1492. case DISPLAY_ROW_ALIGN_64:
  1493. align = 64;
  1494. break;
  1495. default:
  1496. align = 0; /* not permitted */
  1497. }
  1498. if (align < ARCH_ALIGN_PTR_MOD)
  1499. return_error(gs_error_rangecheck);
  1500. switch (ddev->nFormat & DISPLAY_ALPHA_MASK) {
  1501. case DISPLAY_ALPHA_FIRST:
  1502. case DISPLAY_ALPHA_LAST:
  1503. /* Not implemented and unlikely to ever be implemented
  1504. * because they would interact with linear_and_separable
  1505. */
  1506. return_error(gs_error_rangecheck);
  1507. }
  1508. switch (nFormat & DISPLAY_COLORS_MASK) {
  1509. case DISPLAY_COLORS_NATIVE:
  1510. switch (nFormat & DISPLAY_DEPTH_MASK) {
  1511. case DISPLAY_DEPTH_1:
  1512. /* 1bit/pixel, black is 1, white is 0 */
  1513. set_color_info(&dci, DISPLAY_MODEL_GRAY, 1, 1, 1, 0);
  1514. dci.separable_and_linear = GX_CINFO_SEP_LIN_NONE;
  1515. set_gray_color_procs(pdev, gx_b_w_gray_encode,
  1516. gx_default_b_w_map_color_rgb);
  1517. break;
  1518. case DISPLAY_DEPTH_4:
  1519. /* 4bit/pixel VGA color */
  1520. set_color_info(&dci, DISPLAY_MODEL_RGB, 3, 4, 3, 2);
  1521. dci.separable_and_linear = GX_CINFO_SEP_LIN_NONE;
  1522. set_rgb_color_procs(pdev, display_map_rgb_color_device4,
  1523. display_map_color_rgb_device4);
  1524. break;
  1525. case DISPLAY_DEPTH_8:
  1526. /* 8bit/pixel 96 color palette */
  1527. set_color_info(&dci, DISPLAY_MODEL_RGBK, 4, 8, 31, 3);
  1528. dci.separable_and_linear = GX_CINFO_SEP_LIN_NONE;
  1529. set_rgbk_color_procs(pdev, display_encode_color_device8,
  1530. display_decode_color_device8);
  1531. break;
  1532. case DISPLAY_DEPTH_16:
  1533. /* Windows 16-bit display */
  1534. /* Is maxgray = maxcolor = 63 correct? */
  1535. if ((ddev->nFormat & DISPLAY_555_MASK)
  1536. == DISPLAY_NATIVE_555)
  1537. set_color_info(&dci, DISPLAY_MODEL_RGB, 3, 16, 31, 31);
  1538. else
  1539. set_color_info(&dci, DISPLAY_MODEL_RGB, 3, 16, 63, 63);
  1540. set_rgb_color_procs(pdev, display_map_rgb_color_device16,
  1541. display_map_color_rgb_device16);
  1542. break;
  1543. default:
  1544. return_error(gs_error_rangecheck);
  1545. }
  1546. dci.gray_index = GX_CINFO_COMP_NO_INDEX;
  1547. break;
  1548. case DISPLAY_COLORS_GRAY:
  1549. set_color_info(&dci, DISPLAY_MODEL_GRAY, 1, bpc, maxvalue, 0);
  1550. if (bpc == 1)
  1551. set_gray_color_procs(pdev, gx_default_gray_encode,
  1552. gx_default_w_b_map_color_rgb);
  1553. else
  1554. set_gray_color_procs(pdev, gx_default_gray_encode,
  1555. gx_default_gray_map_color_rgb);
  1556. break;
  1557. case DISPLAY_COLORS_RGB:
  1558. if ((nFormat & DISPLAY_ALPHA_MASK) == DISPLAY_ALPHA_NONE)
  1559. bpp = bpc * 3;
  1560. else
  1561. bpp = bpc * 4;
  1562. set_color_info(&dci, DISPLAY_MODEL_RGB, 3, bpp, maxvalue, maxvalue);
  1563. if (((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_8) &&
  1564. ((nFormat & DISPLAY_ALPHA_MASK) == DISPLAY_ALPHA_NONE)) {
  1565. if ((nFormat & DISPLAY_ENDIAN_MASK) == DISPLAY_BIGENDIAN)
  1566. set_rgb_color_procs(pdev, gx_default_rgb_map_rgb_color,
  1567. gx_default_rgb_map_color_rgb);
  1568. else
  1569. set_rgb_color_procs(pdev, display_map_rgb_color_bgr24,
  1570. display_map_color_rgb_bgr24);
  1571. }
  1572. else {
  1573. /* slower flexible functions for alpha/unused component */
  1574. set_rgb_color_procs(pdev, display_map_rgb_color_rgb,
  1575. display_map_color_rgb_rgb);
  1576. }
  1577. break;
  1578. case DISPLAY_COLORS_CMYK:
  1579. bpp = bpc * 4;
  1580. set_color_info(&dci, DISPLAY_MODEL_CMYK, 4, bpp, maxvalue, maxvalue);
  1581. if ((nFormat & DISPLAY_ALPHA_MASK) != DISPLAY_ALPHA_NONE)
  1582. return_error(gs_error_rangecheck);
  1583. if ((nFormat & DISPLAY_ENDIAN_MASK) != DISPLAY_BIGENDIAN)
  1584. return_error(gs_error_rangecheck);
  1585. if ((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_1)
  1586. set_cmyk_color_procs(pdev, cmyk_1bit_map_cmyk_color,
  1587. cmyk_1bit_map_color_cmyk);
  1588. else if ((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_8)
  1589. set_cmyk_color_procs(pdev, cmyk_8bit_map_cmyk_color,
  1590. cmyk_8bit_map_color_cmyk);
  1591. else
  1592. return_error(gs_error_rangecheck);
  1593. break;
  1594. case DISPLAY_COLORS_SEPARATION:
  1595. if ((nFormat & DISPLAY_ENDIAN_MASK) != DISPLAY_BIGENDIAN)
  1596. return_error(gs_error_rangecheck);
  1597. bpp = arch_sizeof_color_index * 8;
  1598. set_color_info(&dci, DISPLAY_MODEL_SEP, bpp/bpc, bpp,
  1599. maxvalue, maxvalue);
  1600. if ((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_8) {
  1601. ddev->devn_params.bitspercomponent = bpc;
  1602. set_color_procs(pdev,
  1603. display_separation_encode_color,
  1604. display_separation_decode_color,
  1605. display_separation_get_color_mapping_procs,
  1606. display_separation_get_color_comp_index);
  1607. }
  1608. else
  1609. return_error(gs_error_rangecheck);
  1610. break;
  1611. default:
  1612. return_error(gs_error_rangecheck);
  1613. }
  1614. /* restore old anti_alias info */
  1615. dci.anti_alias = ddev->color_info.anti_alias;
  1616. ddev->color_info = dci;
  1617. check_device_separable(pdev);
  1618. switch (nFormat & DISPLAY_COLORS_MASK) {
  1619. case DISPLAY_COLORS_NATIVE:
  1620. ddev->color_info.gray_index = GX_CINFO_COMP_NO_INDEX;
  1621. if ((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_1)
  1622. ddev->color_info.gray_index = 0;
  1623. else if ((nFormat & DISPLAY_DEPTH_MASK) == DISPLAY_DEPTH_8)
  1624. ddev->color_info.gray_index = 3;
  1625. break;
  1626. case DISPLAY_COLORS_RGB:
  1627. ddev->color_info.gray_index = GX_CINFO_COMP_NO_INDEX;
  1628. break;
  1629. case DISPLAY_COLORS_GRAY:
  1630. ddev->color_info.gray_index = 0;
  1631. break;
  1632. case DISPLAY_COLORS_CMYK:
  1633. ddev->color_info.gray_index = 3;
  1634. break;
  1635. case DISPLAY_COLORS_SEPARATION:
  1636. ddev->color_info.gray_index = GX_CINFO_COMP_NO_INDEX;
  1637. break;
  1638. }
  1639. ddev->nFormat = nFormat;
  1640. return 0;
  1641. }
  1642. /* ------ Begin Test Code ------ */
  1643. /*********************************************************************
  1644. typedef struct test_mode_s test_mode;
  1645. struct test_mode_s {
  1646. char *name;
  1647. unsigned int format;
  1648. };
  1649. test_mode test_modes[] = {
  1650. {"1bit/pixel native, black is 1, Windows",
  1651. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_1 |
  1652. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1653. {"4bit/pixel native, Windows VGA 16 color palette",
  1654. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_4 |
  1655. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1656. {"8bit/pixel native, Windows SVGA 96 color palette",
  1657. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1658. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1659. {"16bit/pixel native, Windows BGR555",
  1660. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_16 |
  1661. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST | DISPLAY_NATIVE_555},
  1662. {"16bit/pixel native, Windows BGR565",
  1663. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_16 |
  1664. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST | DISPLAY_NATIVE_565},
  1665. {"1bit/pixel gray, black is 0, topfirst",
  1666. DISPLAY_COLORS_GRAY | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_1 |
  1667. DISPLAY_BIGENDIAN | DISPLAY_TOPFIRST},
  1668. {"4bit/pixel gray, bottom first",
  1669. DISPLAY_COLORS_GRAY | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_4 |
  1670. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1671. {"8bit/pixel gray, bottom first",
  1672. DISPLAY_COLORS_GRAY | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1673. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1674. {"24bit/pixel color, bottom first, Windows BGR24",
  1675. DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1676. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1677. {"24bit/pixel color, bottom first, RGB24",
  1678. DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1679. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1680. {"24bit/pixel color, top first, GdkRgb RGB24",
  1681. DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1682. DISPLAY_BIGENDIAN | DISPLAY_TOPFIRST},
  1683. {"32bit/pixel color, top first, Macintosh xRGB",
  1684. DISPLAY_COLORS_RGB | DISPLAY_UNUSED_FIRST | DISPLAY_DEPTH_8 |
  1685. DISPLAY_BIGENDIAN | DISPLAY_TOPFIRST},
  1686. {"32bit/pixel color, bottom first, xBGR",
  1687. DISPLAY_COLORS_RGB | DISPLAY_UNUSED_FIRST | DISPLAY_DEPTH_8 |
  1688. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1689. {"32bit/pixel color, bottom first, Windows BGRx",
  1690. DISPLAY_COLORS_RGB | DISPLAY_UNUSED_LAST | DISPLAY_DEPTH_8 |
  1691. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST},
  1692. {"32bit/pixel color, bottom first, RGBx",
  1693. DISPLAY_COLORS_RGB | DISPLAY_UNUSED_LAST | DISPLAY_DEPTH_8 |
  1694. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1695. {"32bit/pixel CMYK, bottom first",
  1696. DISPLAY_COLORS_CMYK | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1697. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1698. {"64bit/pixel separations, bottom first",
  1699. DISPLAY_COLORS_SEPARATIONS | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1700. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1701. {"4bit/pixel CMYK, bottom first",
  1702. DISPLAY_COLORS_CMYK | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_1 |
  1703. DISPLAY_BIGENDIAN | DISPLAY_BOTTOMFIRST},
  1704. {"1bit/pixel native, black is 1, 8 byte alignment",
  1705. DISPLAY_COLORS_NATIVE | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_1 |
  1706. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST | DISPLAY_ROW_ALIGN_8},
  1707. {"24bit/pixel color, bottom first, BGR24, 64 byte alignment",
  1708. DISPLAY_COLORS_RGB | DISPLAY_ALPHA_NONE | DISPLAY_DEPTH_8 |
  1709. DISPLAY_LITTLEENDIAN | DISPLAY_BOTTOMFIRST | DISPLAY_ROW_ALIGN_64}
  1710. };
  1711. void
  1712. test(int index)
  1713. {
  1714. char buf[1024];
  1715. sprintf(buf, "gs -dDisplayFormat=16#%x examples/colorcir.ps -c quit", test_modes[index].format);
  1716. system(buf);
  1717. }
  1718. int main(int argc, char *argv[])
  1719. {
  1720. int i;
  1721. int dotest = 0;
  1722. if (argc >=2) {
  1723. if (strcmp(argv[1], "-t") == 0)
  1724. dotest = 1;
  1725. else {
  1726. fprintf(stdout, "To show modes: disp\nTo run test: disp -t\n");
  1727. return 1;
  1728. }
  1729. }
  1730. for (i=0; i < sizeof(test_modes)/sizeof(test_mode); i++) {
  1731. fprintf(stdout, "16#%x or %d: %s\n", test_modes[i].format,
  1732. test_modes[i].format, test_modes[i].name);
  1733. if (dotest)
  1734. test(i);
  1735. }
  1736. return 0;
  1737. }
  1738. *********************************************************************/
  1739. /* ------ End Test Code ------ */