gdevdflt.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /* Copyright (C) 1995, 2000 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: gdevdflt.c,v 1.25 2005/03/14 18:08:36 dan Exp $ */
  14. /* Default device implementation */
  15. #include "math_.h"
  16. #include "gx.h"
  17. #include "gserrors.h"
  18. #include "gsropt.h"
  19. #include "gxcomp.h"
  20. #include "gxdevice.h"
  21. /* ---------------- Default device procedures ---------------- */
  22. /*
  23. * Set a color model polarity to be additive or subtractive. In either
  24. * case, indicate an error (and don't modify the polarity) if the current
  25. * setting differs from the desired and is not GX_CINFO_POLARITY_UNKNOWN.
  26. */
  27. static void
  28. set_cinfo_polarity(gx_device * dev, gx_color_polarity_t new_polarity)
  29. {
  30. #ifdef DEBUG
  31. /* sanity check */
  32. if (new_polarity == GX_CINFO_POLARITY_UNKNOWN) {
  33. dprintf("set_cinfo_polarity: illegal operand");
  34. return;
  35. }
  36. #endif
  37. /*
  38. * The meory devices assume that single color devices are gray.
  39. * This may not be true if SeparationOrder is specified. Thus only
  40. * change the value if the current value is unknown.
  41. */
  42. if (dev->color_info.polarity == GX_CINFO_POLARITY_UNKNOWN)
  43. dev->color_info.polarity = new_polarity;
  44. }
  45. private gx_color_index
  46. (*get_encode_color(gx_device *dev))(gx_device *, const gx_color_value *)
  47. {
  48. dev_proc_encode_color(*encode_proc);
  49. /* use encode_color if it has been provided */
  50. if ((encode_proc = dev_proc(dev, encode_color)) == 0) {
  51. if (dev->color_info.num_components == 1 &&
  52. dev_proc(dev, map_rgb_color) != 0) {
  53. set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
  54. encode_proc = gx_backwards_compatible_gray_encode;
  55. } else if ( (dev->color_info.num_components == 3 ) &&
  56. (encode_proc = dev_proc(dev, map_rgb_color)) != 0 )
  57. set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
  58. else if ( dev->color_info.num_components == 4 &&
  59. (encode_proc = dev_proc(dev, map_cmyk_color)) != 0 )
  60. set_cinfo_polarity(dev, GX_CINFO_POLARITY_SUBTRACTIVE);
  61. }
  62. /*
  63. * If no encode_color procedure at this point, the color model had
  64. * better be monochrome (though not necessarily bi-level). In this
  65. * case, it is assumed to be additive, as that is consistent with
  66. * the pre-DeviceN code.
  67. *
  68. * If this is not the case, then the color model had better be known
  69. * to be separable and linear, for there is no other way to derive
  70. * an encoding. This is the case even for weakly linear and separable
  71. * color models with a known polarity.
  72. */
  73. if (encode_proc == 0) {
  74. if (dev->color_info.num_components == 1 && dev->color_info.depth != 0) {
  75. set_cinfo_polarity(dev, GX_CINFO_POLARITY_ADDITIVE);
  76. if (dev->color_info.max_gray == (1 << dev->color_info.depth) - 1)
  77. encode_proc = gx_default_gray_fast_encode;
  78. else
  79. encode_proc = gx_default_gray_encode;
  80. dev->color_info.separable_and_linear = GX_CINFO_SEP_LIN;
  81. } else if (dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN) {
  82. gx_color_value max_gray = dev->color_info.max_gray;
  83. gx_color_value max_color = dev->color_info.max_color;
  84. if ( (max_gray & (max_gray + 1)) == 0 &&
  85. (max_color & (max_color + 1)) == 0 )
  86. /* NB should be gx_default_fast_encode_color */
  87. encode_proc = gx_default_encode_color;
  88. else
  89. encode_proc = gx_default_encode_color;
  90. } else
  91. dprintf("get_encode_color: no color encoding information");
  92. }
  93. return encode_proc;
  94. }
  95. /*
  96. * Determine if a color model has the properties of a DeviceRGB
  97. * color model. This procedure is, in all likelihood, high-grade
  98. * overkill, but since this is not a performance sensitive area
  99. * no harm is done.
  100. *
  101. * Since there is little benefit to checking the values 0, 1, or
  102. * 1/2, we use the values 1/4, 1/3, and 3/4 in their place. We
  103. * compare the results to see if the intensities match to within
  104. * a tolerance of .01, which is arbitrarily selected.
  105. */
  106. private bool
  107. is_like_DeviceRGB(gx_device * dev)
  108. {
  109. const gx_cm_color_map_procs * cm_procs;
  110. frac cm_comp_fracs[3];
  111. int i;
  112. if ( dev->color_info.num_components != 3 ||
  113. dev->color_info.polarity != GX_CINFO_POLARITY_ADDITIVE )
  114. return false;
  115. cm_procs = dev_proc(dev, get_color_mapping_procs)(dev);
  116. if (cm_procs == 0 || cm_procs->map_rgb == 0)
  117. return false;
  118. /* check the values 1/4, 1/3, and 3/4 */
  119. cm_procs->map_rgb( dev,
  120. 0,
  121. frac_1 / 4,
  122. frac_1 / 3,
  123. 3 * frac_1 / 4,
  124. cm_comp_fracs );
  125. /* verify results to .01 */
  126. cm_comp_fracs[0] -= frac_1 / 4;
  127. cm_comp_fracs[1] -= frac_1 / 3;
  128. cm_comp_fracs[2] -= 3 * frac_1 / 4;
  129. for ( i = 0;
  130. i < 3 &&
  131. -frac_1 / 100 < cm_comp_fracs[i] &&
  132. cm_comp_fracs[i] < frac_1 / 100;
  133. i++ )
  134. ;
  135. return i == 3;
  136. }
  137. /*
  138. * Similar to is_like_DeviceRGB, but for DeviceCMYK.
  139. */
  140. private bool
  141. is_like_DeviceCMYK(gx_device * dev)
  142. {
  143. const gx_cm_color_map_procs * cm_procs;
  144. frac cm_comp_fracs[4];
  145. int i;
  146. if ( dev->color_info.num_components != 4 ||
  147. dev->color_info.polarity != GX_CINFO_POLARITY_SUBTRACTIVE )
  148. return false;
  149. cm_procs = dev_proc(dev, get_color_mapping_procs)(dev);
  150. if (cm_procs == 0 || cm_procs->map_cmyk == 0)
  151. return false;
  152. /* check the values 1/4, 1/3, 3/4, and 1/8 */
  153. cm_procs->map_cmyk( dev,
  154. frac_1 / 4,
  155. frac_1 / 3,
  156. 3 * frac_1 / 4,
  157. frac_1 / 8,
  158. cm_comp_fracs );
  159. /* verify results to .01 */
  160. cm_comp_fracs[0] -= frac_1 / 4;
  161. cm_comp_fracs[1] -= frac_1 / 3;
  162. cm_comp_fracs[2] -= 3 * frac_1 / 4;
  163. cm_comp_fracs[3] -= frac_1 / 8;
  164. for ( i = 0;
  165. i < 4 &&
  166. -frac_1 / 100 < cm_comp_fracs[i] &&
  167. cm_comp_fracs[i] < frac_1 / 100;
  168. i++ )
  169. ;
  170. return i == 4;
  171. }
  172. /*
  173. * Two default decode_color procedures to use for monochrome devices.
  174. * These will make use of the map_color_rgb routine, and use the first
  175. * component of the returned value or its inverse.
  176. */
  177. private int
  178. gx_default_1_add_decode_color(
  179. gx_device * dev,
  180. gx_color_index color,
  181. gx_color_value cv[1] )
  182. {
  183. gx_color_value rgb[3];
  184. int code = dev_proc(dev, map_color_rgb)(dev, color, rgb);
  185. cv[0] = rgb[0];
  186. return code;
  187. }
  188. private int
  189. gx_default_1_sub_decode_color(
  190. gx_device * dev,
  191. gx_color_index color,
  192. gx_color_value cv[1] )
  193. {
  194. gx_color_value rgb[3];
  195. int code = dev_proc(dev, map_color_rgb)(dev, color, rgb);
  196. cv[0] = gx_max_color_value - rgb[0];
  197. return code;
  198. }
  199. /*
  200. * A default decode_color procedure for DeviceCMYK color models.
  201. *
  202. * There is no generally accurate way of decode a DeviceCMYK color using
  203. * the map_color_rgb method. Unfortunately, there are many older devices
  204. * employ the DeviceCMYK color model but don't provide a decode_color
  205. * method. The code below works on the assumption of full undercolor
  206. * removal and black generation. This may not be accurate, but is the
  207. * best that can be done in the general case without other information.
  208. */
  209. private int
  210. gx_default_cmyk_decode_color(
  211. gx_device * dev,
  212. gx_color_index color,
  213. gx_color_value cv[4] )
  214. {
  215. /* The device may have been determined to be 'separable'. */
  216. if (dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN)
  217. return gx_default_decode_color(dev, color, cv);
  218. else {
  219. int i, code = dev_proc(dev, map_color_rgb)(dev, color, cv);
  220. gx_color_value min_val = gx_max_color_value;
  221. for (i = 0; i < 3; i++) {
  222. if ((cv[i] = gx_max_color_value - cv[i]) < min_val)
  223. min_val = cv[i];
  224. }
  225. for (i = 0; i < 3; i++)
  226. cv[i] -= min_val;
  227. cv[3] = min_val;
  228. return code;
  229. }
  230. }
  231. /*
  232. * Special case default color decode routine for a canonical 1-bit per
  233. * component DeviceCMYK color model.
  234. */
  235. private int
  236. gx_1bit_cmyk_decode_color(
  237. gx_device * dev,
  238. gx_color_index color,
  239. gx_color_value cv[4] )
  240. {
  241. cv[0] = ((color & 0x8) != 0 ? gx_max_color_value : 0);
  242. cv[1] = ((color & 0x4) != 0 ? gx_max_color_value : 0);
  243. cv[2] = ((color & 0x2) != 0 ? gx_max_color_value : 0);
  244. cv[3] = ((color & 0x1) != 0 ? gx_max_color_value : 0);
  245. return 0;
  246. }
  247. private int
  248. (*get_decode_color(gx_device * dev))(gx_device *, gx_color_index, gx_color_value *)
  249. {
  250. /* if a method has already been provided, use it */
  251. if (dev_proc(dev, decode_color) != 0)
  252. return dev_proc(dev, decode_color);
  253. /*
  254. * If a map_color_rgb method has been provided, we may be able to use it.
  255. * Currently this will always be the case, as a default value will be
  256. * provided this method. While this default may not be correct, we are not
  257. * introducing any new errors by using it.
  258. */
  259. if (dev_proc(dev, map_color_rgb) != 0) {
  260. /* if the device has a DeviceRGB color model, use map_color_rgb */
  261. if (is_like_DeviceRGB(dev))
  262. return dev_proc(dev, map_color_rgb);
  263. /* If separable ande linear then use default */
  264. if ( dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN )
  265. return &gx_default_decode_color;
  266. /* gray devices can be handled based on their polarity */
  267. if ( dev->color_info.num_components == 1 &&
  268. dev->color_info.gray_index == 0 )
  269. return dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE
  270. ? &gx_default_1_add_decode_color
  271. : &gx_default_1_sub_decode_color;
  272. /*
  273. * There is no accurate way to decode colors for cmyk devices
  274. * using the map_color_rgb procedure. Unfortunately, this cases
  275. * arises with some frequency, so it is useful not to generate an
  276. * error in this case. The mechanism below assumes full undercolor
  277. * removal and black generation, which may not be accurate but are
  278. * the best that can be done in the general case in the absence of
  279. * other information.
  280. *
  281. * As a hack to handle certain common devices, if the map_rgb_color
  282. * routine is cmyk_1bit_map_color_rgb, we provide a direct one-bit
  283. * decoder.
  284. */
  285. if (is_like_DeviceCMYK(dev)) {
  286. if (dev_proc(dev, map_color_rgb) == cmyk_1bit_map_color_rgb)
  287. return &gx_1bit_cmyk_decode_color;
  288. else
  289. return &gx_default_cmyk_decode_color;
  290. }
  291. }
  292. /*
  293. * The separable and linear case will already have been handled by
  294. * code in gx_device_fill_in_procs, so at this point we can only hope
  295. * the device doesn't use the decode_color method.
  296. */
  297. if (dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN )
  298. return &gx_default_decode_color;
  299. else
  300. return &gx_error_decode_color;
  301. }
  302. /*
  303. * If a device has a linear and separable encode color function then
  304. * set up the comp_bits, comp_mask, and comp_shift fields. Note: This
  305. * routine assumes that the colorant shift factor decreases with the
  306. * component number. See check_device_separable() for a general routine.
  307. */
  308. void
  309. set_linear_color_bits_mask_shift(gx_device * dev)
  310. {
  311. int i;
  312. byte gray_index = dev->color_info.gray_index;
  313. gx_color_value max_gray = dev->color_info.max_gray;
  314. gx_color_value max_color = dev->color_info.max_color;
  315. int num_components = dev->color_info.num_components;
  316. #define comp_bits (dev->color_info.comp_bits)
  317. #define comp_mask (dev->color_info.comp_mask)
  318. #define comp_shift (dev->color_info.comp_shift)
  319. comp_shift[num_components - 1] = 0;
  320. for ( i = num_components - 1 - 1; i >= 0; i-- ) {
  321. comp_shift[i] = comp_shift[i + 1] +
  322. ( i == gray_index ? ilog2(max_gray + 1) : ilog2(max_color + 1) );
  323. }
  324. for ( i = 0; i < num_components; i++ ) {
  325. comp_bits[i] = ( i == gray_index ?
  326. ilog2(max_gray + 1) :
  327. ilog2(max_color + 1) );
  328. comp_mask[i] = (((gx_color_index)1 << comp_bits[i]) - 1)
  329. << comp_shift[i];
  330. }
  331. #undef comp_bits
  332. #undef comp_mask
  333. #undef comp_shift
  334. }
  335. /* Determine if a number is a power of two. Works only for integers. */
  336. #define is_power_of_two(x) ((((x) - 1) & (x)) == 0)
  337. /*
  338. * This routine attempts to determine if a device's encode_color procedure
  339. * produces gx_color_index values which are 'separable'. A 'separable' value
  340. * means two things. Each colorant has a group of bits in the gx_color_index
  341. * value which is associated with the colorant. These bits are separate.
  342. * I.e. no bit is associated with more than one colorant. If a colorant has
  343. * a value of zero then the bits associated with that colorant are zero.
  344. * These criteria allows the graphics library to build gx_color_index values
  345. * from the colorant values and not using the encode_color routine. This is
  346. * useful and necessary for overprinting, the WTS screeening, halftoning more
  347. * than four colorants, and the fast shading logic. However this information
  348. * is not setup by the default device macros. Thus we attempt to derive this
  349. * information.
  350. *
  351. * This routine can be fooled. However it usually errors on the side of
  352. * assuing that a device is not separable. In this case it does not create
  353. * any new problems. In theory it can be fooled into believing that a device
  354. * is separable when it is not. However we do not know of any real cases that
  355. * will fool it.
  356. */
  357. void
  358. check_device_separable(gx_device * dev)
  359. {
  360. int i, j;
  361. gx_device_color_info * pinfo = &(dev->color_info);
  362. int num_components = pinfo->num_components;
  363. byte comp_shift[GX_DEVICE_COLOR_MAX_COMPONENTS];
  364. byte comp_bits[GX_DEVICE_COLOR_MAX_COMPONENTS];
  365. gx_color_index comp_mask[GX_DEVICE_COLOR_MAX_COMPONENTS];
  366. gx_color_index color_index;
  367. gx_color_index current_bits = 0;
  368. gx_color_value colorants[GX_DEVICE_COLOR_MAX_COMPONENTS] = { 0 };
  369. /* If this is already known then we do not need to do anything. */
  370. if (pinfo->separable_and_linear != GX_CINFO_UNKNOWN_SEP_LIN)
  371. return;
  372. /* If there is not an encode_color_routine then we cannot proceed. */
  373. if (dev_proc(dev, encode_color) == NULL)
  374. return;
  375. /*
  376. * If these values do not check then we should have an error. However
  377. * we do not know what to do so we are simply exitting and hoping that
  378. * the device will clean up its values.
  379. */
  380. if (pinfo->gray_index < num_components &&
  381. (!pinfo->dither_grays || pinfo->dither_grays != (pinfo->max_gray + 1)))
  382. return;
  383. if ((num_components > 1 || pinfo->gray_index != 0) &&
  384. (!pinfo->dither_colors || pinfo->dither_colors != (pinfo->max_color + 1)))
  385. return;
  386. /*
  387. * If dither_grays or dither_colors is not a power of two then we assume
  388. * that the device is not separable. In theory this not a requirement
  389. * but it has been true for all of the devices that we have seen so far.
  390. * This assumption also makes the logic in the next section easier.
  391. */
  392. if (!is_power_of_two(pinfo->dither_grays)
  393. || !is_power_of_two(pinfo->dither_colors))
  394. return;
  395. /*
  396. * Use the encode_color routine to try to verify that the device is
  397. * separable and to determine the shift count, etc. for each colorant.
  398. */
  399. color_index = dev_proc(dev, encode_color)(dev, colorants);
  400. if (color_index != 0)
  401. return; /* Exit if zero colorants produce a non zero index */
  402. for (i = 0; i < num_components; i++) {
  403. /* Check this colorant = max with all others = 0 */
  404. for (j = 0; j < num_components; j++)
  405. colorants[j] = 0;
  406. colorants[i] = gx_max_color_value;
  407. color_index = dev_proc(dev, encode_color)(dev, colorants);
  408. if (color_index == 0) /* If no bits then we have a problem */
  409. return;
  410. if (color_index & current_bits) /* Check for overlapping bits */
  411. return;
  412. current_bits |= color_index;
  413. comp_mask[i] = color_index;
  414. /* Determine the shift count for the colorant */
  415. for (j = 0; (color_index & 1) == 0 && color_index != 0; j++)
  416. color_index >>= 1;
  417. comp_shift[i] = j;
  418. /* Determine the bit count for the colorant */
  419. for (j = 0; color_index != 0; j++) {
  420. if ((color_index & 1) == 0) /* check for non-consecutive bits */
  421. return;
  422. color_index >>= 1;
  423. }
  424. comp_bits[i] = j;
  425. /*
  426. * We could verify that the bit count matches the dither_grays or
  427. * dither_colors values, but this is not really required unless we
  428. * are halftoning. Thus we are allowing for non equal colorant sizes.
  429. */
  430. /* Check for overlap with other colorant if they are all maxed */
  431. for (j = 0; j < num_components; j++)
  432. colorants[j] = gx_max_color_value;
  433. colorants[i] = 0;
  434. color_index = dev_proc(dev, encode_color)(dev, colorants);
  435. if (color_index & comp_mask[i]) /* Check for overlapping bits */
  436. return;
  437. }
  438. /* If we get to here then the device is very likely to be separable. */
  439. pinfo->separable_and_linear = GX_CINFO_SEP_LIN;
  440. for (i = 0; i < num_components; i++) {
  441. pinfo->comp_shift[i] = comp_shift[i];
  442. pinfo->comp_bits[i] = comp_bits[i];
  443. pinfo->comp_mask[i] = comp_mask[i];
  444. }
  445. /*
  446. * The 'gray_index' value allows one colorant to have a different number
  447. * of shades from the remainder. Since the default macros only guess at
  448. * an appropriate value, we are setting its value based upon the data that
  449. * we just determined. Note: In some cases the macros set max_gray to 0
  450. * and dither_grays to 1. This is not valid so ignore this case.
  451. */
  452. for (i = 0; i < num_components; i++) {
  453. int dither = 1 << comp_bits[i];
  454. if (pinfo->dither_grays != 1 && dither == pinfo->dither_grays) {
  455. pinfo->gray_index = i;
  456. break;
  457. }
  458. }
  459. }
  460. #undef is_power_of_two
  461. /* Fill in NULL procedures in a device procedure record. */
  462. void
  463. gx_device_fill_in_procs(register gx_device * dev)
  464. {
  465. gx_device_set_procs(dev);
  466. fill_dev_proc(dev, open_device, gx_default_open_device);
  467. fill_dev_proc(dev, get_initial_matrix, gx_default_get_initial_matrix);
  468. fill_dev_proc(dev, sync_output, gx_default_sync_output);
  469. fill_dev_proc(dev, output_page, gx_default_output_page);
  470. fill_dev_proc(dev, close_device, gx_default_close_device);
  471. /* see below for map_rgb_color */
  472. fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
  473. /* NOT fill_rectangle */
  474. fill_dev_proc(dev, tile_rectangle, gx_default_tile_rectangle);
  475. fill_dev_proc(dev, copy_mono, gx_default_copy_mono);
  476. fill_dev_proc(dev, copy_color, gx_default_copy_color);
  477. fill_dev_proc(dev, obsolete_draw_line, gx_default_draw_line);
  478. fill_dev_proc(dev, get_bits, gx_default_get_bits);
  479. fill_dev_proc(dev, get_params, gx_default_get_params);
  480. fill_dev_proc(dev, put_params, gx_default_put_params);
  481. /* see below for map_cmyk_color */
  482. fill_dev_proc(dev, get_xfont_procs, gx_default_get_xfont_procs);
  483. fill_dev_proc(dev, get_xfont_device, gx_default_get_xfont_device);
  484. fill_dev_proc(dev, map_rgb_alpha_color, gx_default_map_rgb_alpha_color);
  485. fill_dev_proc(dev, get_page_device, gx_default_get_page_device);
  486. fill_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
  487. fill_dev_proc(dev, copy_alpha, gx_default_copy_alpha);
  488. fill_dev_proc(dev, get_band, gx_default_get_band);
  489. fill_dev_proc(dev, copy_rop, gx_default_copy_rop);
  490. fill_dev_proc(dev, fill_path, gx_default_fill_path);
  491. fill_dev_proc(dev, stroke_path, gx_default_stroke_path);
  492. fill_dev_proc(dev, fill_mask, gx_default_fill_mask);
  493. fill_dev_proc(dev, fill_trapezoid, gx_default_fill_trapezoid);
  494. fill_dev_proc(dev, fill_parallelogram, gx_default_fill_parallelogram);
  495. fill_dev_proc(dev, fill_triangle, gx_default_fill_triangle);
  496. fill_dev_proc(dev, draw_thin_line, gx_default_draw_thin_line);
  497. fill_dev_proc(dev, begin_image, gx_default_begin_image);
  498. /*
  499. * We always replace get_alpha_bits, image_data, and end_image with the
  500. * new procedures, and, if in a DEBUG configuration, print a warning if
  501. * the definitions aren't the default ones.
  502. */
  503. #ifdef DEBUG
  504. # define CHECK_NON_DEFAULT(proc, default, procname)\
  505. BEGIN\
  506. if ( dev_proc(dev, proc) != NULL && dev_proc(dev, proc) != default )\
  507. dprintf2("**** Warning: device %s implements obsolete procedure %s\n",\
  508. dev->dname, procname);\
  509. END
  510. #else
  511. # define CHECK_NON_DEFAULT(proc, default, procname)\
  512. DO_NOTHING
  513. #endif
  514. CHECK_NON_DEFAULT(get_alpha_bits, gx_default_get_alpha_bits,
  515. "get_alpha_bits");
  516. set_dev_proc(dev, get_alpha_bits, gx_default_get_alpha_bits);
  517. CHECK_NON_DEFAULT(image_data, gx_default_image_data, "image_data");
  518. set_dev_proc(dev, image_data, gx_default_image_data);
  519. CHECK_NON_DEFAULT(end_image, gx_default_end_image, "end_image");
  520. set_dev_proc(dev, end_image, gx_default_end_image);
  521. #undef CHECK_NON_DEFAULT
  522. fill_dev_proc(dev, strip_tile_rectangle, gx_default_strip_tile_rectangle);
  523. fill_dev_proc(dev, strip_copy_rop, gx_default_strip_copy_rop);
  524. fill_dev_proc(dev, get_clipping_box, gx_default_get_clipping_box);
  525. fill_dev_proc(dev, begin_typed_image, gx_default_begin_typed_image);
  526. fill_dev_proc(dev, get_bits_rectangle, gx_default_get_bits_rectangle);
  527. fill_dev_proc(dev, map_color_rgb_alpha, gx_default_map_color_rgb_alpha);
  528. fill_dev_proc(dev, create_compositor, gx_default_create_compositor);
  529. fill_dev_proc(dev, get_hardware_params, gx_default_get_hardware_params);
  530. fill_dev_proc(dev, text_begin, gx_default_text_begin);
  531. fill_dev_proc(dev, finish_copydevice, gx_default_finish_copydevice);
  532. set_dev_proc(dev, encode_color, get_encode_color(dev));
  533. if (dev->color_info.num_components == 3)
  534. set_dev_proc(dev, map_rgb_color, dev_proc(dev, encode_color));
  535. if (dev->color_info.num_components == 4)
  536. set_dev_proc(dev, map_cmyk_color, dev_proc(dev, encode_color));
  537. if ( dev->color_info.separable_and_linear == GX_CINFO_SEP_LIN ) {
  538. fill_dev_proc(dev, encode_color, gx_default_encode_color);
  539. fill_dev_proc(dev, map_cmyk_color, gx_default_encode_color);
  540. fill_dev_proc(dev, map_rgb_color, gx_default_encode_color);
  541. } else {
  542. /* if it isn't set now punt */
  543. fill_dev_proc(dev, encode_color, gx_error_encode_color);
  544. fill_dev_proc(dev, map_cmyk_color, gx_error_encode_color);
  545. fill_dev_proc(dev, map_rgb_color, gx_error_encode_color);
  546. }
  547. /*
  548. * Fill in the color mapping procedures and the component index
  549. * assignment procedure if they have not been provided by the client.
  550. *
  551. * Because it is difficult to provide default encoding procedures
  552. * that handle level inversion, this code needs to check both
  553. * the number of components and the polarity of color model.
  554. */
  555. switch (dev->color_info.num_components) {
  556. case 1: /* DeviceGray or DeviceInvertGray */
  557. if (dev_proc(dev, get_color_mapping_procs) == NULL) {
  558. /*
  559. * If not gray then the device must provide the color
  560. * mapping procs.
  561. */
  562. if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
  563. fill_dev_proc( dev,
  564. get_color_mapping_procs,
  565. gx_default_DevGray_get_color_mapping_procs );
  566. }
  567. }
  568. fill_dev_proc( dev,
  569. get_color_comp_index,
  570. gx_default_DevGray_get_color_comp_index );
  571. break;
  572. case 3:
  573. if (dev_proc(dev, get_color_mapping_procs) == NULL) {
  574. if (dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE) {
  575. fill_dev_proc( dev,
  576. get_color_mapping_procs,
  577. gx_default_DevRGB_get_color_mapping_procs );
  578. fill_dev_proc( dev,
  579. get_color_comp_index,
  580. gx_default_DevRGB_get_color_comp_index );
  581. } else {
  582. #if 0
  583. fill_dev_proc( dev,
  584. get_color_mapping_procs,
  585. gx_default_DevCMY_get_color_mapping_procs );
  586. fill_dev_proc( dev,
  587. get_color_comp_index,
  588. gx_default_DevCMY_get_color_comp_index );
  589. #endif
  590. }
  591. }
  592. break;
  593. case 4:
  594. fill_dev_proc(dev, get_color_mapping_procs, gx_default_DevCMYK_get_color_mapping_procs);
  595. fill_dev_proc(dev, get_color_comp_index, gx_default_DevCMYK_get_color_comp_index);
  596. break;
  597. default: /* Unknown color model - set error handlers */
  598. fill_dev_proc(dev, get_color_mapping_procs, gx_error_get_color_mapping_procs);
  599. fill_dev_proc(dev, get_color_comp_index, gx_error_get_color_comp_index);
  600. }
  601. set_dev_proc(dev, decode_color, get_decode_color(dev));
  602. fill_dev_proc(dev, map_color_rgb, gx_default_map_color_rgb);
  603. /*
  604. * If the device is known not to support overprint mode, indicate this now.
  605. * Note that we do not insist that a device be use a strict DeviceCMYK
  606. * encoding; any color model that is subtractive and supports the cyan,
  607. * magenta, yellow, and black color components will do. We defer a more
  608. * explicit check until this information is explicitly required.
  609. */
  610. if ( dev->color_info.opmode == GX_CINFO_OPMODE_UNKNOWN &&
  611. (dev->color_info.num_components < 4 ||
  612. dev->color_info.polarity == GX_CINFO_POLARITY_ADDITIVE ||
  613. dev->color_info.gray_index == GX_CINFO_COMP_NO_INDEX ) )
  614. dev->color_info.opmode = GX_CINFO_OPMODE_NOT;
  615. fill_dev_proc(dev, pattern_manage, gx_default_pattern_manage);
  616. fill_dev_proc(dev, fill_rectangle_hl_color, gx_default_fill_rectangle_hl_color);
  617. fill_dev_proc(dev, include_color_space, gx_default_include_color_space);
  618. fill_dev_proc(dev, fill_linear_color_scanline, gx_default_fill_linear_color_scanline);
  619. fill_dev_proc(dev, fill_linear_color_trapezoid, gx_default_fill_linear_color_trapezoid);
  620. fill_dev_proc(dev, fill_linear_color_triangle, gx_default_fill_linear_color_triangle);
  621. fill_dev_proc(dev, update_spot_equivalent_colors, gx_default_update_spot_equivalent_colors);
  622. }
  623. int
  624. gx_default_open_device(gx_device * dev)
  625. {
  626. /* Initialize the separable status if not known. */
  627. check_device_separable(dev);
  628. return 0;
  629. }
  630. /* Get the initial matrix for a device with inverted Y. */
  631. /* This includes essentially all printers and displays. */
  632. void
  633. gx_default_get_initial_matrix(gx_device * dev, register gs_matrix * pmat)
  634. {
  635. pmat->xx = dev->HWResolution[0] / 72.0; /* x_pixels_per_inch */
  636. pmat->xy = 0;
  637. pmat->yx = 0;
  638. pmat->yy = dev->HWResolution[1] / -72.0; /* y_pixels_per_inch */
  639. /****** tx/y is WRONG for devices with ******/
  640. /****** arbitrary initial matrix ******/
  641. pmat->tx = 0;
  642. pmat->ty = (float)dev->height;
  643. }
  644. /* Get the initial matrix for a device with upright Y. */
  645. /* This includes just a few printers and window systems. */
  646. void
  647. gx_upright_get_initial_matrix(gx_device * dev, register gs_matrix * pmat)
  648. {
  649. pmat->xx = dev->HWResolution[0] / 72.0; /* x_pixels_per_inch */
  650. pmat->xy = 0;
  651. pmat->yx = 0;
  652. pmat->yy = dev->HWResolution[1] / 72.0; /* y_pixels_per_inch */
  653. /****** tx/y is WRONG for devices with ******/
  654. /****** arbitrary initial matrix ******/
  655. pmat->tx = 0;
  656. pmat->ty = 0;
  657. }
  658. int
  659. gx_default_sync_output(gx_device * dev)
  660. {
  661. return 0;
  662. }
  663. int
  664. gx_default_output_page(gx_device * dev, int num_copies, int flush)
  665. {
  666. int code = dev_proc(dev, sync_output)(dev);
  667. if (code >= 0)
  668. code = gx_finish_output_page(dev, num_copies, flush);
  669. return code;
  670. }
  671. int
  672. gx_default_close_device(gx_device * dev)
  673. {
  674. return 0;
  675. }
  676. const gx_xfont_procs *
  677. gx_default_get_xfont_procs(gx_device * dev)
  678. {
  679. return NULL;
  680. }
  681. gx_device *
  682. gx_default_get_xfont_device(gx_device * dev)
  683. {
  684. return dev;
  685. }
  686. gx_device *
  687. gx_default_get_page_device(gx_device * dev)
  688. {
  689. return NULL;
  690. }
  691. gx_device *
  692. gx_page_device_get_page_device(gx_device * dev)
  693. {
  694. return dev;
  695. }
  696. int
  697. gx_default_get_alpha_bits(gx_device * dev, graphics_object_type type)
  698. {
  699. return (type == go_text ? dev->color_info.anti_alias.text_bits :
  700. dev->color_info.anti_alias.graphics_bits);
  701. }
  702. int
  703. gx_default_get_band(gx_device * dev, int y, int *band_start)
  704. {
  705. return 0;
  706. }
  707. void
  708. gx_default_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  709. {
  710. pbox->p.x = 0;
  711. pbox->p.y = 0;
  712. pbox->q.x = int2fixed(dev->width);
  713. pbox->q.y = int2fixed(dev->height);
  714. }
  715. void
  716. gx_get_largest_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  717. {
  718. pbox->p.x = min_fixed;
  719. pbox->p.y = min_fixed;
  720. pbox->q.x = max_fixed;
  721. pbox->q.y = max_fixed;
  722. }
  723. int
  724. gx_no_create_compositor(gx_device * dev, gx_device ** pcdev,
  725. const gs_composite_t * pcte,
  726. gs_imager_state * pis, gs_memory_t * memory)
  727. {
  728. return_error(gs_error_unknownerror); /* not implemented */
  729. }
  730. int
  731. gx_default_create_compositor(gx_device * dev, gx_device ** pcdev,
  732. const gs_composite_t * pcte,
  733. gs_imager_state * pis, gs_memory_t * memory)
  734. {
  735. return pcte->type->procs.create_default_compositor
  736. (pcte, pcdev, dev, pis, memory);
  737. }
  738. int
  739. gx_null_create_compositor(gx_device * dev, gx_device ** pcdev,
  740. const gs_composite_t * pcte,
  741. gs_imager_state * pis, gs_memory_t * memory)
  742. {
  743. *pcdev = dev;
  744. return 0;
  745. }
  746. /*
  747. * Default handler for creating a compositor device when writing the clist. */
  748. int
  749. gx_default_composite_clist_write_update(const gs_composite_t *pcte, gx_device * dev,
  750. gx_device ** pcdev, gs_imager_state * pis, gs_memory_t * mem)
  751. {
  752. *pcdev = dev; /* Do nothing -> return the same device */
  753. return 0;
  754. }
  755. /*
  756. * Default handler for updating the clist device when reading a compositing
  757. * device.
  758. */
  759. int
  760. gx_default_composite_clist_read_update(gs_composite_t *pxcte, gx_device * cdev,
  761. gx_device * tdev, gs_imager_state * pis, gs_memory_t * mem)
  762. {
  763. return 0; /* Do nothing */
  764. }
  765. int
  766. gx_default_finish_copydevice(gx_device *dev, const gx_device *from_dev)
  767. {
  768. /* Only allow copying the prototype. */
  769. return (from_dev->memory ? gs_note_error(gs_error_rangecheck) : 0);
  770. }
  771. int
  772. gx_default_pattern_manage(gx_device *pdev, gx_bitmap_id id,
  773. gs_pattern1_instance_t *pinst, pattern_manage_t function)
  774. {
  775. return 0;
  776. }
  777. int
  778. gx_default_fill_rectangle_hl_color(gx_device *pdev,
  779. const gs_fixed_rect *rect,
  780. const gs_imager_state *pis, const gx_drawing_color *pdcolor,
  781. const gx_clip_path *pcpath)
  782. {
  783. return_error(gs_error_rangecheck);
  784. }
  785. int
  786. gx_default_include_color_space(gx_device *pdev, gs_color_space *cspace,
  787. const byte *res_name, int name_length)
  788. {
  789. return 0;
  790. }
  791. /*
  792. * If a device want to determine an equivalent color for its spot colors then
  793. * it needs to implement this method. See comments at the start of
  794. * src/gsequivc.c.
  795. */
  796. int
  797. gx_default_update_spot_equivalent_colors(gx_device *pdev, const gs_state * pgs)
  798. {
  799. return 0;
  800. }
  801. /* ---------------- Default per-instance procedures ---------------- */
  802. int
  803. gx_default_install(gx_device * dev, gs_state * pgs)
  804. {
  805. return 0;
  806. }
  807. int
  808. gx_default_begin_page(gx_device * dev, gs_state * pgs)
  809. {
  810. return 0;
  811. }
  812. int
  813. gx_default_end_page(gx_device * dev, int reason, gs_state * pgs)
  814. {
  815. return (reason != 2 ? 1 : 0);
  816. }