gscspace.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* Copyright (C) 1991, 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: gscspace.h,v 1.14 2004/08/04 19:36:12 stefan Exp $ */
  14. /* Client interface to color spaces */
  15. #ifndef gscspace_INCLUDED
  16. # define gscspace_INCLUDED
  17. #include "gsmemory.h"
  18. #include "gsiparam.h"
  19. /*
  20. * The handling of color spaces in the graphic library is somewhat
  21. * awkward because of historical artifacts.
  22. *
  23. * The PostScript Level 1 (DeviceGray/RGB) color spaces, and the "Level
  24. * 1 1/2" DeviceCMYK space, have no associated parameters. Therefore,
  25. * they can be represented as simple objects (just a pointer to a type
  26. * vector containing procedures and parameters). This was the original
  27. * design.
  28. *
  29. * PostScript Level 2 and LanguageLevel 3 add two new kinds of color spaces:
  30. * color spaces with parameters (CIEBased and DevicePixel spaces), and
  31. * compound color spaces (Indexed, Separation, Pattern, and DeviceN spaces),
  32. * with parameters that include a specification of an alternative or
  33. * underlying color space. To handle these spaces, we extended the original
  34. * design to store in-line certain scalar parameters (i.e., parameters other
  35. * than the complex color transformation data for CIEBased spaces, the lookup
  36. * table for Indexed spaces, and the list of component names for DeviceN
  37. * spaces) in the color space object. For compound spaces, this requires
  38. * storing a color space in-line inside another color space, which is clearly
  39. * impossible. Therefore, we defined a generality hierarchy for color spaces:
  40. *
  41. * - Base spaces (DeviceGray/RGB/CMYK/Pixel, CIEBased), whose
  42. * whose parameters (if any) don't include other color spaces.
  43. *
  44. * - Direct spaces (base spaces + Separation and DeviceN), which
  45. * may have a base space as an alternative space.
  46. *
  47. * - Paint spaces (direct spaces + Indexed), which may have a direct
  48. * space as the underlying space
  49. *
  50. * - General spaces (paint spaces + Pattern), which may have a
  51. * paint space as the underlying space.
  52. *
  53. * With this approach, a general space can include a paint space stored
  54. * in-line; a paint space (either in its own right or as the underlying
  55. * space of a Pattern space) can include a direct space in-line; and a
  56. * direct space can include a base space in-line.
  57. *
  58. * The introduction of ICCBased color spaces in PDF 1.3 wrecked havoc on
  59. * this hierarchy. ICCBased color spaces contain alternative color spaces,
  60. * which may be paint spaces. On the other hand, ICCBased spaces may be
  61. * use as alternative or underlying color spaces in direct and paint
  62. * spaces.
  63. *
  64. * The proper solution to this problem would be to reference alternative and
  65. * underlying color spaces via a pointer. The cost and risk of that approach
  66. * is, however, larger than is warranted just by the addition of ICCBased
  67. * color spaces. Another alternative is to allow just base color spaces to
  68. * include an alternative color space via a pointer. That would require two
  69. * separate mechanisms for dealing with alternative color spaces, which
  70. * seems messy and may well cause problems in the future.
  71. *
  72. * Examination of some PDF files indicates that ICCBased color spaces are
  73. * routinely used as alternative spaces for Separation or Device color
  74. * spaces, but essentially never make use of alternative color spaces other
  75. * than the device-specific spaces. To support this usage, the approach
  76. * taken here splits previous base color space class into "small" and
  77. * "regular" base color spaces. Small base color spaces include all of
  78. * those color spaces that previously were considered base spaces. Regular
  79. * base spaces add the ICCBased color spaces to this set. To maintain
  80. * compatibility with the existing code base, regular base spaces make use
  81. * of the gs_base_color_space type name.
  82. *
  83. * Note that because general, paint, direct, regular base, and small base
  84. * spaces are (necessarily) of different sizes, assigning (copying the top
  85. * object of) color spaces must take into account the actual size of the
  86. * color space being assigned. In principle, this also requires checking
  87. * that the source object will actually fit into the destination. Currently
  88. * we rely on the caller to ensure that this is the case; in fact, the
  89. * current API (gs_cspace_init and gs_cspace_assign) doesn't even provide
  90. * enough information to make the check.
  91. *
  92. * In retrospect, we might have gotten a simpler design without significant
  93. * performance loss by always referencing underlying and alternate spaces
  94. * through a pointer; however, at this point, the cost and risk of changing
  95. * to such a design are too high.
  96. *
  97. * There are two aspects to memory management for color spaces: managing
  98. * the color space objects themselves, and managing the non-scalar
  99. * parameters that they reference (if any).
  100. *
  101. * - Color space objects per se have no special management properties:
  102. * they can be allocated on the stack or on the heap, and freed by
  103. * scope exit, explicit deallocation, or garbage collection.
  104. *
  105. * - Separately allocated (non-scalar) color space parameters are
  106. * managed by reference counting. Currently we do this for the
  107. * CIEBased spaces, and for the first-level parameters of Indexed and
  108. * Separation spaces: clients must deal with deallocating the other
  109. * parameter structures mentioned above, including the Indexed lookup
  110. * table if any. This is clearly not a good situation, but we don't
  111. * envision fixing it any time soon.
  112. *
  113. * Here is the information associated with the various color space
  114. * structures. Note that DevicePixel, DeviceN, and the ability to use
  115. * Separation or DeviceN spaces as the base space of an Indexed space
  116. * are LanguageLevel 3 additions. Unfortunately, the terminology for the
  117. * different levels of generality is confusing and inconsistent for
  118. * historical reasons.
  119. *
  120. * For base spaces:
  121. *
  122. * Space Space parameters Color parameters
  123. * ----- ---------------- ----------------
  124. * DeviceGray (none) 1 real [0-1]
  125. * DeviceRGB (none) 3 reals [0-1]
  126. * DeviceCMYK (none) 4 reals [0-1]
  127. * DevicePixel depth 1 int [up to depth bits]
  128. * CIEBasedDEFG dictionary 4 reals
  129. * CIEBasedDEF dictionary 3 reals
  130. * CIEBasedABC dictionary 3 reals
  131. * CIEBasedA dictionary 1 real
  132. *
  133. * For non-base direct spaces:
  134. *
  135. * Space Space parameters Color parameters
  136. * ----- ---------------- ----------------
  137. *
  138. * Separation name, alt_space, tint_xform 1 real [0-1]
  139. * DeviceN names, alt_space, tint_xform N reals
  140. *
  141. * For non-direct paint spaces:
  142. *
  143. * Space Space parameters Color parameters
  144. * ----- ---------------- ----------------
  145. * Indexed base_space, hival, lookup 1 int [0-hival]
  146. * ICCBased dictionary, alt_space 1, 3, or 4 reals
  147. *
  148. * For non-paint spaces:
  149. *
  150. * Space Space parameters Color parameters
  151. * ----- ---------------- ----------------
  152. * Pattern colored: (none) dictionary
  153. * uncolored: base_space dictionary + base space params
  154. */
  155. /*
  156. * Define color space type indices. NOTE: PostScript code (gs_res.ps,
  157. * gs_ll3.ps) and the color space substitution code (gscssub.[hc] and its
  158. * clients) assumes values 0-2 for DeviceGray/RGB/CMYK respectively.
  159. */
  160. typedef enum {
  161. /* Supported in all configurations */
  162. gs_color_space_index_DeviceGray = 0,
  163. gs_color_space_index_DeviceRGB,
  164. /* Supported in extended Level 1, and in Level 2 and above */
  165. gs_color_space_index_DeviceCMYK,
  166. /* Supported in LanguageLevel 3 only */
  167. gs_color_space_index_DevicePixel,
  168. gs_color_space_index_DeviceN,
  169. /* Supported in Level 2 and above only */
  170. /* DEC C truncates identifiers at 32 characters, so.... */
  171. gs_color_space_index_CIEDEFG,
  172. gs_color_space_index_CIEDEF,
  173. gs_color_space_index_CIEABC,
  174. gs_color_space_index_CIEA,
  175. gs_color_space_index_Separation,
  176. gs_color_space_index_Indexed,
  177. gs_color_space_index_Pattern,
  178. /* Supported in PDF 1.3 and later only */
  179. gs_color_space_index_CIEICC
  180. } gs_color_space_index;
  181. /* We define the names only for debugging printout. */
  182. #define GS_COLOR_SPACE_TYPE_NAMES\
  183. "DeviceGray", "DeviceRGB", "DeviceCMYK", "DevicePixel", "DeviceN",\
  184. "ICCBased", "CIEBasedDEFG", "CIEBasedDEF", "CIEBasedABC", "CIEBasedA",\
  185. "Separation", "Indexed", "Pattern"
  186. /* Define an abstract type for color space types (method structures). */
  187. typedef struct gs_color_space_type_s gs_color_space_type;
  188. /*
  189. * The common part of all color spaces. This structure now includes a memory
  190. * structure pointer, so that it may be released without providing this
  191. * information separately. (type is a pointer to the structure of methods.)
  192. *
  193. * Note that all color space structures consist of the basic information and
  194. * a union containing some additional information. The macro operand is that
  195. * union.
  196. */
  197. #define gs_cspace_common(param_union) \
  198. const gs_color_space_type * type; \
  199. gs_memory_t * pmem; \
  200. gs_id id; \
  201. union { \
  202. param_union; \
  203. } params
  204. /*
  205. * Parameters for "small" base color spaces. Of the small base color spaces,
  206. * only DevicePixel and CIE spaces have parameters: see gscie.h for the
  207. * structure definitions for CIE space parameters.
  208. */
  209. typedef struct gs_device_pixel_params_s {
  210. int depth;
  211. } gs_device_pixel_params;
  212. typedef struct gs_cie_a_s gs_cie_a;
  213. typedef struct gs_cie_abc_s gs_cie_abc;
  214. typedef struct gs_cie_def_s gs_cie_def;
  215. typedef struct gs_cie_defg_s gs_cie_defg;
  216. #define gs_small_base_cspace_params \
  217. gs_device_pixel_params pixel; \
  218. gs_cie_defg * defg; \
  219. gs_cie_def * def; \
  220. gs_cie_abc * abc; \
  221. gs_cie_a * a
  222. typedef struct gs_small_base_color_space_s {
  223. gs_cspace_common(gs_small_base_cspace_params);
  224. } gs_small_base_color_space;
  225. #define gs_small_base_color_space_size sizeof(gs_small_base_color_space)
  226. /*
  227. * "Regular" base color spaces include all of the small base color space and
  228. * the ICCBased color space, which includes a small base color space as an
  229. * alternative color space. See gsicc.h for the structure definition of
  230. * gs_cie_icc_s.
  231. */
  232. typedef struct gs_cie_icc_s gs_cie_icc;
  233. typedef struct gs_cieicc_params_s {
  234. gs_cie_icc * picc_info;
  235. gs_small_base_color_space alt_space;
  236. } gs_icc_params;
  237. #define gs_base_cspace_params \
  238. gs_small_base_cspace_params;\
  239. gs_icc_params icc
  240. typedef struct gs_base_color_space_s {
  241. gs_cspace_common(gs_base_cspace_params);
  242. } gs_base_color_space;
  243. #define gs_base_color_space_size sizeof(gs_base_color_space)
  244. #ifndef gs_device_n_map_DEFINED
  245. # define gs_device_n_map_DEFINED
  246. typedef struct gs_device_n_map_s gs_device_n_map;
  247. #endif
  248. /*
  249. * Non-base direct color spaces: Separation and DeviceN.
  250. * These include a base alternative color space.
  251. */
  252. typedef ulong gs_separation_name; /* BOGUS */
  253. /*
  254. * Define callback function for graphics library to ask
  255. * interpreter about character string representation of
  256. * component names. This is used for comparison of component
  257. * names with similar objects like ProcessColorModel colorant
  258. * names.
  259. */
  260. typedef int (gs_callback_func_get_colorname_string)
  261. (const gs_memory_t *mem, gs_separation_name colorname, unsigned char **ppstr, unsigned int *plen);
  262. typedef enum { SEP_NONE, SEP_ALL, SEP_OTHER } separation_type;
  263. typedef struct gs_separation_params_s {
  264. gs_separation_name sep_name;
  265. gs_base_color_space alt_space;
  266. gs_device_n_map *map;
  267. separation_type sep_type;
  268. bool use_alt_cspace;
  269. gs_callback_func_get_colorname_string *get_colorname_string;
  270. } gs_separation_params;
  271. typedef struct gs_device_n_params_s {
  272. gs_separation_name *names;
  273. uint num_components;
  274. gs_base_color_space alt_space;
  275. gs_device_n_map *map;
  276. bool use_alt_cspace;
  277. gs_callback_func_get_colorname_string *get_colorname_string;
  278. } gs_device_n_params;
  279. #define gs_direct_cspace_params \
  280. gs_base_cspace_params; \
  281. gs_separation_params separation; \
  282. gs_device_n_params device_n
  283. typedef struct gs_direct_color_space_s {
  284. gs_cspace_common(gs_direct_cspace_params);
  285. } gs_direct_color_space;
  286. #define gs_direct_color_space_size sizeof(gs_direct_color_space)
  287. /*
  288. * Non-direct paint space: Indexed space.
  289. *
  290. * Note that for indexed color spaces, hival is the highest support index,
  291. * which is one less than the number of entries in the palette (as defined
  292. * in PostScript).
  293. */
  294. typedef struct gs_indexed_map_s gs_indexed_map;
  295. typedef struct gs_indexed_params_s {
  296. gs_direct_color_space base_space;
  297. int hival; /* num_entries - 1 */
  298. union {
  299. gs_const_string table; /* size is implicit */
  300. gs_indexed_map *map;
  301. } lookup;
  302. bool use_proc; /* 0 = use table, 1 = use proc & map */
  303. } gs_indexed_params;
  304. #define gs_paint_cspace_params \
  305. gs_direct_cspace_params; \
  306. gs_indexed_params indexed
  307. typedef struct gs_paint_color_space_s {
  308. gs_cspace_common(gs_paint_cspace_params);
  309. } gs_paint_color_space;
  310. #define gs_paint_color_space_size sizeof(gs_paint_color_space)
  311. /*
  312. * Pattern parameter set. This may contain an instances of a paintable
  313. * color space. The boolean indicates if this is the case.
  314. */
  315. typedef struct gs_pattern_params_s {
  316. bool has_base_space;
  317. gs_paint_color_space base_space;
  318. } gs_pattern_params;
  319. /*
  320. * Fully general color spaces.
  321. */
  322. struct gs_color_space_s {
  323. gs_cspace_common(
  324. gs_paint_cspace_params;
  325. gs_pattern_params pattern
  326. );
  327. };
  328. #define gs_pattern_color_space_size sizeof(gs_color_space)
  329. /*
  330. * Define the abstract type for color space objects.
  331. */
  332. #ifndef gs_color_space_DEFINED
  333. # define gs_color_space_DEFINED
  334. typedef struct gs_color_space_s gs_color_space;
  335. #endif
  336. /*extern_st(st_color_space); *//* in gxcspace.h */
  337. #define public_st_color_space() /* in gscspace.c */ \
  338. gs_public_st_composite( st_color_space, \
  339. gs_color_space, \
  340. "gs_color_space", \
  341. color_space_enum_ptrs, \
  342. color_space_reloc_ptrs \
  343. )
  344. #define st_color_space_max_ptrs 2 /* 1 base + 1 indexed */
  345. /* ---------------- Procedures ---------------- */
  346. /* ------ Create/copy/destroy ------ */
  347. /*
  348. * Note that many of the constructors take no parameters, and the
  349. * remainder take only a few (CIE color spaces constructures take a
  350. * client data pointer as an operand, the composite color space (Separation,
  351. * Indexed, and Pattern) constructurs take the base space as an operand,
  352. * and the Indexed color space constructors have a few additiona operands).
  353. * This is done to conserve memory. If initialization values for all the
  354. * color space parameters were provided to the constructors, these values
  355. * would need to have some fairly generic format. Different clients gather
  356. * this data in different forms, so they would need to allocate memory to
  357. * convert it to the generic form, only to immediately release this memory
  358. * once the construction is complete.
  359. *
  360. * The alternative approach employed here is to provide a number of access
  361. * methods (macros) that return pointers to individual fields of the
  362. * various color space structures. This requires exporting only fairly simple
  363. * data structures, and so does not violate modularity too severely.
  364. *
  365. * NB: Of necessity, the macros provide access to modifiable structures. If
  366. * these structures are modified after the color space object is first
  367. * initialized, unpredictable and, most likely, undesirable results will
  368. * occur.
  369. *
  370. * The constructors return an integer, 0 on success and an
  371. * error code on failure (gs_error_VMerror or gs_error_rangecheck).
  372. *
  373. * In parallel with the constructors, we provide initializers that assume
  374. * the client has allocated the color space object (perhaps on the stack)
  375. * and takes responsibility for freeing it.
  376. */
  377. extern int
  378. gs_cspace_init_DeviceGray(const gs_memory_t *mem, gs_color_space *pcs),
  379. gs_cspace_build_DeviceGray(gs_color_space ** ppcspace,
  380. gs_memory_t * pmem),
  381. gs_cspace_init_DeviceRGB(const gs_memory_t *mem, gs_color_space *pcs),
  382. gs_cspace_build_DeviceRGB(gs_color_space ** ppcspace,
  383. gs_memory_t * pmem),
  384. gs_cspace_init_DeviceCMYK(const gs_memory_t *mem, gs_color_space *pcs),
  385. gs_cspace_build_DeviceCMYK(gs_color_space ** ppcspace,
  386. gs_memory_t * pmem);
  387. /* Copy a color space into one newly allocated by the caller. */
  388. void gs_cspace_init_from(gs_color_space * pcsto,
  389. const gs_color_space * pcsfrom);
  390. /* Assign a color space into a previously initialized one. */
  391. void gs_cspace_assign(gs_color_space * pdest, const gs_color_space * psrc);
  392. /* Prepare to free a color space. */
  393. void gs_cspace_release(gs_color_space * pcs);
  394. /* ------ Accessors ------ */
  395. /* Get the index of a color space. */
  396. gs_color_space_index gs_color_space_get_index(const gs_color_space *);
  397. /* Get the number of components in a color space. */
  398. int gs_color_space_num_components(const gs_color_space *);
  399. /*
  400. * Test whether two color spaces are equal. Note that this test is
  401. * conservative: if it returns true, the color spaces are definitely
  402. * equal, while if it returns false, they might still be equivalent.
  403. */
  404. bool gs_color_space_equal(const gs_color_space *pcs1,
  405. const gs_color_space *pcs2);
  406. /* Restrict a color to its legal range. */
  407. #ifndef gs_client_color_DEFINED
  408. # define gs_client_color_DEFINED
  409. typedef struct gs_client_color_s gs_client_color;
  410. #endif
  411. void gs_color_space_restrict_color(gs_client_color *, const gs_color_space *);
  412. /*
  413. * Get the base space of an Indexed or uncolored Pattern color space, or the
  414. * alternate space of a Separation or DeviceN space. Return NULL if the
  415. * color space does not have a base/alternative color space.
  416. */
  417. const gs_color_space *gs_cspace_base_space(const gs_color_space * pcspace);
  418. /* backwards compatibility */
  419. #define gs_color_space_indexed_base_space(pcspace)\
  420. gs_cspace_base_space(pcspace)
  421. #endif /* gscspace_INCLUDED */