gximag3x.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  1. /* Copyright (C) 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: gximag3x.c,v 1.20 2004/09/16 08:03:56 igor Exp $ */
  14. /* ImageType 3x image implementation */
  15. /****** THE REAL WORK IS NYI ******/
  16. #include "math_.h" /* for ceil, floor */
  17. #include "memory_.h"
  18. #include "gx.h"
  19. #include "gserrors.h"
  20. #include "gsbitops.h"
  21. #include "gscspace.h"
  22. #include "gscpixel.h"
  23. #include "gsstruct.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"
  26. #include "gximag3x.h"
  27. #include "gxistate.h"
  28. #include "gdevbbox.h"
  29. extern_st(st_color_space);
  30. /* Forward references */
  31. private dev_proc_begin_typed_image(gx_begin_image3x);
  32. private image_enum_proc_plane_data(gx_image3x_plane_data);
  33. private image_enum_proc_end_image(gx_image3x_end_image);
  34. private image_enum_proc_flush(gx_image3x_flush);
  35. private image_enum_proc_planes_wanted(gx_image3x_planes_wanted);
  36. /* GC descriptor */
  37. private_st_gs_image3x();
  38. /* Define the image type for ImageType 3x images. */
  39. const gx_image_type_t gs_image_type_3x = {
  40. &st_gs_image3x, gx_begin_image3x, gx_data_image_source_size,
  41. gx_image_no_sput, gx_image_no_sget, gx_image_default_release,
  42. IMAGE3X_IMAGETYPE
  43. };
  44. private const gx_image_enum_procs_t image3x_enum_procs = {
  45. gx_image3x_plane_data, gx_image3x_end_image,
  46. gx_image3x_flush, gx_image3x_planes_wanted
  47. };
  48. /* Initialize an ImageType 3x image. */
  49. private void
  50. gs_image3x_mask_init(gs_image3x_mask_t *pimm)
  51. {
  52. pimm->InterleaveType = 0; /* not a valid type */
  53. pimm->has_Matte = false;
  54. gs_data_image_t_init(&pimm->MaskDict, 1);
  55. pimm->MaskDict.BitsPerComponent = 0; /* not supplied */
  56. }
  57. void
  58. gs_image3x_t_init(gs_image3x_t * pim, const gs_color_space * color_space)
  59. {
  60. gs_pixel_image_t_init((gs_pixel_image_t *) pim, color_space);
  61. pim->type = &gs_image_type_3x;
  62. gs_image3x_mask_init(&pim->Opacity);
  63. gs_image3x_mask_init(&pim->Shape);
  64. }
  65. /*
  66. * We implement ImageType 3 images by interposing a mask clipper in
  67. * front of an ordinary ImageType 1 image. Note that we build up the
  68. * mask row-by-row as we are processing the image.
  69. *
  70. * We export a generalized form of the begin_image procedure for use by
  71. * the PDF and PostScript writers.
  72. */
  73. typedef struct image3x_channel_state_s {
  74. gx_image_enum_common_t *info;
  75. gx_device *mdev; /* gx_device_memory in default impl. */
  76. /* (only for masks) */
  77. gs_image3_interleave_type_t InterleaveType;
  78. int width, height, full_height, depth;
  79. byte *data; /* (if chunky) */
  80. /* Only the following change dynamically. */
  81. int y;
  82. int skip; /* only for masks, # of rows to skip, */
  83. /* see below */
  84. } image3x_channel_state_t;
  85. typedef struct gx_image3x_enum_s {
  86. gx_image_enum_common;
  87. gx_device *pcdev; /* gx_device_mask_clip in default impl. */
  88. int num_components; /* (not counting masks) */
  89. int bpc; /* pixel BitsPerComponent */
  90. gs_memory_t *memory;
  91. #define NUM_MASKS 2 /* opacity, shape */
  92. image3x_channel_state_t mask[NUM_MASKS], pixel;
  93. } gx_image3x_enum_t;
  94. extern_st(st_gx_image_enum_common);
  95. gs_private_st_suffix_add9(st_image3x_enum, gx_image3x_enum_t,
  96. "gx_image3x_enum_t", image3x_enum_enum_ptrs, image3x_enum_reloc_ptrs,
  97. st_gx_image_enum_common, pcdev, mask[0].info, mask[0].mdev, mask[0].data,
  98. mask[1].info, mask[1].mdev, mask[1].data, pixel.info, pixel.data);
  99. /*
  100. * Begin a generic ImageType 3x image, with client handling the creation of
  101. * the mask image and mask clip devices.
  102. */
  103. typedef struct image3x_channel_values_s {
  104. gs_matrix matrix;
  105. gs_point corner;
  106. gs_int_rect rect;
  107. gs_image_t image;
  108. } image3x_channel_values_t;
  109. private int check_image3x_mask(const gs_image3x_t *pim,
  110. const gs_image3x_mask_t *pimm,
  111. const image3x_channel_values_t *ppcv,
  112. image3x_channel_values_t *pmcv,
  113. image3x_channel_state_t *pmcs,
  114. gs_memory_t *mem);
  115. int
  116. gx_begin_image3x_generic(gx_device * dev,
  117. const gs_imager_state *pis, const gs_matrix *pmat,
  118. const gs_image_common_t *pic, const gs_int_rect *prect,
  119. const gx_drawing_color *pdcolor,
  120. const gx_clip_path *pcpath, gs_memory_t *mem,
  121. image3x_make_mid_proc_t make_mid,
  122. image3x_make_mcde_proc_t make_mcde,
  123. gx_image_enum_common_t **pinfo)
  124. {
  125. const gs_image3x_t *pim = (const gs_image3x_t *)pic;
  126. gx_image3x_enum_t *penum;
  127. gx_device *pcdev = 0;
  128. image3x_channel_values_t mask[2], pixel;
  129. gs_matrix mat;
  130. gx_device *midev[2];
  131. gx_image_enum_common_t *minfo[2];
  132. gs_int_point origin[2];
  133. int code;
  134. int i;
  135. /* Validate the parameters. */
  136. if (pim->Height <= 0)
  137. return_error(gs_error_rangecheck);
  138. penum = gs_alloc_struct(mem, gx_image3x_enum_t, &st_image3x_enum,
  139. "gx_begin_image3x");
  140. if (penum == 0)
  141. return_error(gs_error_VMerror);
  142. /* Initialize pointers now in case we bail out. */
  143. penum->mask[0].info = 0, penum->mask[0].mdev = 0, penum->mask[0].data = 0;
  144. penum->mask[1].info = 0, penum->mask[1].mdev = 0, penum->mask[1].data = 0;
  145. penum->pixel.info = 0, penum->pixel.data = 0;
  146. if (prect)
  147. pixel.rect = *prect;
  148. else {
  149. pixel.rect.p.x = pixel.rect.p.y = 0;
  150. pixel.rect.q.x = pim->Width;
  151. pixel.rect.q.y = pim->Height;
  152. }
  153. if ((code = gs_matrix_invert(&pim->ImageMatrix, &pixel.matrix)) < 0 ||
  154. (code = gs_point_transform(pim->Width, pim->Height, &pixel.matrix,
  155. &pixel.corner)) < 0 ||
  156. (code = check_image3x_mask(pim, &pim->Opacity, &pixel, &mask[0],
  157. &penum->mask[0], mem)) < 0 ||
  158. (code = check_image3x_mask(pim, &pim->Shape, &pixel, &mask[1],
  159. &penum->mask[1], mem)) < 0
  160. ) {
  161. goto out0;
  162. }
  163. penum->num_components =
  164. gs_color_space_num_components(pim->ColorSpace);
  165. gx_image_enum_common_init((gx_image_enum_common_t *) penum,
  166. (const gs_data_image_t *)pim,
  167. &image3x_enum_procs, dev,
  168. 1 + penum->num_components,
  169. pim->format);
  170. penum->pixel.width = pixel.rect.q.x - pixel.rect.p.x;
  171. penum->pixel.height = pixel.rect.q.y - pixel.rect.p.y;
  172. penum->pixel.full_height = pim->Height;
  173. penum->pixel.y = 0;
  174. if (penum->mask[0].data || penum->mask[1].data) {
  175. /* Also allocate a row buffer for the pixel data. */
  176. penum->pixel.data =
  177. gs_alloc_bytes(mem,
  178. (penum->pixel.width * pim->BitsPerComponent *
  179. penum->num_components + 7) >> 3,
  180. "gx_begin_image3x(pixel.data)");
  181. if (penum->pixel.data == 0) {
  182. code = gs_note_error(gs_error_VMerror);
  183. goto out1;
  184. }
  185. }
  186. penum->bpc = pim->BitsPerComponent;
  187. penum->memory = mem;
  188. if (pmat == 0)
  189. pmat = &ctm_only(pis);
  190. for (i = 0; i < NUM_MASKS; ++i) {
  191. gs_rect mrect;
  192. gx_device *mdev;
  193. /*
  194. * The mask data has to be defined in a DevicePixel color space
  195. * of the correct depth so that no color mapping will occur.
  196. */
  197. /****** FREE COLOR SPACE ON ERROR OR AT END ******/
  198. gs_color_space *pmcs;
  199. if (penum->mask[i].depth == 0) { /* mask not supplied */
  200. midev[i] = 0;
  201. minfo[i] = 0;
  202. continue;
  203. }
  204. pmcs = gs_alloc_struct(mem, gs_color_space, &st_color_space,
  205. "gx_begin_image3x_generic");
  206. if (pmcs == 0)
  207. return_error(gs_error_VMerror);
  208. gs_cspace_init_DevicePixel(mem, pmcs, penum->mask[i].depth);
  209. mrect.p.x = mrect.p.y = 0;
  210. mrect.q.x = penum->mask[i].width;
  211. mrect.q.y = penum->mask[i].height;
  212. if ((code = gs_matrix_multiply(&mask[i].matrix, pmat, &mat)) < 0 ||
  213. (code = gs_bbox_transform(&mrect, &mat, &mrect)) < 0
  214. )
  215. return code;
  216. origin[i].x = (int)floor(mrect.p.x);
  217. origin[i].y = (int)floor(mrect.p.y);
  218. code = make_mid(&mdev, dev,
  219. (int)ceil(mrect.q.x) - origin[i].x,
  220. (int)ceil(mrect.q.y) - origin[i].y,
  221. penum->mask[i].depth, mem);
  222. if (code < 0)
  223. goto out1;
  224. penum->mask[i].mdev = mdev;
  225. gs_image_t_init(&mask[i].image, pmcs);
  226. mask[i].image.ColorSpace = pmcs;
  227. mask[i].image.adjust = false;
  228. {
  229. const gx_image_type_t *type1 = mask[i].image.type;
  230. const gs_image3x_mask_t *pixm =
  231. (i == 0 ? &pim->Opacity : &pim->Shape);
  232. *(gs_data_image_t *)&mask[i].image = pixm->MaskDict;
  233. mask[i].image.type = type1;
  234. mask[i].image.BitsPerComponent = pixm->MaskDict.BitsPerComponent;
  235. }
  236. {
  237. gs_matrix m_mat;
  238. /*
  239. * Adjust the translation for rendering the mask to include a
  240. * negative translation by origin.{x,y} in device space.
  241. */
  242. m_mat = *pmat;
  243. m_mat.tx -= origin[i].x;
  244. m_mat.ty -= origin[i].y;
  245. /*
  246. * Note that pis = NULL here, since we don't want to have to
  247. * create another imager state with default log_op, etc.
  248. * dcolor = NULL is OK because this is an opaque image with
  249. * CombineWithColor = false.
  250. */
  251. code = gx_device_begin_typed_image(mdev, NULL, &m_mat,
  252. (const gs_image_common_t *)&mask[i].image,
  253. &mask[i].rect, NULL, NULL,
  254. mem, &penum->mask[i].info);
  255. if (code < 0)
  256. goto out2;
  257. }
  258. midev[i] = mdev;
  259. minfo[i] = penum->mask[i].info;
  260. }
  261. gs_image_t_init(&pixel.image, pim->ColorSpace);
  262. {
  263. const gx_image_type_t *type1 = pixel.image.type;
  264. *(gs_pixel_image_t *)&pixel.image = *(const gs_pixel_image_t *)pim;
  265. pixel.image.type = type1;
  266. }
  267. code = make_mcde(dev, pis, pmat, (const gs_image_common_t *)&pixel.image,
  268. prect, pdcolor, pcpath, mem, &penum->pixel.info,
  269. &pcdev, midev, minfo, origin, pim);
  270. if (code < 0)
  271. goto out3;
  272. penum->pcdev = pcdev;
  273. /*
  274. * Set num_planes, plane_widths, and plane_depths from the values in the
  275. * enumerators for the mask(s) and the image data.
  276. */
  277. {
  278. int added_depth = 0;
  279. int pi = 0;
  280. for (i = 0; i < NUM_MASKS; ++i) {
  281. if (penum->mask[i].depth == 0) /* no mask */
  282. continue;
  283. switch (penum->mask[i].InterleaveType) {
  284. case interleave_chunky:
  285. /* Add the mask data to the depth of the image data. */
  286. added_depth += pim->BitsPerComponent;
  287. break;
  288. case interleave_separate_source:
  289. /* Insert the mask as a separate plane. */
  290. penum->plane_widths[pi] = penum->mask[i].width;
  291. penum->plane_depths[pi] = penum->mask[i].depth;
  292. ++pi;
  293. break;
  294. default: /* can't happen */
  295. code = gs_note_error(gs_error_Fatal);
  296. goto out3;
  297. }
  298. }
  299. memcpy(&penum->plane_widths[pi], &penum->pixel.info->plane_widths[0],
  300. penum->pixel.info->num_planes * sizeof(penum->plane_widths[0]));
  301. memcpy(&penum->plane_depths[pi], &penum->pixel.info->plane_depths[0],
  302. penum->pixel.info->num_planes * sizeof(penum->plane_depths[0]));
  303. penum->plane_depths[pi] += added_depth;
  304. penum->num_planes = pi + penum->pixel.info->num_planes;
  305. }
  306. if (midev[0])
  307. gx_device_retain(midev[0], true); /* will free explicitly */
  308. if (midev[1])
  309. gx_device_retain(midev[1], true); /* ditto */
  310. gx_device_retain(pcdev, true); /* ditto */
  311. *pinfo = (gx_image_enum_common_t *) penum;
  312. return 0;
  313. out3:
  314. if (penum->mask[1].info)
  315. gx_image_end(penum->mask[1].info, false);
  316. if (penum->mask[0].info)
  317. gx_image_end(penum->mask[0].info, false);
  318. out2:
  319. if (penum->mask[1].mdev) {
  320. gs_closedevice(penum->mask[1].mdev);
  321. gs_free_object(mem, penum->mask[1].mdev,
  322. "gx_begin_image3x(mask[1].mdev)");
  323. }
  324. if (penum->mask[0].mdev) {
  325. gs_closedevice(penum->mask[0].mdev);
  326. gs_free_object(mem, penum->mask[0].mdev,
  327. "gx_begin_image3x(mask[0].mdev)");
  328. }
  329. out1:
  330. gs_free_object(mem, penum->mask[0].data, "gx_begin_image3x(mask[0].data)");
  331. gs_free_object(mem, penum->mask[1].data, "gx_begin_image3x(mask[1].data)");
  332. gs_free_object(mem, penum->pixel.data, "gx_begin_image3x(pixel.data)");
  333. out0:
  334. gs_free_object(mem, penum, "gx_begin_image3x");
  335. return code;
  336. }
  337. private bool
  338. check_image3x_extent(floatp mask_coeff, floatp data_coeff)
  339. {
  340. if (mask_coeff == 0)
  341. return data_coeff == 0;
  342. if (data_coeff == 0 || (mask_coeff > 0) != (data_coeff > 0))
  343. return false;
  344. return true;
  345. }
  346. /*
  347. * Check mask parameters.
  348. * Reads ppcv->{matrix,corner,rect}, sets pmcv->{matrix,corner,rect} and
  349. * pmcs->{InterleaveType,width,height,full_height,depth,data,y,skip}.
  350. * If the mask is omitted, sets pmcs->depth = 0 and returns normally.
  351. */
  352. private bool
  353. check_image3x_mask(const gs_image3x_t *pim, const gs_image3x_mask_t *pimm,
  354. const image3x_channel_values_t *ppcv,
  355. image3x_channel_values_t *pmcv,
  356. image3x_channel_state_t *pmcs, gs_memory_t *mem)
  357. {
  358. int mask_width = pimm->MaskDict.Width, mask_height = pimm->MaskDict.Height;
  359. int code;
  360. if (pimm->MaskDict.BitsPerComponent == 0) { /* mask missing */
  361. pmcs->depth = 0;
  362. pmcs->InterleaveType = 0; /* not a valid type */
  363. return 0;
  364. }
  365. if (mask_height <= 0)
  366. return_error(gs_error_rangecheck);
  367. switch (pimm->InterleaveType) {
  368. /*case interleave_scan_lines:*/ /* not supported */
  369. default:
  370. return_error(gs_error_rangecheck);
  371. case interleave_chunky:
  372. if (mask_width != pim->Width ||
  373. mask_height != pim->Height ||
  374. pimm->MaskDict.BitsPerComponent != pim->BitsPerComponent ||
  375. pim->format != gs_image_format_chunky
  376. )
  377. return_error(gs_error_rangecheck);
  378. break;
  379. case interleave_separate_source:
  380. switch (pimm->MaskDict.BitsPerComponent) {
  381. case 1: case 2: case 4: case 8:
  382. break;
  383. default:
  384. return_error(gs_error_rangecheck);
  385. }
  386. }
  387. if (!check_image3x_extent(pim->ImageMatrix.xx,
  388. pimm->MaskDict.ImageMatrix.xx) ||
  389. !check_image3x_extent(pim->ImageMatrix.xy,
  390. pimm->MaskDict.ImageMatrix.xy) ||
  391. !check_image3x_extent(pim->ImageMatrix.yx,
  392. pimm->MaskDict.ImageMatrix.yx) ||
  393. !check_image3x_extent(pim->ImageMatrix.yy,
  394. pimm->MaskDict.ImageMatrix.yy)
  395. )
  396. return_error(gs_error_rangecheck);
  397. if ((code = gs_matrix_invert(&pimm->MaskDict.ImageMatrix, &pmcv->matrix)) < 0 ||
  398. (code = gs_point_transform(mask_width, mask_height,
  399. &pmcv->matrix, &pmcv->corner)) < 0
  400. )
  401. return code;
  402. if (fabs(ppcv->matrix.tx - pmcv->matrix.tx) >= 0.5 ||
  403. fabs(ppcv->matrix.ty - pmcv->matrix.ty) >= 0.5 ||
  404. fabs(ppcv->corner.x - pmcv->corner.x) >= 0.5 ||
  405. fabs(ppcv->corner.y - pmcv->corner.y) >= 0.5
  406. )
  407. return_error(gs_error_rangecheck);
  408. pmcv->rect.p.x = ppcv->rect.p.x * mask_width / pim->Width;
  409. pmcv->rect.p.y = ppcv->rect.p.y * mask_height / pim->Height;
  410. pmcv->rect.q.x = (ppcv->rect.q.x * mask_width + pim->Width - 1) /
  411. pim->Width;
  412. pmcv->rect.q.y = (ppcv->rect.q.y * mask_height + pim->Height - 1) /
  413. pim->Height;
  414. /* Initialize the channel state in the enumerator. */
  415. pmcs->InterleaveType = pimm->InterleaveType;
  416. pmcs->width = pmcv->rect.q.x - pmcv->rect.p.x;
  417. pmcs->height = pmcv->rect.q.y - pmcv->rect.p.y;
  418. pmcs->full_height = pimm->MaskDict.Height;
  419. pmcs->depth = pimm->MaskDict.BitsPerComponent;
  420. if (pmcs->InterleaveType == interleave_chunky) {
  421. /* Allocate a buffer for the data. */
  422. pmcs->data =
  423. gs_alloc_bytes(mem,
  424. (pmcs->width * pimm->MaskDict.BitsPerComponent + 7) >> 3,
  425. "gx_begin_image3x(mask data)");
  426. if (pmcs->data == 0)
  427. return_error(gs_error_VMerror);
  428. }
  429. pmcs->y = pmcs->skip = 0;
  430. return 0;
  431. }
  432. /*
  433. * Return > 0 if we want more data from channel 1 now, < 0 if we want more
  434. * from channel 2 now, 0 if we want both.
  435. */
  436. private int
  437. channel_next(const image3x_channel_state_t *pics1,
  438. const image3x_channel_state_t *pics2)
  439. {
  440. /*
  441. * The invariant we need to maintain is that we always have at least as
  442. * much channel N as channel N+1 data, where N = 0 = opacity, 1 = shape,
  443. * and 2 = pixel. I.e., for any two consecutive channels c1 and c2, we
  444. * require c1.y / c1.full_height >= c2.y / c2.full_height, or, to avoid
  445. * floating point, c1.y * c2.full_height >= c2.y * c1.full_height. We
  446. * know this condition is true now; return a value that indicates how to
  447. * maintain it.
  448. */
  449. int h1 = pics1->full_height;
  450. int h2 = pics2->full_height;
  451. long current = pics1->y * (long)h2 - pics2->y * (long)h1;
  452. #ifdef DEBUG
  453. if (current < 0)
  454. lprintf4("channel_next invariant fails: %d/%d < %d/%d\n",
  455. pics1->y, pics1->full_height,
  456. pics2->y, pics2->full_height);
  457. #endif
  458. return ((current -= h1) >= 0 ? -1 :
  459. current + h2 >= 0 ? 0 : 1);
  460. }
  461. /* Define the default implementation of ImageType 3 processing. */
  462. private IMAGE3X_MAKE_MID_PROC(make_midx_default); /* check prototype */
  463. private int
  464. make_midx_default(gx_device **pmidev, gx_device *dev, int width, int height,
  465. int depth, gs_memory_t *mem)
  466. {
  467. const gx_device_memory *mdproto = gdev_mem_device_for_bits(depth);
  468. gx_device_memory *midev;
  469. int code;
  470. if (mdproto == 0)
  471. return_error(gs_error_rangecheck);
  472. midev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  473. "make_mid_default");
  474. if (midev == 0)
  475. return_error(gs_error_VMerror);
  476. gs_make_mem_device(midev, mdproto, mem, 0, NULL);
  477. midev->bitmap_memory = mem;
  478. midev->width = width;
  479. midev->height = height;
  480. check_device_separable((gx_device *)midev);
  481. gx_device_fill_in_procs((gx_device *)midev);
  482. code = dev_proc(midev, open_device)((gx_device *)midev);
  483. if (code < 0) {
  484. gs_free_object(mem, midev, "make_midx_default");
  485. return code;
  486. }
  487. midev->is_open = true;
  488. dev_proc(midev, fill_rectangle)
  489. ((gx_device *)midev, 0, 0, width, height, (gx_color_index)0);
  490. *pmidev = (gx_device *)midev;
  491. return 0;
  492. }
  493. private IMAGE3X_MAKE_MCDE_PROC(make_mcdex_default); /* check prototype */
  494. private int
  495. make_mcdex_default(gx_device *dev, const gs_imager_state *pis,
  496. const gs_matrix *pmat, const gs_image_common_t *pic,
  497. const gs_int_rect *prect, const gx_drawing_color *pdcolor,
  498. const gx_clip_path *pcpath, gs_memory_t *mem,
  499. gx_image_enum_common_t **pinfo,
  500. gx_device **pmcdev, gx_device *midev[2],
  501. gx_image_enum_common_t *pminfo[2],
  502. const gs_int_point origin[2],
  503. const gs_image3x_t *pim)
  504. {
  505. /**************** NYI ****************/
  506. /*
  507. * There is no soft-mask analogue of make_mcde_default, because
  508. * soft-mask clipping is a more complicated operation, implemented
  509. * by the general transparency code. As a default, we simply ignore
  510. * the soft mask. However, we have to create an intermediate device
  511. * that can be freed at the end and that simply forwards all calls.
  512. * The most convenient device for this purpose is the bbox device.
  513. */
  514. gx_device_bbox *bbdev =
  515. gs_alloc_struct_immovable(mem, gx_device_bbox, &st_device_bbox,
  516. "make_mcdex_default");
  517. int code;
  518. if (bbdev == 0)
  519. return_error(gs_error_VMerror);
  520. gx_device_bbox_init(bbdev, dev, mem);
  521. gx_device_bbox_fwd_open_close(bbdev, false);
  522. code = dev_proc(bbdev, begin_typed_image)
  523. ((gx_device *)bbdev, pis, pmat, pic, prect, pdcolor, pcpath, mem,
  524. pinfo);
  525. if (code < 0) {
  526. gs_free_object(mem, bbdev, "make_mcdex_default");
  527. return code;
  528. }
  529. *pmcdev = (gx_device *)bbdev;
  530. return 0;
  531. }
  532. private int
  533. gx_begin_image3x(gx_device * dev,
  534. const gs_imager_state * pis, const gs_matrix * pmat,
  535. const gs_image_common_t * pic, const gs_int_rect * prect,
  536. const gx_drawing_color * pdcolor, const gx_clip_path * pcpath,
  537. gs_memory_t * mem, gx_image_enum_common_t ** pinfo)
  538. {
  539. return gx_begin_image3x_generic(dev, pis, pmat, pic, prect, pdcolor,
  540. pcpath, mem, make_midx_default,
  541. make_mcdex_default, pinfo);
  542. }
  543. /* Process the next piece of an ImageType 3 image. */
  544. private int
  545. gx_image3x_plane_data(gx_image_enum_common_t * info,
  546. const gx_image_plane_t * planes, int height,
  547. int *rows_used)
  548. {
  549. gx_image3x_enum_t *penum = (gx_image3x_enum_t *) info;
  550. int pixel_height = penum->pixel.height;
  551. int pixel_used = 0;
  552. int mask_height[2];
  553. int mask_used[2];
  554. int h1 = pixel_height - penum->pixel.y;
  555. int h;
  556. const gx_image_plane_t *pixel_planes;
  557. gx_image_plane_t pixel_plane, mask_plane[2];
  558. int code = 0;
  559. int i, pi = 0;
  560. int num_chunky = 0;
  561. for (i = 0; i < NUM_MASKS; ++i) {
  562. int mh = mask_height[i] = penum->mask[i].height;
  563. mask_plane[i].data = 0;
  564. mask_used[i] = 0;
  565. if (!penum->mask[i].depth)
  566. continue;
  567. h1 = min(h1, mh - penum->mask[i].y);
  568. if (penum->mask[i].InterleaveType == interleave_chunky)
  569. ++num_chunky;
  570. }
  571. h = min(height, h1);
  572. /* Initialized rows_used in case we get an error. */
  573. *rows_used = 0;
  574. if (h <= 0)
  575. return 0;
  576. /* Handle masks from separate sources. */
  577. for (i = 0; i < NUM_MASKS; ++i)
  578. if (penum->mask[i].InterleaveType == interleave_separate_source) {
  579. /*
  580. * In order to be able to recover from interruptions, we must
  581. * limit separate-source processing to 1 scan line at a time.
  582. */
  583. if (h > 1)
  584. h = 1;
  585. mask_plane[i] = planes[pi++];
  586. }
  587. pixel_planes = &planes[pi];
  588. /* Handle chunky masks. */
  589. if (num_chunky) {
  590. int bpc = penum->bpc;
  591. int num_components = penum->num_components;
  592. int width = penum->pixel.width;
  593. /* Pull apart the source data and the mask data. */
  594. /* We do this in the simplest (not fastest) way for now. */
  595. uint bit_x = bpc * (num_components + num_chunky) * planes[pi].data_x;
  596. sample_load_declare_setup(sptr, sbit, planes[0].data + (bit_x >> 3),
  597. bit_x & 7, bpc);
  598. sample_store_declare_setup(pptr, pbit, pbbyte,
  599. penum->pixel.data, 0, bpc);
  600. sample_store_declare(dptr[NUM_MASKS], dbit[NUM_MASKS],
  601. dbbyte[NUM_MASKS]);
  602. int depth[NUM_MASKS];
  603. int x;
  604. if (h > 1) {
  605. /* Do the operation one row at a time. */
  606. h = 1;
  607. }
  608. for (i = 0; i < NUM_MASKS; ++i)
  609. if (penum->mask[i].data) {
  610. depth[i] = penum->mask[i].depth;
  611. mask_plane[i].data = dptr[i] = penum->mask[i].data;
  612. mask_plane[i].data_x = 0;
  613. /* raster doesn't matter */
  614. sample_store_setup(dbit[i], 0, depth[i]);
  615. sample_store_preload(dbbyte[i], dptr[i], 0, depth[i]);
  616. } else
  617. depth[i] = 0;
  618. pixel_plane.data = pptr;
  619. pixel_plane.data_x = 0;
  620. /* raster doesn't matter */
  621. pixel_planes = &pixel_plane;
  622. for (x = 0; x < width; ++x) {
  623. uint value;
  624. for (i = 0; i < NUM_MASKS; ++i)
  625. if (depth[i]) {
  626. sample_load_next12(value, sptr, sbit, bpc);
  627. sample_store_next12(value, dptr[i], dbit[i], depth[i],
  628. dbbyte[i]);
  629. }
  630. for (i = 0; i < num_components; ++i) {
  631. sample_load_next12(value, sptr, sbit, bpc);
  632. sample_store_next12(value, pptr, pbit, bpc, pbbyte);
  633. }
  634. }
  635. for (i = 0; i < NUM_MASKS; ++i)
  636. if (penum->mask[i].data)
  637. sample_store_flush(dptr[i], dbit[i], depth[i], dbbyte[i]);
  638. sample_store_flush(pptr, pbit, bpc, pbbyte);
  639. }
  640. /*
  641. * Process the mask data first, so it will set up the mask
  642. * device for clipping the pixel data.
  643. */
  644. for (i = 0; i < NUM_MASKS; ++i)
  645. if (mask_plane[i].data) {
  646. /*
  647. * If, on the last call, we processed some mask rows
  648. * successfully but processing the pixel rows was interrupted,
  649. * we set rows_used to indicate the number of pixel rows
  650. * processed (since there is no way to return two rows_used
  651. * values). If this happened, some mask rows may get presented
  652. * again. We must skip over them rather than processing them
  653. * again.
  654. */
  655. int skip = penum->mask[i].skip;
  656. if (skip >= h) {
  657. penum->mask[i].skip = skip - (mask_used[i] = h);
  658. } else {
  659. int mask_h = h - skip;
  660. mask_plane[i].data += skip * mask_plane[i].raster;
  661. penum->mask[i].skip = 0;
  662. code = gx_image_plane_data_rows(penum->mask[i].info,
  663. &mask_plane[i],
  664. mask_h, &mask_used[i]);
  665. mask_used[i] += skip;
  666. }
  667. *rows_used = mask_used[i];
  668. penum->mask[i].y += mask_used[i];
  669. if (code < 0)
  670. return code;
  671. }
  672. if (pixel_planes[0].data) {
  673. /*
  674. * If necessary, flush any buffered mask data to the mask clipping
  675. * device.
  676. */
  677. for (i = 0; i < NUM_MASKS; ++i)
  678. if (penum->mask[i].info)
  679. gx_image_flush(penum->mask[i].info);
  680. code = gx_image_plane_data_rows(penum->pixel.info, pixel_planes, h,
  681. &pixel_used);
  682. /*
  683. * There isn't any way to set rows_used if different amounts of
  684. * the mask and pixel data were used. Fake it.
  685. */
  686. *rows_used = pixel_used;
  687. /*
  688. * Don't return code yet: we must account for the fact that
  689. * some mask data may have been processed.
  690. */
  691. penum->pixel.y += pixel_used;
  692. if (code < 0) {
  693. /*
  694. * We must prevent the mask data from being processed again.
  695. * We rely on the fact that h > 1 is only possible if the
  696. * mask and pixel data have the same Y scaling.
  697. */
  698. for (i = 0; i < NUM_MASKS; ++i)
  699. if (mask_used[i] > pixel_used) {
  700. int skip = mask_used[i] - pixel_used;
  701. penum->mask[i].skip = skip;
  702. penum->mask[i].y -= skip;
  703. mask_used[i] = pixel_used;
  704. }
  705. }
  706. }
  707. if_debug7('b', "[b]image3x h=%d %sopacity.y=%d %sopacity.y=%d %spixel.y=%d\n",
  708. h, (mask_plane[0].data ? "+" : ""), penum->mask[0].y,
  709. (mask_plane[1].data ? "+" : ""), penum->mask[1].y,
  710. (pixel_planes[0].data ? "+" : ""), penum->pixel.y);
  711. if (penum->mask[0].y >= penum->mask[0].height &&
  712. penum->mask[1].y >= penum->mask[1].height &&
  713. penum->pixel.y >= penum->pixel.height)
  714. return 1;
  715. /*
  716. * The mask may be complete (gx_image_plane_data_rows returned 1),
  717. * but there may still be pixel rows to go, so don't return 1 here.
  718. */
  719. return (code < 0 ? code : 0);
  720. }
  721. /* Flush buffered data. */
  722. private int
  723. gx_image3x_flush(gx_image_enum_common_t * info)
  724. {
  725. gx_image3x_enum_t * const penum = (gx_image3x_enum_t *) info;
  726. int code = gx_image_flush(penum->mask[0].info);
  727. if (code >= 0)
  728. code = gx_image_flush(penum->mask[1].info);
  729. if (code >= 0)
  730. code = gx_image_flush(penum->pixel.info);
  731. return code;
  732. }
  733. /* Determine which data planes are wanted. */
  734. private bool
  735. gx_image3x_planes_wanted(const gx_image_enum_common_t * info, byte *wanted)
  736. {
  737. const gx_image3x_enum_t * const penum = (const gx_image3x_enum_t *) info;
  738. /*
  739. * We always want at least as much of the mask(s) to be filled as the
  740. * pixel data.
  741. */
  742. bool
  743. sso = penum->mask[0].InterleaveType == interleave_separate_source,
  744. sss = penum->mask[1].InterleaveType == interleave_separate_source;
  745. if (sso & sss) {
  746. /* Both masks have separate sources. */
  747. int mask_next = channel_next(&penum->mask[1], &penum->pixel);
  748. memset(wanted + 2, (mask_next <= 0 ? 0xff : 0), info->num_planes - 2);
  749. wanted[1] = (mask_next >= 0 ? 0xff : 0);
  750. if (wanted[1]) {
  751. mask_next = channel_next(&penum->mask[0], &penum->mask[1]);
  752. wanted[0] = mask_next >= 0;
  753. } else
  754. wanted[0] = 0;
  755. return false; /* see below */
  756. } else if (sso | sss) {
  757. /* Only one separate source. */
  758. const image3x_channel_state_t *pics =
  759. (sso ? &penum->mask[0] : &penum->mask[1]);
  760. int mask_next = channel_next(pics, &penum->pixel);
  761. wanted[0] = (mask_next >= 0 ? 0xff : 0);
  762. memset(wanted + 1, (mask_next <= 0 ? 0xff : 0), info->num_planes - 1);
  763. /*
  764. * In principle, wanted will always be true for both mask and pixel
  765. * data if the full_heights are equal. Unfortunately, even in this
  766. * case, processing may be interrupted after a mask row has been
  767. * passed to the underlying image processor but before the data row
  768. * has been passed, in which case pixel data will be 'wanted', but
  769. * not mask data, for the next call. Therefore, we must return
  770. * false.
  771. */
  772. return false
  773. /*(next == 0 &&
  774. pics->full_height == penum->pixel.full_height)*/;
  775. } else {
  776. /* Everything is chunky, only 1 plane. */
  777. wanted[0] = 0xff;
  778. return true;
  779. }
  780. }
  781. /* Clean up after processing an ImageType 3x image. */
  782. private int
  783. gx_image3x_end_image(gx_image_enum_common_t * info, bool draw_last)
  784. {
  785. gx_image3x_enum_t *penum = (gx_image3x_enum_t *) info;
  786. gs_memory_t *mem = penum->memory;
  787. gx_device *mdev0 = penum->mask[0].mdev;
  788. int ocode =
  789. (penum->mask[0].info ? gx_image_end(penum->mask[0].info, draw_last) :
  790. 0);
  791. gx_device *mdev1 = penum->mask[1].mdev;
  792. int scode =
  793. (penum->mask[1].info ? gx_image_end(penum->mask[1].info, draw_last) :
  794. 0);
  795. gx_device *pcdev = penum->pcdev;
  796. int pcode = gx_image_end(penum->pixel.info, draw_last);
  797. gs_closedevice(pcdev);
  798. if (mdev0)
  799. gs_closedevice(mdev0);
  800. if (mdev1)
  801. gs_closedevice(mdev1);
  802. gs_free_object(mem, penum->mask[0].data,
  803. "gx_image3x_end_image(mask[0].data)");
  804. gs_free_object(mem, penum->mask[1].data,
  805. "gx_image3x_end_image(mask[1].data)");
  806. gs_free_object(mem, penum->pixel.data,
  807. "gx_image3x_end_image(pixel.data)");
  808. gs_free_object(mem, pcdev, "gx_image3x_end_image(pcdev)");
  809. gs_free_object(mem, mdev0, "gx_image3x_end_image(mask[0].mdev)");
  810. gs_free_object(mem, mdev1, "gx_image3x_end_image(mask[1].mdev)");
  811. gs_free_object(mem, penum, "gx_image3x_end_image");
  812. return (pcode < 0 ? pcode : scode < 0 ? scode : ocode);
  813. }