ilimage.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: ilimage.c /main/3 1995/10/23 15:48:58 rswiston $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1991 Hewlett-Packard Co.
  27. ***
  28. *** RESTRICTED RIGHTS LEGEND
  29. *** Use, duplication, or disclosure by the U.S. Government is subject to
  30. *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
  31. *** Technical Data and Computer Software clause in DFARS 252.227-7013.
  32. *** Hewlett-Packard Company
  33. *** 3000 Hanover Street
  34. *** Palo Alto, CA 94304 U.S.A.
  35. *** Rights for non-DOD U.S. Government Departments and Agencies are as set
  36. *** forth in FAR 52.227-19(c)(1,2).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. /* /ilc/ilimage.c : General image handling code.
  40. */
  41. #include <stdlib.h>
  42. #include "ilint.h"
  43. #include "ilpipelem.h"
  44. #include "ilimage.h"
  45. #include "ilcontext.h"
  46. #include "ilerrors.h"
  47. #include "ilutiljpeg.h"
  48. /* Table of supported compression modes for each image type.
  49. */
  50. static unsigned long ilSupportedCompressions [IL_MAX_TYPE+1] = {
  51. /* IL_BITONAL */ ((1 << IL_UNCOMPRESSED) |
  52. (1 << IL_G3) |
  53. (1 << IL_G4) |
  54. (1 << IL_LZW) |
  55. (1 << IL_PACKBITS)),
  56. /* IL_GRAY */ ((1 << IL_UNCOMPRESSED) |
  57. (1 << IL_LZW) |
  58. (1 << IL_PACKBITS) |
  59. (1 << IL_JPEG)),
  60. /* IL_PALETTE */ ((1 << IL_UNCOMPRESSED) |
  61. (1 << IL_PACKBITS) |
  62. (1 << IL_LZW)),
  63. /* IL_RGB */ ((1 << IL_UNCOMPRESSED) |
  64. (1 << IL_LZW) |
  65. (1 << IL_JPEG)),
  66. /* IL_YCRCB */ ((1 << IL_UNCOMPRESSED) |
  67. (1 << IL_LZW) |
  68. (1 << IL_JPEG)) };
  69. /* (ilBool) IL_VALID_COMPRESSION(comp, imageType)
  70. returns TRUE if "comp" is a valid compression for "imageType", a valid image
  71. type (e.g. IL_BITONAL).
  72. */
  73. #define IL_VALID_COMPRESSION(_comp, _imageType) \
  74. ((01L << (_comp)) & ilSupportedCompressions[_imageType])
  75. /* ------------------------ ilValidateDesFormat ---------------------------- */
  76. /* Validate the given image descriptor at "pDes" and the image format at
  77. "pFormat" if it is non-null and validate that they are compatible
  78. with each other.
  79. If "allowPrivateTypes" is true, private image type codes are allowed;
  80. otherwise an error is declared. Should be true for client images.
  81. Return 0 (IL_OK) if valid, else error code.
  82. NOTE: this function defines valid compressions and nBitsPerSample.
  83. */
  84. IL_PRIVATE ilError _ilValidateDesFormat (
  85. ilBool allowPrivateTypes,
  86. const ilImageDes *pDes,
  87. const ilImageFormat *pFormat
  88. )
  89. {
  90. #define MAX_BITS_PER_SAMPLE 24 /* maximum # of bits/sample supported */
  91. unsigned int i, j, nBits, nLevels, nSamplesPerPixel;
  92. const ilYCbCrSampleInfo *pSample;
  93. /* reserved must = 0; # samples must be 1 .. IL_MAX_SAMPLES.
  94. */
  95. for (i = 0; i < IL_DES_RESERVED_SIZE; i++)
  96. if (pDes->reserved[i] != 0)
  97. return IL_ERROR_RESERVED_NOT_ZERO;
  98. nSamplesPerPixel = pDes->nSamplesPerPixel;
  99. if ((nSamplesPerPixel <= 0) || (nSamplesPerPixel > IL_MAX_SAMPLES))
  100. return IL_ERROR_SAMPLES_PER_PIXEL;
  101. /* unused bits in flags must = 0; if dithered palette bit set, must be a
  102. palette image with levels whose product is <= # levels.
  103. */
  104. if (pDes->flags & ~(IL_DITHERED_PALETTE))
  105. return IL_ERROR_RESERVED_NOT_ZERO;
  106. if (pDes->flags & IL_DITHERED_PALETTE) {
  107. if ((pDes->type != IL_PALETTE)
  108. || ((pDes->typeInfo.palette.levels[0] * pDes->typeInfo.palette.levels[1] *
  109. pDes->typeInfo.palette.levels[2]) > pDes->nLevelsPerSample[0]))
  110. return IL_ERROR_PALETTE_INFO;
  111. }
  112. /* If validating format: all values must be as documented in spec.
  113. For each sample: bits must be 1..MAX_BITS_PER_SAMPLE, and levels
  114. be >= 2 and fit in those bits.
  115. If no format, levels must be 1..2**MAX_BITS_PER_SAMPLE.
  116. */
  117. if (pFormat) {
  118. if (pFormat->byteOrder > IL_MAX_BYTE_ORDER)
  119. return IL_ERROR_BYTE_ORDER;
  120. if (pFormat->sampleOrder > IL_MAX_SAMPLE_ORDER)
  121. return IL_ERROR_SAMPLE_ORDER;
  122. /* NOTE: currently only pixel sample order supported if compressed */
  123. if ((pDes->compression != IL_UNCOMPRESSED)
  124. && (pFormat->sampleOrder != IL_SAMPLE_PIXELS))
  125. return IL_ERROR_SAMPLE_ORDER;
  126. nBits = pFormat->rowBitAlign;
  127. if ((nBits != 8) && (nBits != 16) && (nBits != 32))
  128. return IL_ERROR_ROW_BIT_ALIGN;
  129. for (i = 0; i < IL_FORMAT_RESERVED_SIZE; i++)
  130. if (pFormat->reserved[i] != 0)
  131. return IL_ERROR_RESERVED_NOT_ZERO;
  132. for (i = 0; i < nSamplesPerPixel; i++) {
  133. nBits = pFormat->nBitsPerSample[i];
  134. if ((nBits <= 0) || (nBits > MAX_BITS_PER_SAMPLE))
  135. return IL_ERROR_BITS_PER_SAMPLE;
  136. nLevels = pDes->nLevelsPerSample[i];
  137. if ((nLevels < 2) || (nLevels > (1<<nBits)))
  138. return IL_ERROR_LEVELS_PER_SAMPLE;
  139. }
  140. }
  141. else {
  142. for (i = 0; i < nSamplesPerPixel; i++) {
  143. nLevels = pDes->nLevelsPerSample [i];
  144. if ((nLevels < 2) || (nLevels > (1<<MAX_BITS_PER_SAMPLE)))
  145. return IL_ERROR_LEVELS_PER_SAMPLE;
  146. }
  147. }
  148. /* Format and des are valid, excluding image type considerations.
  149. Validated based on type; allow user type only if caller said to.
  150. Note that no check is made on nBitsPerSample based on type.
  151. That is, it is allowed to create a bitonal image with 8 bits/pixel.
  152. It's just that the IL (probably) will never provide a conversion
  153. to 1 bit/pixel, so IL intrinsic functions are used on this image
  154. an error will occur - but client pipe elements could work on that image.
  155. */
  156. switch (pDes->type) {
  157. case IL_BITONAL:
  158. if (nSamplesPerPixel != 1)
  159. return IL_ERROR_SAMPLES_PER_PIXEL;
  160. break;
  161. case IL_PALETTE:
  162. if (pDes->nLevelsPerSample[0] > 256)
  163. return IL_ERROR_LEVELS_PER_SAMPLE;
  164. case IL_GRAY:
  165. if (nSamplesPerPixel != 1)
  166. return IL_ERROR_SAMPLES_PER_PIXEL;
  167. break;
  168. case IL_RGB:
  169. if (nSamplesPerPixel != 3)
  170. return IL_ERROR_SAMPLES_PER_PIXEL;
  171. break;
  172. /* Validate YCbCr data; subsamples must be 1, 2 or 4, refWhite > refBlack.
  173. Image must be planar, 8 bits per sample.
  174. */
  175. case IL_YCBCR:
  176. if (nSamplesPerPixel != 3)
  177. return IL_ERROR_SAMPLES_PER_PIXEL;
  178. pSample = pDes->typeInfo.YCbCr.sample;
  179. i = 3;
  180. while (i-- > 0) {
  181. j = pSample->subsampleHoriz;
  182. if ((j != 1) && (j != 2) && (j != 4))
  183. return IL_ERROR_YCBCR_DATA;
  184. j = pSample->subsampleVert;
  185. if ((j != 1) && (j != 2) && (j != 4))
  186. return IL_ERROR_YCBCR_DATA;
  187. if (pSample->refBlack >= pSample->refWhite)
  188. return IL_ERROR_YCBCR_DATA;
  189. pSample++;
  190. }
  191. if (pDes->typeInfo.YCbCr.positioning != 0)
  192. return IL_ERROR_YCBCR_DATA;
  193. if (pFormat) {
  194. if ((pFormat->nBitsPerSample[0] != 8)
  195. || (pFormat->nBitsPerSample[1] != 8)
  196. || (pFormat->nBitsPerSample[2] != 8))
  197. return IL_ERROR_YCBCR_FORMAT;
  198. }
  199. break;
  200. /* Unknown type: error or ok if private types allowed.
  201. */
  202. default:
  203. return (allowPrivateTypes) ? IL_OK : IL_ERROR_IMAGE_TYPE;
  204. break;
  205. }
  206. /* Validate compression: not a user image type (exited above)
  207. */
  208. if (!IL_VALID_COMPRESSION (pDes->compression, pDes->type))
  209. return IL_ERROR_COMPRESSION;
  210. return IL_OK;
  211. }
  212. /* ----------------------- ilGetBytesPerRow ------------------------------ */
  213. /* Public function: see spec.
  214. Returns the # bytes / row into "pnBytesPerRow" (for each plane of image),
  215. for an image of size "width", with "nSamplesPerPixel" (from des) and the
  216. format "*pFormat".
  217. */
  218. void ilGetBytesPerRow (
  219. const ilImageDes *pDes,
  220. const ilImageFormat *pFormat,
  221. long width,
  222. long *pnBytesPerRow /* RETURNED */
  223. )
  224. {
  225. const unsigned short *pNBits;
  226. int rowBitAlign, nBits, nSamplesPerPixel, i;
  227. pNBits = pFormat->nBitsPerSample;
  228. rowBitAlign = pFormat->rowBitAlign;
  229. if (rowBitAlign < 8)
  230. rowBitAlign = 8;
  231. nSamplesPerPixel = pDes->nSamplesPerPixel;
  232. /* Calculate nBytesPerRow: # bits * width, rounded up by rowBitAlign.
  233. For 8 < bits/pixel <= 16, use word/pixel; <= 32, use long (TIFF std).
  234. Allocation size for each plane is then nBytesPerRow * height of image.
  235. If a YCBCrImage, take horizontal subsampling into account.
  236. */
  237. if (pFormat->sampleOrder == IL_SAMPLE_PIXELS) {
  238. /* Pixel order: one row contains all of the bits for each pixel, in sample
  239. order. Sum the bits for one pixel; always start on a byte boundary for
  240. samples > 8 bits.
  241. */
  242. nBits = 0;
  243. while (nSamplesPerPixel-- > 0) {
  244. i = *pNBits++;
  245. if (i > 8) {
  246. nBits = ((nBits + 7) / 8) * 8; /* round nBits to byte boundary */
  247. if (i <= 16)
  248. i = 16;
  249. else i = 32;
  250. }
  251. nBits += i;
  252. }
  253. *pnBytesPerRow = ((nBits * width + rowBitAlign - 1) / rowBitAlign)
  254. * (rowBitAlign / 8);
  255. }
  256. else {
  257. const ilYCbCrSampleInfo *pYCbCr;
  258. /* Planar order: each plane is independent; return nSamplesPerPixel rowbytes.
  259. Take into account subsampling if YCbCr image.
  260. */
  261. pYCbCr = (pDes->type == IL_YCBCR) ?
  262. pDes->typeInfo.YCbCr.sample : (ilYCbCrSampleInfo *)NULL;
  263. while (nSamplesPerPixel-- > 0) {
  264. nBits = *pNBits++;
  265. if (nBits > 8) {
  266. if (nBits <= 16)
  267. nBits = 16;
  268. else nBits = 32;
  269. }
  270. if (pYCbCr) {
  271. i = width >> _ilSubsampleShift[pYCbCr->subsampleHoriz];
  272. if (i <= 0) i = 1;
  273. pYCbCr++;
  274. }
  275. else i = width;
  276. *pnBytesPerRow++ = ((nBits * i + rowBitAlign - 1) / rowBitAlign)
  277. * (rowBitAlign / 8);
  278. }
  279. }
  280. }
  281. /* ------------------------ ilMallocImagePixels ---------------------------- */
  282. /* For uncompressed images: create a buffer for the pixels of image "*pImage",
  283. based on pImage->des, pImage->format, and pImage->height.
  284. For compressed images: just null pPixels and bufferSize; code that
  285. writes to a compressed image must check the bufferSize and malloc (first
  286. time) or realloc as necessary. Set ownPixels to true in either case.
  287. Returns: zero if success, else error code.
  288. */
  289. IL_PRIVATE ilError _ilMallocImagePixels (
  290. ilImagePtr pImage
  291. )
  292. {
  293. int plane, nPlanes;
  294. int MCUMinus1 = 0;
  295. long nBytesToAlloc, nBytesPerRow, allocHeight;
  296. ilPtr pPixels;
  297. long rowBytesPerPlane [IL_MAX_SAMPLES];
  298. ilYCbCrSampleInfo *pYCbCr;
  299. /* Get the bytes per row, set into the image. */
  300. ilGetBytesPerRow (&pImage->des, &pImage->format, pImage->i.width, rowBytesPerPlane);
  301. /* Alloc each plane; on failure, backtrack and free those already created.
  302. Don't allocate buffer for compressed image!
  303. If YCbCr image point pYCbCr and calc maxVertSubsample (1,2,4) and
  304. MCU (Minimum Coded Unit) = 8 * maxVertSubsample.
  305. */
  306. nPlanes = (pImage->format.sampleOrder == IL_SAMPLE_PIXELS) ? 1 :
  307. pImage->des.nSamplesPerPixel;
  308. if (pImage->des.type == IL_YCBCR) {
  309. pYCbCr = pImage->des.typeInfo.YCbCr.sample;
  310. MCUMinus1 = pYCbCr[0].subsampleVert;
  311. if (pYCbCr[1].subsampleVert > MCUMinus1)
  312. MCUMinus1 = pYCbCr[1].subsampleVert;
  313. if (pYCbCr[2].subsampleVert > MCUMinus1)
  314. MCUMinus1 = pYCbCr[2].subsampleVert;
  315. MCUMinus1 = MCUMinus1 * 8 - 1; /* e.g. if max = 4 MCUMinus1 is 31 */
  316. }
  317. else pYCbCr = (ilYCbCrSampleInfo *)NULL;
  318. for (plane = 0; plane < nPlanes; plane++) {
  319. nBytesPerRow = rowBytesPerPlane [plane];
  320. if (pImage->des.compression != IL_UNCOMPRESSED)
  321. nBytesToAlloc = 0;
  322. else {
  323. /* If YCbCr image, alloc smaller buffer (divide by subsample) but
  324. first rounded up to the MCU. For example, if max subsample is 2,
  325. an make unsubsampled buffer a mult of 16; a 2 subsampled mult of 8.
  326. "allocHeight" is height rounded up to MCU / vertSubsample.
  327. */
  328. if (pYCbCr) {
  329. allocHeight = ((pImage->i.height + MCUMinus1) & ~MCUMinus1)
  330. >> _ilSubsampleShift[pYCbCr->subsampleVert];
  331. pYCbCr++;
  332. }
  333. else allocHeight = pImage->i.height;
  334. nBytesToAlloc = nBytesPerRow * allocHeight;
  335. }
  336. if (!nBytesToAlloc)
  337. pPixels = (ilPtr)NULL;
  338. else {
  339. pPixels = (ilPtr)IL_MALLOC (nBytesToAlloc);
  340. if (!pPixels) { /* error; free those already created */
  341. while ((plane != 0)) {
  342. plane--;
  343. if ((pPixels = pImage->i.plane[plane].pPixels))
  344. IL_FREE (pPixels);
  345. }
  346. pImage->i.clientPixels = TRUE; /* so IL does not deallocate them */
  347. return IL_ERROR_MALLOC; /* EXIT */
  348. }
  349. }
  350. pImage->i.plane[plane].pPixels = pPixels;
  351. pImage->i.plane[plane].nBytesPerRow = nBytesPerRow;
  352. pImage->i.plane[plane].bufferSize = nBytesToAlloc;
  353. } /* END each plane */
  354. pImage->i.clientPixels = FALSE;
  355. return IL_OK;
  356. }
  357. /* ------------------------ ilFreeImagePixels ----------------------------- */
  358. /* Destroy() function for client and internal image objects.
  359. Also used to discard pixels when writing images or loading client images.
  360. Free the pixels of the image only if the IL owns them.
  361. */
  362. IL_PRIVATE void _ilFreeImagePixels (
  363. ilImagePtr pImage
  364. )
  365. {
  366. int plane;
  367. ilPtr pPixels;
  368. /* If the IL owns the pixels (it does not if "clientPixels" true to
  369. ilCreateClientImage()), then free them if non-null.
  370. If pixel sample order, just one plane, else nSamplesPerPixel planes.
  371. */
  372. if (!pImage->i.clientPixels) {
  373. for (plane = 0; plane < IL_NPLANES(pImage); plane++) {
  374. pPixels = pImage->i.plane[plane].pPixels;
  375. if (pPixels)
  376. IL_FREE (pPixels);
  377. }
  378. pImage->i.clientPixels = TRUE; /* so IL does not deallocate them */
  379. }
  380. /* If own palette and it is non-null free it. */
  381. if (!pImage->i.clientPalette && pImage->i.pPalette) {
  382. IL_FREE (pImage->i.pPalette);
  383. pImage->i.clientPalette = TRUE;
  384. }
  385. /* If own comp data and it is non-null free it and associated (JPEG) tables */
  386. if (!pImage->i.clientCompData && pImage->i.pCompData) {
  387. if (pImage->des.compression == IL_JPEG)
  388. _ilJPEGFreeTables ((ilJPEGData *)pImage->i.pCompData);
  389. IL_FREE (pImage->i.pCompData);
  390. pImage->i.pCompData = (ilPtr)NULL;
  391. pImage->i.clientCompData = TRUE;
  392. }
  393. /* If strip offset array non-null free it */
  394. if (pImage->pStripOffsets) {
  395. IL_FREE (pImage->pStripOffsets);
  396. pImage->pStripOffsets = (long *)NULL;
  397. }
  398. }
  399. /* ------------------------ ilAllocStripOffsets ----------------------------- */
  400. /* For compressed images only: allocate strip offsets array (pStripOffsets)
  401. using the given "stripHeight", or use the default strip height if "stripHeight"
  402. is zero (0). Locks image to that strip height.
  403. */
  404. IL_PRIVATE ilBool _ilAllocStripOffsets (
  405. ilImagePtr pImage,
  406. long stripHeight
  407. )
  408. {
  409. long nStrips;
  410. if (stripHeight <= 0)
  411. stripHeight = ilRecommendedStripHeight (pImage->i.pDes, pImage->i.pFormat,
  412. pImage->i.width, pImage->i.height);
  413. if (stripHeight > pImage->i.height)
  414. stripHeight = pImage->i.height;
  415. nStrips = (pImage->i.height + stripHeight - 1) / stripHeight;
  416. pImage->pStripOffsets = (long *)IL_MALLOC_ZERO ((nStrips + 1) * sizeof(long));
  417. if (!pImage->pStripOffsets) {
  418. pImage->o.p.context->error = IL_ERROR_MALLOC;
  419. return FALSE;
  420. }
  421. pImage->nStrips = nStrips;
  422. pImage->stripHeight = stripHeight;
  423. pImage->o.p.context->error = IL_OK;
  424. return TRUE;
  425. }
  426. /* ---------------------- ilCreateInternalImage --------------------------- */
  427. /* Public function: see spec.
  428. */
  429. ilInternalImage ilCreateInternalImage (
  430. ilContext context,
  431. long width,
  432. long height,
  433. ilImageDes *pDes,
  434. unsigned long mustBeZero
  435. )
  436. {
  437. ilImagePtr pImage;
  438. ilError error;
  439. if (mustBeZero != 0) {
  440. context->error = IL_ERROR_PAR_NOT_ZERO;
  441. return (ilInternalImage)NULL;
  442. }
  443. /* Null (zero-sized) images not allowed; exit w/ error if so. */
  444. if ((width <= 0) || (height <= 0)) {
  445. context->error = IL_ERROR_ZERO_SIZE_IMAGE;
  446. return (ilInternalImage)NULL;
  447. }
  448. /* Validate pDes: don't allow user-defined types. */
  449. if ((context->error = _ilValidateDesFormat (FALSE, pDes, (ilImageFormat *)NULL)))
  450. return (ilInternalImage)NULL;
  451. /* Create the object and add it to context. */
  452. pImage = (ilImagePtr)_ilCreateObject (context, IL_INTERNAL_IMAGE, _ilFreeImagePixels,
  453. sizeof (ilImageRec));
  454. if (!pImage)
  455. return (ilInternalImage)NULL;
  456. /* Fill in the image structure, excluding object header (.o).
  457. Init the format to std format, based on image type, which is not private type.
  458. For bits per sample (other than bitonal): use 4 bits if levels/sample <= 16,
  459. else use 8 bits. Could use other values (e.g. min # bits for levels/sample),
  460. but at this point there are no conversions other than for 4<->8 bits.
  461. */
  462. pImage->des = *pDes;
  463. switch (pDes->type) {
  464. case IL_BITONAL:
  465. pImage->format = *IL_FORMAT_BIT;
  466. break;
  467. case IL_GRAY:
  468. case IL_PALETTE:
  469. pImage->format = *IL_FORMAT_BYTE;
  470. if (pDes->nLevelsPerSample[0] <= 16)
  471. pImage->format.nBitsPerSample[0] = 4;
  472. break;
  473. case IL_RGB:
  474. pImage->format = *IL_FORMAT_3BYTE_PIXEL;
  475. if ((pDes->nLevelsPerSample[0] <= 16)
  476. && (pDes->nLevelsPerSample[1] <= 16)
  477. && (pDes->nLevelsPerSample[2] <= 16))
  478. pImage->format.nBitsPerSample[0] = pImage->format.nBitsPerSample[1] =
  479. pImage->format.nBitsPerSample[2] = 4;
  480. break;
  481. /* YCbCr: always use 8 bits / pixel. If uncompressed use planar format,
  482. but if compressed use pixel (e.g. JPEG interleaved format).
  483. */
  484. case IL_YCBCR:
  485. pImage->format = (pDes->compression == IL_UNCOMPRESSED) ?
  486. *IL_FORMAT_3BYTE_PLANE : *IL_FORMAT_3BYTE_PIXEL;
  487. break;
  488. default:
  489. context->error = IL_ERROR_IMAGE_TYPE;
  490. return (ilInternalImage)NULL;
  491. }
  492. /* Init image data, allocate pixels if uncompressed */
  493. pImage->nStrips = 0; /* if compressed, strip height not set yet */
  494. pImage->stripHeight = 0;
  495. pImage->pStripOffsets = (long *)NULL;
  496. pImage->i.pDes = &pImage->des;
  497. pImage->i.pFormat = &pImage->format;
  498. pImage->i.width = width;
  499. pImage->i.height = height;
  500. pImage->i.pCompData = (ilPtr)NULL;
  501. pImage->i.clientCompData = TRUE;
  502. pImage->i.pPalette = (unsigned short *)NULL;
  503. pImage->i.clientPalette = TRUE;
  504. if ((error = _ilMallocImagePixels (pImage))) {
  505. ilDestroyObject ((ilObject)pImage);
  506. context->error = error;
  507. return (ilInternalImage)NULL;
  508. }
  509. /* If a palette image, create an empty palette. */
  510. if (pDes->type == IL_PALETTE) {
  511. pImage->i.pPalette = (unsigned short *)
  512. IL_MALLOC_ZERO (sizeof (unsigned short) * (3 * 256));
  513. if (!pImage->i.pPalette) {
  514. ilDestroyObject ((ilObject)pImage);
  515. context->error = IL_ERROR_MALLOC;
  516. return (ilInternalImage)NULL;
  517. }
  518. pImage->i.clientPalette = FALSE;
  519. }
  520. /* If raw JPEG compressed, point pCompData to allocated JPEG info block, else
  521. init pCompData to null, owned by client (i.e. not freed when destroyed).
  522. */
  523. if ((pDes->compression == IL_JPEG) && (pDes->compInfo.JPEG.reserved & IL_JPEGM_RAW)) {
  524. if (!(pImage->i.pCompData = (ilPtr)IL_MALLOC_ZERO (sizeof (ilJPEGData)))) {
  525. ilDestroyObject ((ilObject)pImage);
  526. context->error = IL_ERROR_MALLOC;
  527. return (ilInternalImage)NULL;
  528. }
  529. pImage->i.clientCompData = FALSE; /* IL must now destroy comp data */
  530. }
  531. context->error = IL_OK;
  532. return (ilInternalImage)pImage;
  533. }
  534. /* ------------------------ ilQueryInternalImage ----------------------------- */
  535. /* Public function: see spec.
  536. */
  537. ilBool ilQueryInternalImage (
  538. ilInternalImage image,
  539. long *pWidth, /* RETURNED */
  540. long *pHeight, /* RETURNED */
  541. ilImageDes *pDes, /* RETURNED */
  542. unsigned long mustBeZero
  543. )
  544. {
  545. ilImagePtr pImage;
  546. /* Check that given image is an internal image.
  547. */
  548. pImage = (ilImagePtr)image;
  549. if (mustBeZero != 0) {
  550. pImage->o.p.context->error = IL_ERROR_PAR_NOT_ZERO;
  551. return 0; /* EXIT */
  552. }
  553. if (pImage->o.p.objectType != IL_INTERNAL_IMAGE) {
  554. pImage->o.p.context->error = IL_ERROR_OBJECT_TYPE;
  555. return 0; /* EXIT */
  556. }
  557. pImage->o.p.context->error = IL_OK;
  558. /* Right type of image: return values to non-null ptrs. */
  559. if (pWidth)
  560. *pWidth = pImage->i.width;
  561. if (pHeight)
  562. *pHeight = pImage->i.height;
  563. if (pDes)
  564. *pDes = pImage->des;
  565. return TRUE;
  566. }
  567. /* ---------------------- ilCreateClientImage --------------------------- */
  568. /* Public function: see spec.
  569. */
  570. ilClientImage ilCreateClientImage (
  571. ilContext context,
  572. ilImageInfo *pInfo,
  573. unsigned long mustBeZero
  574. )
  575. {
  576. ilImagePtr pImage;
  577. ilError error;
  578. int plane, nPlanes;
  579. if (mustBeZero != 0) {
  580. context->error = IL_ERROR_PAR_NOT_ZERO;
  581. return (ilClientImage)NULL;
  582. }
  583. /* Null (zero-sized) images not allowed; exit w/ error if so. */
  584. if ((pInfo->width <= 0) || (pInfo->height <= 0)) {
  585. context->error = IL_ERROR_ZERO_SIZE_IMAGE;
  586. return (ilClientImage)NULL;
  587. }
  588. /* Validate pDes and pFormat: do allow user-defined types. */
  589. if ((context->error = _ilValidateDesFormat (TRUE, pInfo->pDes, pInfo->pFormat)))
  590. return (ilClientImage)NULL;
  591. /* Create the object and add it to context. */
  592. pImage = (ilImagePtr)_ilCreateObject (context, IL_CLIENT_IMAGE, _ilFreeImagePixels,
  593. sizeof (ilImageRec));
  594. if (!pImage)
  595. return (ilClientImage)NULL;
  596. /* Fill in the image structure, excluding object header (.o).
  597. Copy caller's des, and if "raw" JPEG force to non-raw. Client must be
  598. able to access image, and non-raw (JIF) format only meaningful way to do so.
  599. */
  600. pImage->des = *pInfo->pDes;
  601. if (pImage->des.compression == IL_JPEG)
  602. pImage->des.compInfo.JPEG.reserved = 0;
  603. pImage->format = *pInfo->pFormat;
  604. pImage->nStrips = 0; /* if compressed, no data present yet */
  605. pImage->stripHeight = 0;
  606. pImage->pStripOffsets = (long *)NULL;
  607. pImage->i.pDes = &pImage->des;
  608. pImage->i.pFormat = &pImage->format;
  609. pImage->i.width = pInfo->width;
  610. pImage->i.height = pInfo->height;
  611. pImage->i.pPalette = (unsigned short *)NULL; /* assume no palette or comp data */
  612. pImage->i.clientPalette = TRUE;
  613. pImage->i.pCompData = (ilPtr)NULL;
  614. pImage->i.clientCompData = TRUE;
  615. pImage->i.clientPixels = pInfo->clientPixels;
  616. /* If a palette image, point to the one from the caller if pPalette not null;
  617. if null, create one here and the IL owns it.
  618. */
  619. if (pImage->des.type == IL_PALETTE) {
  620. if (pInfo->pPalette && pInfo->clientPalette) {
  621. pImage->i.pPalette = pInfo->pPalette;
  622. pImage->i.clientPalette = TRUE;
  623. }
  624. else {
  625. pImage->i.pPalette = (unsigned short *)
  626. IL_MALLOC_ZERO (sizeof (unsigned short) * (3 * 256));
  627. if (!pImage->i.pPalette) {
  628. ilDestroyObject ((ilObject)pImage);
  629. context->error = IL_ERROR_MALLOC;
  630. return (ilClientImage)NULL;
  631. }
  632. pImage->i.clientPalette = FALSE;
  633. }
  634. }
  635. /* multi-plane planar compressed images not supported */
  636. nPlanes = IL_NPLANES(pImage);
  637. if ((pImage->des.compression != IL_UNCOMPRESSED) && (nPlanes > 1)) {
  638. ilDestroyObject ((ilObject)pImage);
  639. context->error = IL_ERROR_SAMPLE_ORDER;
  640. return (ilClientImage)NULL;
  641. }
  642. /* If caller providing pixels (clientPixels == true), point to them,
  643. otherwise allocate buffer space; if failure delete object and return error.
  644. */
  645. if (pInfo->clientPixels) {
  646. for (plane = 0; plane < nPlanes; plane++)
  647. pImage->i.plane[plane] = pInfo->plane[plane];
  648. }
  649. else { /* no pixels from client; allocate them here if uncompressed image */
  650. if ((error = _ilMallocImagePixels (pImage))) {
  651. ilDestroyObject ((ilObject)pImage);
  652. context->error = error;
  653. return (ilClientImage)NULL;
  654. }
  655. }
  656. /* Compressed client images stored as one big strip; allocate strip offset array.
  657. If client pixels, offset[0] = 0, offset[1] = nBytes - all data in 1 strip.
  658. */
  659. if (pImage->des.compression != IL_UNCOMPRESSED) {
  660. if (!_ilAllocStripOffsets (pImage, pImage->i.height)) {
  661. error = context->error;
  662. ilDestroyObject ((ilObject)pImage);
  663. context->error = error;
  664. return (ilClientImage)NULL;
  665. }
  666. if (pInfo->clientPixels) {
  667. pImage->pStripOffsets[0] = 0;
  668. pImage->pStripOffsets[1] = pImage->i.plane[0].bufferSize;
  669. }
  670. }
  671. context->error = IL_OK;
  672. return (ilClientImage)pImage;
  673. }
  674. /* ------------------------ ilQueryClientImage ----------------------------- */
  675. /* Public function: see spec.
  676. */
  677. ilBool ilQueryClientImage (
  678. ilClientImage image,
  679. ilImageInfo **ppInfo, /* RETURNED */
  680. unsigned long mustBeZero
  681. )
  682. {
  683. ilImagePtr pImage;
  684. /* Validate given image of correct type; return false if not realized.
  685. */
  686. pImage = (ilImagePtr)image;
  687. if (mustBeZero != 0) {
  688. pImage->o.p.context->error = IL_ERROR_PAR_NOT_ZERO;
  689. return FALSE;
  690. }
  691. if (pImage->o.p.objectType != IL_CLIENT_IMAGE) {
  692. pImage->o.p.context->error = IL_ERROR_OBJECT_TYPE;
  693. return FALSE;
  694. }
  695. if (ppInfo)
  696. *ppInfo = &pImage->i;
  697. pImage->o.p.context->error = IL_OK;
  698. return TRUE;
  699. }