iltiff.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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: iltiff.c /main/6 1996/10/04 11:29:48 rcs $ */
  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/iltiff.c : General TIFF file access functions, including
  40. those exposed in /ilc/iltiffint.h to other /ilc/iltiff*.c code.
  41. */
  42. #include <stdlib.h>
  43. #include "iltiffint.h"
  44. #include "ilerrors.h"
  45. #ifdef DTLIB
  46. /* Used when this module is compiled by the DtHelp library build */
  47. #include "GraphicsP.h"
  48. #endif /* DTLIB */
  49. /* Size in bytes of one item (# items = TIFF "length"), for each TIFF tag type.
  50. Also, # of entries of that type which fit in one long.
  51. Indexed by tag type = 1..IL_MAX_TAG_TYPE (0 entry unused).
  52. */
  53. IL_PRIVATE int _ilTagTypeItemSizes [IL_MAX_TAG_TYPE+1] = {
  54. 0, /* 0th entry unused; invalid tag type */
  55. 1, /* IL_TAG_BYTE */
  56. 1, /* IL_TAG_ASCII */
  57. 2, /* IL_TAG_SHORT */
  58. 4, /* IL_TAG_LONG */
  59. 8 }; /* IL_TAG_RATIONAL */
  60. IL_PRIVATE int _ilTagTypeItemsThatFit [IL_MAX_TAG_TYPE+1] = {
  61. 0, /* 0th entry unused; invalid tag type */
  62. 4, /* IL_TAG_BYTE */
  63. 4, /* IL_TAG_ASCII */
  64. 2, /* IL_TAG_SHORT */
  65. 1, /* IL_TAG_LONG */
  66. 0 }; /* IL_TAG_RATIONAL */
  67. /* Structure of the first 8 bytes of a TIFF file: see ilConnectFile() */
  68. typedef struct {
  69. ilByte byteOrder[2];
  70. ilByte version[2];
  71. ilByte IFDHeadOffset[4];
  72. } ilTIFFHeader;
  73. /* ------------------------- ilFreeFileList -------------------------------- */
  74. /* Free the file list in the given file "*pFile".
  75. Note: ilFreeFileList() is also the Destroy() function for ilFile object.
  76. */
  77. static void ilFreeFileList (
  78. ilFilePtr pFile
  79. )
  80. {
  81. ilFileImagePtr pFileImage, pAltFileImage, pFileImageTemp;
  82. /* Run thru the list of file images; for each: if a mask image destroy it
  83. (only one); if child images, destroy them - they are linked by pChild.
  84. */
  85. pFileImage = pFile->pFileImageHead;
  86. while (pFileImage) {
  87. /* Destroy the mask image if present */
  88. pAltFileImage = (ilFileImagePtr)pFileImage->p.pMask;
  89. if (pAltFileImage)
  90. IL_FREE (pAltFileImage);
  91. /* Destroy the child images if present. They are linked
  92. by pNext, and each one can have a mask image.
  93. */
  94. pAltFileImage = (ilFileImagePtr)pFileImage->p.pChild;
  95. while (pAltFileImage) {
  96. if (pAltFileImage->p.pMask)
  97. IL_FREE (pAltFileImage->p.pMask);
  98. pFileImageTemp = pAltFileImage;
  99. pAltFileImage = (ilFileImagePtr)pAltFileImage->p.pNext;
  100. IL_FREE (pFileImageTemp);
  101. }
  102. /* Destroy the main image */
  103. pFileImageTemp = pFileImage;
  104. pFileImage = (ilFileImagePtr)pFileImage->p.pNext;
  105. IL_FREE (pFileImageTemp);
  106. }
  107. /* Make the list empty and declare it invalid.
  108. */
  109. pFile->pFileImageHead = pFile->pFileImageTail = (ilFileImagePtr)NULL;
  110. pFile->haveImageList = FALSE;
  111. }
  112. /* ----------------------- ilReadFileTags --------------------------------- */
  113. /* Public function: see spec.
  114. */
  115. ilPtr ilReadFileTags (
  116. ilFileImage fileImage,
  117. int nTagsInit,
  118. unsigned short *pTagNumbers,
  119. ilFileTag **ppTags,
  120. unsigned long mustBeZero
  121. )
  122. {
  123. #define RFT_FATAL_ERROR(_error) {error = (_error); goto RFTFatalError; }
  124. ilFileImagePtr pFileImage;
  125. ilTIFFTagPtr pFileTag, pFileTags;
  126. ilError error;
  127. ilBool tagFound;
  128. unsigned long readSize, mallocSize;
  129. CARD32 offset;
  130. ilPtr pTagData, pTagAlloc;
  131. ilFilePtr pFile;
  132. ilFileTag *pTag, **ppTag;
  133. int tagNumber, nFileTags, nItems, nTags, tagType, i;
  134. ilBool bigEndian;
  135. pFileImage = (ilFileImagePtr)fileImage;
  136. if (mustBeZero != 0) {
  137. pFileImage->context->error = IL_ERROR_PAR_NOT_ZERO;
  138. return (ilPtr)NULL; /* EXIT */
  139. }
  140. if (nTagsInit <= 0) {
  141. pFileImage->context->error = IL_OK;
  142. return (ilPtr)NULL; /* no tags; EXIT */
  143. }
  144. pFile = (ilFilePtr)pFileImage->p.file;
  145. bigEndian = pFile->bigEndian;
  146. /* Allocate space and read in all the file tags to "*pFileTags".
  147. Flip the bytes of number, type and length, but not the data/offset.
  148. */
  149. pTagAlloc = (ilPtr)NULL; /* in case of fatal error */
  150. readSize = pFileImage->tagCount * IL_TAG_SIZE;
  151. pFileTags = (ilTIFFTagPtr)IL_MALLOC (readSize);
  152. if (!pFileTags)
  153. RFT_FATAL_ERROR (IL_ERROR_MALLOC)
  154. if (!IL_SEEK (pFile, pFileImage->tagOffset)
  155. || !IL_READ (pFile, readSize, pFileTags))
  156. RFT_FATAL_ERROR (IL_ERROR_FILE_IO)
  157. pFileTag = pFileTags;
  158. pTagData = (ilPtr)pFileTag;
  159. nFileTags = pFileImage->tagCount;
  160. while (nFileTags-- > 0) {
  161. IL_FLIP_SHORT (bigEndian, pTagData, pFileTag->number)
  162. pTagData += 2;
  163. IL_FLIP_SHORT (bigEndian, pTagData, tagType)
  164. /* NOTE: declare fatal error if tag type out of range, BUT TIFF 6.0 says
  165. we should ignore these tags; ALSO, there are new tag types for 6.0.
  166. */
  167. if ((tagType == 0) || (tagType > IL_MAX_TAG_TYPE))
  168. RFT_FATAL_ERROR (IL_ERROR_FILE_TAG_TYPE)
  169. pFileTag->type = tagType;
  170. pTagData += 2;
  171. IL_FLIP_LONG (bigEndian, pTagData, pFileTag->length)
  172. pTagData += 8; /* skip length and data */
  173. pFileTag++;
  174. }
  175. /* For each of caller's tags: find *pTagNumber in the pFileTags list.
  176. If not found, set corresponding "ppTags" entry to null, else point it
  177. at the found entry in pFileTags list, add its data size (rounded up to
  178. multiple of 4 - long-align), and the size of one ilFileTag, to "mallocSize".
  179. */
  180. mallocSize = 0;
  181. ppTag = ppTags;
  182. nTags = nTagsInit;
  183. while (nTags-- > 0) {
  184. tagNumber = *pTagNumbers++;
  185. tagFound = FALSE;
  186. pFileTag = pFileTags;
  187. nFileTags = pFileImage->tagCount;
  188. while (nFileTags-- > 0) {
  189. if (pFileTag->number == tagNumber) {
  190. tagFound = TRUE;
  191. break;
  192. }
  193. else pFileTag++;
  194. }
  195. if (tagFound) {
  196. mallocSize += (sizeof (ilFileTag) +
  197. (pFileTag->length * _ilTagTypeItemSizes [pFileTag->type] + 3)) & ~3;
  198. *ppTag++ = (ilFileTag *)pFileTag;
  199. }
  200. else *ppTag++ = (ilFileTag *)NULL;
  201. }
  202. /* If no tags found; exit, else allocate "mallocSize" bytes, and for each found
  203. tag, point corresponding "ppTags" to spot in pTagAlloc that has the ilFileTag
  204. data, followed by the data itself, then long aligned.
  205. What is returned will look like:
  206. ppTags [0] == null if tag not present, else ->:
  207. tag data: byte offset
  208. number (same value as corresponding 0
  209. entry in pTagsNumbers)
  210. type 2
  211. nItems 4
  212. pItems (points to <items>, i.e. one 8
  213. long past this spot, or null if
  214. "nItems" is zero (0).
  215. <items> - "nItems" occurrences of data 12
  216. depending on "type".
  217. ppTags [1]-> <next tag, at offset 12 + ppTag[0]->nItems
  218. * size in bytes of one ppTag[0] item,
  219. as determined by "type".
  220. */
  221. if (mallocSize == 0) {
  222. IL_FREE (pFileTags);
  223. pFileImage->context->error = IL_OK;
  224. return (ilPtr)NULL; /* EXIT */
  225. }
  226. pTagAlloc = (ilPtr)malloc (mallocSize); /* caller uses free() to free this */
  227. if (!pTagAlloc)
  228. RFT_FATAL_ERROR (IL_ERROR_MALLOC)
  229. pTag = (ilFileTag *)pTagAlloc;
  230. nTags = nTagsInit;
  231. ppTag = ppTags;
  232. while (nTags-- > 0) {
  233. if (!*ppTag)
  234. ppTag++; /* tag not found, already null, next */
  235. else { /* tag found */
  236. pFileTag = (ilTIFFTagPtr)*ppTag; /* points to internal tag data */
  237. *ppTag++ = pTag; /* point to converted data, next tag */
  238. pTag->number = pFileTag->number; /* copy from pFileTags to pTagAlloc */
  239. tagType = pTag->type = pFileTag->type;
  240. nItems = pTag->nItems = pFileTag->length;
  241. pTagData = (ilPtr)pTag + sizeof(ilFileTag);
  242. if (nItems == 0) {
  243. pTag->pItems = (ilPtr)NULL;
  244. pTag = (ilFileTag *)pTagData; /* point pTag to spot for next tag */
  245. }
  246. else {
  247. pTag->pItems = pTagData; /* data follows the tag */
  248. pTag = (ilFileTag *)(pTagData + /* next spot = past data, long-aligned */
  249. ((nItems * _ilTagTypeItemSizes [tagType] + 3) & ~3));
  250. /* Byte flip the tag data to the spot pointed to by "pTagData",
  251. i.e. right after the 12 bytes for the tag. In TIFF, the data
  252. is in the "offset" field tag if it fits in 4 bytes, else offset
  253. points to the data in the file - seek and read it in that case.
  254. */
  255. switch (tagType) {
  256. case IL_TAG_BYTE:
  257. case IL_TAG_ASCII:
  258. if (nItems <= 4)
  259. bcopy (pFileTag->data.chars, (char *)pTagData, nItems);
  260. else {
  261. IL_FLIP_LONG (bigEndian, pFileTag->data.chars, offset)
  262. if (!IL_SEEK (pFile, offset)
  263. || !IL_READ (pFile, nItems, pTagData))
  264. RFT_FATAL_ERROR (IL_ERROR_FILE_IO)
  265. }
  266. break;
  267. case IL_TAG_SHORT:
  268. if (nItems <= 2) {
  269. for (i = 0; nItems > 0; nItems--, i += 2, pTagData += 2)
  270. IL_FLIP_SHORT (bigEndian, pFileTag->data.chars + i,
  271. *((unsigned short *)pTagData))
  272. }
  273. else {
  274. IL_FLIP_LONG (bigEndian, pFileTag->data.chars, offset)
  275. if (!IL_SEEK (pFile, offset)
  276. || !IL_READ (pFile, nItems<<1, pTagData))
  277. RFT_FATAL_ERROR (IL_ERROR_FILE_IO)
  278. for (; nItems > 0; nItems--, pTagData += 2)
  279. IL_FLIP_SHORT (bigEndian, pTagData, *((unsigned short *)pTagData))
  280. }
  281. break;
  282. case IL_TAG_LONG:
  283. if (nItems == 1)
  284. IL_FLIP_LONG (bigEndian, pFileTag->data.chars, *((INT32 *)pTagData))
  285. else {
  286. IL_FLIP_LONG (bigEndian, pFileTag->data.chars, offset)
  287. if (!IL_SEEK (pFile, offset)
  288. || !IL_READ (pFile, nItems<<2, pTagData))
  289. RFT_FATAL_ERROR (IL_ERROR_FILE_IO)
  290. for (; nItems > 0; nItems--, pTagData += 4)
  291. IL_FLIP_LONG (bigEndian, pTagData, *((INT32 *)pTagData))
  292. }
  293. break;
  294. case IL_TAG_RATIONAL:
  295. IL_FLIP_LONG (bigEndian, pFileTag->data.chars, offset)
  296. if (!IL_SEEK (pFile, offset) || !IL_READ (pFile, nItems<<3, pTagData))
  297. RFT_FATAL_ERROR (IL_ERROR_FILE_IO)
  298. while (nItems-- > 0) {
  299. IL_FLIP_LONG (bigEndian, pTagData, *((INT32 *)pTagData))
  300. pTagData += 4;
  301. IL_FLIP_LONG (bigEndian, pTagData, *((INT32 *)pTagData))
  302. pTagData += 4;
  303. }
  304. break;
  305. }
  306. } /* END nItems > 0 */
  307. } /* END tag found */
  308. } /* END each caller's tag */
  309. /* Tags read successfully: free pFileTags (local buffer); return pTagAlloc.
  310. */
  311. IL_FREE (pFileTags);
  312. pFileImage->context->error = IL_OK;
  313. return pTagAlloc; /* EXIT */
  314. /* Fatal error: free pFileTags, pTagAlloc if not null, return error code, null.
  315. */
  316. RFTFatalError:
  317. if (pFileTags)
  318. IL_FREE (pFileTags);
  319. if (pTagAlloc)
  320. free (pTagAlloc);
  321. pFileImage->context->error = error;
  322. return (ilPtr)NULL; /* EXIT */
  323. }
  324. /* --------------------- ilAddImageToList --------------------------------- */
  325. /* Called when forming file image list. "pPrevFileImage" is a pointer to the
  326. previous non-mask image in the file; null if pFileImage first non-mask read.
  327. pFileImage->imageType indicates whether image being added is
  328. child/mask/mainImage; if child or mask, pPrevFileImage must be non-null.
  329. */
  330. static void ilAddImageToList (
  331. ilFilePtr pFile,
  332. ilFileImagePtr pFileImage,
  333. ilFileImagePtr pPrevFileImage
  334. )
  335. {
  336. pFileImage->p.pNext = pFileImage->p.pPrev =
  337. pFileImage->p.pChild = pFileImage->p.pMask = (ilFileImage)NULL;
  338. switch (pFileImage->imageType) {
  339. case childImage:
  340. if (pPrevFileImage->imageType == childImage)
  341. pPrevFileImage->p.pNext = (ilFileImage)pFileImage;
  342. else pPrevFileImage->p.pChild = (ilFileImage)pFileImage;
  343. break;
  344. case maskImage:
  345. pPrevFileImage->p.pMask = (ilFileImage)pFileImage;
  346. break;
  347. /* Main image: if page # present, add to list in page order, else add to end */
  348. case mainImage:
  349. if (pFileImage->p.nPages < 0) { /* no page #; add to end of list */
  350. if ((pFileImage->p.pPrev = (ilFileImage)pFile->pFileImageTail))
  351. pFileImage->p.pPrev->pNext = (ilFileImage)pFileImage;
  352. else pFile->pFileImageHead = pFileImage;
  353. }
  354. else {
  355. /* Have page #. If list is empty or page # <= that of first element in
  356. list, add pFileImage to front of list; otherwise find place in list
  357. based on page # and insert image in that place.
  358. */
  359. pPrevFileImage = pFile->pFileImageHead;
  360. if (!pPrevFileImage || (pFileImage->p.page <= pPrevFileImage->p.page)) {
  361. pFileImage->p.pNext = (ilFileImage)pPrevFileImage; /* add to front */
  362. pFileImage->p.pPrev = (ilFileImage)NULL;
  363. pFile->pFileImageHead = pFileImage;
  364. }
  365. else { /* Add after pPrevFileImage w/ highest page # <= image's page # */
  366. while (pPrevFileImage->p.pNext
  367. && (pPrevFileImage->p.pNext->page <= pFileImage->p.page)) {
  368. pPrevFileImage = (ilFileImagePtr)pPrevFileImage->p.pNext;
  369. }
  370. pFileImage->p.pNext = pPrevFileImage->p.pNext;
  371. pFileImage->p.pPrev = (ilFileImage)pPrevFileImage;
  372. pPrevFileImage->p.pNext = (ilFileImage)pFileImage;
  373. }
  374. }
  375. /* Point next in list, or tail if this image is last, back to this image */
  376. if (pFileImage->p.pNext)
  377. pFileImage->p.pNext->pPrev = (ilFileImage)pFileImage;
  378. else pFile->pFileImageTail = pFileImage;
  379. break;
  380. }
  381. }
  382. /* --------------------- ilReadTagsAndAddImageToList ------------------------ */
  383. /* Look at the tag values in the given file image "*pFileImage", and if the tags
  384. indicate a supported image, add it to the list of file images in "*pFile".
  385. If image is not supported (e.g unknown compression type) the image is freed.
  386. ppPrevFileImage must point to the ptr to the previous image read from the
  387. file, or null if this is the first image; used for child and mask images.
  388. Return IL_OK if image valid, even if not supported; an error means
  389. the image is malformed (e.g. missing required tag).
  390. */
  391. static ilError ilReadTagsAndAddImageToList (
  392. ilFilePtr pFile,
  393. ilFileImagePtr *ppPrevFileImage,
  394. ilFileImagePtr pFileImage
  395. )
  396. {
  397. /* Indexes into "tagNumbers" below. NOTE: order is important, as tags are checked
  398. in the below order, and some tags default to values which must be before them.
  399. Also NOTE: check for type other than short/long and # items done below!
  400. */
  401. #define NEW_SUBFILE_TYPE 0
  402. #define IMAGE_WIDTH 1
  403. #define IMAGE_LENGTH 2
  404. #define RESOLUTION_UNIT 3
  405. #define X_RESOLUTION 4 /* follow RESOLUTION_UNIT */
  406. #define Y_RESOLUTION 5 /* follow RESOLUTION_UNIT */
  407. #define ROWS_PER_STRIP 6 /* follow IMAGE_WIDTH */
  408. #define SAMPLES_PER_PIXEL 7
  409. #define PLANAR_CONFIGURATION 8 /* follow SAMPLES_PER_PIXEL */
  410. #define BITS_PER_SAMPLE 9 /* follow SAMPLES_PER_PIXEL */
  411. #define FILL_ORDER 10
  412. #define GROUP_3_OPTIONS 11 /* follow FILL_ORDER */
  413. #define GROUP_4_OPTIONS 12 /* follow FILL_ORDER */
  414. #define COMPRESSION 13 /* follow GROUP_3/4_OPTIONS, FILL_ORDER */
  415. #define PREDICTOR 14 /* follow COMPRESSION */
  416. #define PHOTOMETRIC_INTERPRETATION 15 /* follow COMP, BITS_PS, SAMPLES_PP, NEW_SFT */
  417. #define PAGE_NUMBER 16 /* follow NEW_SFT */
  418. #define JPEG_PROC 17 /* follow COMPRESSION */
  419. #define YCBCR_COEFFICIENTS 18 /* follow PHOTO_INT */
  420. #define YCBCR_SUBSAMPLING 19 /* follow PHOTO_INT */
  421. #define YCBCR_POSITIONING 20 /* follow PHOTO_INT */
  422. #define REFERENCE_BLACK_WHITE 21 /* follow PHOTO_INT */
  423. #define PRIVATE_0 22 /* follow PHOTO_INT */
  424. static unsigned short tagNumbers [] = {
  425. IL_TAG_NEW_SUBFILE_TYPE,
  426. IL_TAG_IMAGE_WIDTH,
  427. IL_TAG_IMAGE_LENGTH,
  428. IL_TAG_RESOLUTION_UNIT,
  429. IL_TAG_X_RESOLUTION,
  430. IL_TAG_Y_RESOLUTION,
  431. IL_TAG_ROWS_PER_STRIP,
  432. IL_TAG_SAMPLES_PER_PIXEL,
  433. IL_TAG_PLANAR_CONFIGURATION,
  434. IL_TAG_BITS_PER_SAMPLE,
  435. IL_TAG_FILL_ORDER,
  436. IL_TAG_GROUP_3_OPTIONS,
  437. IL_TAG_GROUP_4_OPTIONS,
  438. IL_TAG_COMPRESSION,
  439. IL_TAG_PREDICTOR,
  440. IL_TAG_PHOTOMETRIC_INTERPRETATION,
  441. IL_TAG_PAGE_NUMBER,
  442. IL_TAG_JPEG_PROC,
  443. IL_TAG_YCBCR_COEFFICIENTS,
  444. IL_TAG_YCBCR_SUBSAMPLING,
  445. IL_TAG_YCBCR_POSITIONING,
  446. IL_TAG_REFERENCE_BLACK_WHITE,
  447. IL_TAG_PRIVATE_0 };
  448. #define AI_NTAGS (sizeof(tagNumbers) / sizeof (unsigned short))
  449. #define AIL_FATAL_ERROR(_error) {error = (_error); goto AILFatalError; }
  450. ilFileImageRelation imageType = mainImage;
  451. ilFileTag *tag [AI_NTAGS];
  452. ilBool present;
  453. ilError error;
  454. ilPtr pTagAlloc;
  455. unsigned long value;
  456. int i, tagIndex, nBits, resolutionUnit, fillOrder = 0;
  457. unsigned long group3CompData;
  458. unsigned long group4CompData;
  459. ilFileTag *pTag;
  460. ilYCbCrInfo *pYCbCr = NULL; /* null if not YCbCr ; else -> des...YCbCr */
  461. /* Init/zero des and format in *pFileImage.
  462. */
  463. IL_INIT_IMAGE_DES (&pFileImage->p.des)
  464. IL_INIT_IMAGE_FORMAT (&pFileImage->p.format)
  465. /* Read the above file tags; fatal error if error returned.
  466. */
  467. pTagAlloc = ilReadFileTags ((ilFileImage)pFileImage, AI_NTAGS, tagNumbers, tag, 0);
  468. if (!pTagAlloc)
  469. return (pFileImage->context->error) ?
  470. pFileImage->context->error : IL_ERROR_FILE_MISSING_TAG;
  471. /* Loop thru the tags; set "present" true if tag found, set "value" to integer
  472. value of tag (long or short allowed - exceptions checked explicitly - else
  473. fatal error).
  474. Fix bug #0169: ignore tags with # items <= 0.
  475. */
  476. for (tagIndex = 0; tagIndex < AI_NTAGS; tagIndex++) {
  477. if ((pTag = tag [tagIndex]) && (pTag->nItems > 0)) {
  478. if (pTag->type == IL_TAG_SHORT)
  479. value = *((unsigned short *)pTag->pItems);
  480. else if (pTag->type == IL_TAG_LONG)
  481. /* compatibility problem with long and unsigned long data fields */
  482. value = *((CARD32 *)pTag->pItems);
  483. else if ((tagIndex != X_RESOLUTION)
  484. && (tagIndex != Y_RESOLUTION)
  485. && (tagIndex != YCBCR_COEFFICIENTS)
  486. && (tagIndex != REFERENCE_BLACK_WHITE))
  487. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  488. if (pTag->nItems != 1) {
  489. if ((tagIndex != BITS_PER_SAMPLE)
  490. && (tagIndex != PAGE_NUMBER)
  491. && (tagIndex != YCBCR_COEFFICIENTS)
  492. && (tagIndex != YCBCR_SUBSAMPLING)
  493. && (tagIndex != REFERENCE_BLACK_WHITE)
  494. && (tagIndex != PRIVATE_0))
  495. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  496. }
  497. present = TRUE;
  498. }
  499. else present = FALSE;
  500. /* Parse the tags and fill in pFileImage->format, des. Init des first.
  501. See page 8 in the TIFF v5.0 spec for info on each tag type.
  502. */
  503. switch (tagIndex) {
  504. case NEW_SUBFILE_TYPE:
  505. if (present && (value & 4)) imageType = maskImage;
  506. else if (present && (value & 1)) imageType = childImage;
  507. else imageType = mainImage;
  508. break;
  509. case IMAGE_WIDTH:
  510. if (!present) AIL_FATAL_ERROR (IL_ERROR_FILE_MISSING_TAG);
  511. pFileImage->p.width = value;
  512. break;
  513. case IMAGE_LENGTH:
  514. if (!present) AIL_FATAL_ERROR (IL_ERROR_FILE_MISSING_TAG);
  515. pFileImage->p.height = value;
  516. break;
  517. /* Resolution returned as dpi * 2, or "0" if not present. The TIFF
  518. spec says should default to 300 dpi if not present, but we let
  519. the caller make that assumption.
  520. */
  521. case RESOLUTION_UNIT:
  522. if (!present)
  523. resolutionUnit = 2; /* default to inches */
  524. else resolutionUnit = value;
  525. break;
  526. case X_RESOLUTION:
  527. case Y_RESOLUTION:
  528. { long denom;
  529. if (!present)
  530. value = 0;
  531. else {
  532. if (pTag->type != IL_TAG_RATIONAL)
  533. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  534. /* compatibility problem with long and unsigned long data fields */
  535. value = *((INT32 *)pTag->pItems);
  536. denom = *((INT32 *)pTag->pItems + 1);
  537. if (resolutionUnit == 3) /* centimeters */
  538. value = (denom == 0) ? 0 : (double)value / 2.54 / (double)denom + 0.5;
  539. else /* not centimeters: assume inches */
  540. value = (denom == 0) ? 0 : (double)value / (double)denom + 0.5;
  541. }
  542. if (tagIndex == X_RESOLUTION)
  543. pFileImage->p.xRes = value;
  544. else pFileImage->p.yRes = value;
  545. }
  546. break;
  547. case ROWS_PER_STRIP:
  548. if (!present || (((long)value) < 0) || (value > pFileImage->p.height))
  549. pFileImage->p.stripHeight = pFileImage->p.height;
  550. else pFileImage->p.stripHeight = value;
  551. break;
  552. case SAMPLES_PER_PIXEL:
  553. if (!present) value = 1;
  554. if (value <= 0)
  555. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  556. if (value > IL_MAX_SAMPLES)
  557. goto UnsupportedImage;
  558. pFileImage->p.des.nSamplesPerPixel = value;
  559. break;
  560. /* Planar configuration: default to pixels unless more than one
  561. sample, and value is present and is 2.
  562. */
  563. case PLANAR_CONFIGURATION:
  564. pFileImage->p.format.sampleOrder = ((pFileImage->p.des.nSamplesPerPixel != 1)
  565. && present && (value == 2)) ? IL_SAMPLE_PLANES : IL_SAMPLE_PIXELS;
  566. break;
  567. /* bits per sample: default to 1, or to first item (in "value") if
  568. fewer items than samples per pixel.
  569. */
  570. case BITS_PER_SAMPLE:
  571. for (i = 0; i < pFileImage->p.des.nSamplesPerPixel; i++)
  572. pFileImage->p.format.nBitsPerSample [i] = (!present || (pTag->nItems < 1)) ?
  573. 1 : ((i > pTag->nItems - 1) ? value : *((unsigned short *)pTag->pItems + i));
  574. break;
  575. /* FillOrder: used for class F only (TIFF compression "3")
  576. */
  577. case FILL_ORDER:
  578. fillOrder = (present && (value == 2)) ? IL_LSB_FIRST : IL_MSB_FIRST;
  579. break;
  580. /* Parse group 3 TIFF options, assumed to be 0 if not present.
  581. As per TIFF spec, don't support if any unrecognized bits are not 0.
  582. Note that "group3CompData" is set to 0 below if comp = 2 (TIFF G3).
  583. */
  584. case GROUP_3_OPTIONS:
  585. group3CompData = IL_G3M_EOL_MARKERS; /* EOL present if not "TIFF" G3 */
  586. if (fillOrder == IL_LSB_FIRST)
  587. group3CompData |= IL_G3M_LSB_FIRST;
  588. if (present) {
  589. if (value & ~0x7)
  590. goto UnsupportedImage; /* non-zero unhandled bits; unsupported */
  591. if (value & 1)
  592. group3CompData |= IL_G3M_2D;
  593. if (value & 2)
  594. group3CompData |= IL_G3M_UNCOMPRESSED;
  595. if (!(value & 4))
  596. group3CompData |= IL_G3M_EOL_UNALIGNED;
  597. }
  598. break;
  599. /* Group 4: set LSB first if FillOrder tag specifies it. If
  600. Group4Options tag defined and any bit other than zero set (bit 0
  601. is undefined) than consider image unsupported. Bit one (1) specifies
  602. "uncompressed" mode which is not supported.
  603. */
  604. case GROUP_4_OPTIONS:
  605. group4CompData = 0;
  606. if (fillOrder == IL_LSB_FIRST)
  607. group4CompData |= IL_G4M_LSB_FIRST;
  608. if (present) {
  609. if (value & ~1)
  610. goto UnsupportedImage; /* non-zero unhandled bits; unsupported */
  611. }
  612. break;
  613. case COMPRESSION:
  614. if (!present) value = 1;
  615. switch (value) {
  616. case 1: pFileImage->p.des.compression = IL_UNCOMPRESSED; break;
  617. /* Comp codes 2 and 3 are both Group 3 - the difference is only in
  618. what *pCompData describes. For code 2 ("TIFF" group 3), the comp
  619. data is 0 - the bits were defined that way. For code 3, use what
  620. was deciphered/defaulted by the group 3 options tag.
  621. */
  622. case 2:
  623. group3CompData = 0; /* "TIFF" G3 - default options, set to 0 */
  624. case 3: /* "true" G3 */
  625. pFileImage->p.des.compression = IL_G3;
  626. pFileImage->p.des.compInfo.g3.flags = group3CompData;
  627. break;
  628. case 4: pFileImage->p.des.compression = IL_G4;
  629. pFileImage->p.des.compInfo.g4.flags = group4CompData;
  630. break;
  631. case 5: pFileImage->p.des.compression = IL_LZW; break;
  632. case 6:
  633. pFileImage->p.des.compression = IL_JPEG;
  634. pFileImage->p.des.compInfo.JPEG.reserved = IL_JPEGM_RAW;
  635. pFileImage->p.des.compInfo.JPEG.process = 0;
  636. break;
  637. case 32773: pFileImage->p.des.compression = IL_PACKBITS; break;
  638. default: goto UnsupportedImage;
  639. }
  640. break;
  641. /* NOTE: predictive LZW not supported yet !!!!
  642. */
  643. case PREDICTOR:
  644. if (present && (value != 1) && (pFileImage->p.des.compression == IL_LZW))
  645. goto UnsupportedImage;
  646. break;
  647. /* If no photometric interpration, default based on (assumed) image
  648. type: 1 sample: 1 bit/pixel: bitonal, white=0 if fax comp, else 1;
  649. > 1 bit/pixel: gray, black=0.
  650. 3 samples: RGB; else error.
  651. */
  652. case PHOTOMETRIC_INTERPRETATION:
  653. pYCbCr = (ilYCbCrInfo *)NULL; /* unless a YCbCr image */
  654. if (!present) {
  655. if (pFileImage->p.des.nSamplesPerPixel == 1) {
  656. if (pFileImage->p.format.nBitsPerSample[0] == 1) {
  657. pFileImage->p.des.type = IL_BITONAL;
  658. switch (pFileImage->p.des.compression) {
  659. case IL_G3: case IL_G4:
  660. pFileImage->p.des.blackIsZero = FALSE;
  661. break;
  662. default:
  663. pFileImage->p.des.blackIsZero = TRUE;
  664. break;
  665. }
  666. }
  667. else {
  668. pFileImage->p.des.type = IL_GRAY;
  669. pFileImage->p.des.blackIsZero = TRUE;
  670. }
  671. }
  672. else if (pFileImage->p.des.nSamplesPerPixel == 3)
  673. pFileImage->p.des.type = IL_RGB;
  674. else AIL_FATAL_ERROR (IL_ERROR_FILE_MISSING_TAG);
  675. }
  676. else switch (value) { /* photo interpretation present; handle it */
  677. /* Bitonal or gray; 0 = white is 0, else black is zero. */
  678. case 0:
  679. case 1:
  680. if (pFileImage->p.des.nSamplesPerPixel != 1)
  681. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  682. pFileImage->p.des.blackIsZero = (value == 1);
  683. pFileImage->p.des.type = (pFileImage->p.format.nBitsPerSample[0] == 1) ?
  684. IL_BITONAL : IL_GRAY;
  685. break;
  686. /* RGB. */
  687. case 2:
  688. if (pFileImage->p.des.nSamplesPerPixel != 3)
  689. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  690. pFileImage->p.des.type = IL_RGB;
  691. break;
  692. /* Palette images: max of 8 bits */
  693. case 3:
  694. if (pFileImage->p.des.nSamplesPerPixel != 1)
  695. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  696. if (pFileImage->p.format.nBitsPerSample[0] > 8)
  697. goto UnsupportedImage;
  698. pFileImage->p.des.type = IL_PALETTE;
  699. break;
  700. /* Mask image: is bitonal */
  701. case 4:
  702. if (pFileImage->p.des.nSamplesPerPixel != 1)
  703. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  704. pFileImage->p.des.type = IL_BITONAL;
  705. pFileImage->p.des.blackIsZero = FALSE;
  706. imageType = maskImage;
  707. break;
  708. /* YCbCr: set pYCbCr to non-null */
  709. case 6:
  710. if (pFileImage->p.des.nSamplesPerPixel != 3)
  711. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE);
  712. pFileImage->p.des.type = IL_YCBCR;
  713. pYCbCr = &pFileImage->p.des.typeInfo.YCbCr; /* point to YCbCr data */
  714. break;
  715. /* Others not supported */
  716. default: goto UnsupportedImage;
  717. }
  718. break;
  719. case PAGE_NUMBER:
  720. if (present) {
  721. if ((pTag->nItems != 2) || (pTag->type != IL_TAG_SHORT))
  722. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  723. pFileImage->p.page = value;
  724. pFileImage->p.nPages = *((unsigned short *)pTag->pItems + 1);
  725. }
  726. else pFileImage->p.page = pFileImage->p.nPages = -1;
  727. break;
  728. /* JPEG tags: ignore if not JPEG compression */
  729. case JPEG_PROC:
  730. if (pFileImage->p.des.compression == IL_JPEG) {
  731. if (!present) AIL_FATAL_ERROR (IL_ERROR_FILE_MISSING_TAG);
  732. if (value != 1) /* only baseline sequential supported! */
  733. goto UnsupportedImage;
  734. }
  735. break;
  736. /* YCbCr tags: ignored if not YCbCr image */
  737. case YCBCR_COEFFICIENTS:
  738. if (pYCbCr) {
  739. if (!present) {
  740. pYCbCr->lumaRed = 2990;
  741. pYCbCr->lumaGreen = 5870;
  742. pYCbCr->lumaBlue = 1140;
  743. }
  744. else {
  745. /* compatibility problem with long or unsigned long data fields */
  746. INT32 *pLong = (INT32 *)pTag->pItems;
  747. double num, denom;
  748. if ((pTag->nItems != 3) || (pTag->type != IL_TAG_RATIONAL))
  749. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  750. num = *pLong++; denom = *pLong++;
  751. pYCbCr->lumaRed = num/denom * 10000;
  752. num = *pLong++; denom = *pLong++;
  753. pYCbCr->lumaGreen = num/denom * 10000;
  754. num = *pLong++; denom = *pLong++;
  755. pYCbCr->lumaBlue = num/denom * 10000;
  756. }
  757. }
  758. break;
  759. case YCBCR_SUBSAMPLING:
  760. if (pYCbCr) {
  761. if (!present)
  762. pYCbCr->sample[1].subsampleHoriz = pYCbCr->sample[1].subsampleVert = 2;
  763. else {
  764. if ((pTag->nItems != 2) || (pTag->type != IL_TAG_SHORT))
  765. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  766. pYCbCr->sample[1].subsampleHoriz = *((short *)pTag->pItems);
  767. pYCbCr->sample[1].subsampleVert = *((short *)pTag->pItems + 1);
  768. }
  769. /* Set Y subsampling to (1,1); copy Cr to Cb: defined by TIFF */
  770. pYCbCr->sample[0].subsampleHoriz = pYCbCr->sample[0].subsampleVert = 1;
  771. pYCbCr->sample[2].subsampleHoriz = pYCbCr->sample[1].subsampleHoriz;
  772. pYCbCr->sample[2].subsampleVert = pYCbCr->sample[1].subsampleVert;
  773. }
  774. break;
  775. /* Ignore positioning for now; set to zero (0) */
  776. case YCBCR_POSITIONING:
  777. if (pYCbCr)
  778. pYCbCr->positioning = 0;
  779. break;
  780. /* ReferenceBlackWhite: if present, allow tag type to be "long" or "rational".
  781. "rational" is the correct tag type; "long" was written by Image 1 (the
  782. first Developer's Kit that supported JPEG), so support the old files.
  783. */
  784. case REFERENCE_BLACK_WHITE:
  785. if (pYCbCr) {
  786. ilYCbCrSampleInfo *pSample = pYCbCr->sample;
  787. int i;
  788. if (!present) {
  789. pSample->refBlack = 0; pSample->refWhite = 255; pSample++;
  790. pSample->refBlack = 0; pSample->refWhite = 255; pSample++;
  791. pSample->refBlack = 0; pSample->refWhite = 255; pSample++;
  792. }
  793. else {
  794. /* compatibility problem with long and unsigned long data fields */
  795. INT32 *pLong = (INT32 *)pTag->pItems;
  796. if (pTag->nItems != 6)
  797. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  798. if (pTag->type == IL_TAG_LONG) { /* old type */
  799. for (i = 0; i < 3; i++, pSample++) {
  800. pSample->refBlack = *pLong++;
  801. pSample->refWhite = *pLong++;
  802. }
  803. }
  804. else if (pTag->type == IL_TAG_RATIONAL) { /* correct TIFF 6.0 type */
  805. long num, denom;
  806. for (i = 0; i < 3; i++, pSample++) {
  807. num = *pLong++;
  808. denom = *pLong++;
  809. pSample->refBlack = (denom == 1) ? num :
  810. (double)num / (double)denom + 0.5;
  811. num = *pLong++;
  812. denom = *pLong++;
  813. pSample->refWhite = (denom == 1) ? num :
  814. (double)num / (double)denom + 0.5;
  815. }
  816. }
  817. else AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG); /* bad tag type */
  818. }
  819. }
  820. break;
  821. /* HP IL private tag. See /ilc/iltiffint.h for definition.
  822. If "dithered palette" mask on, set levels and bit in flags in des.
  823. */
  824. case PRIVATE_0:
  825. if (present) {
  826. if (value & IL_TAG_P0_FLAG_DITHER_LEVELS) {
  827. if (pFileImage->p.des.type == IL_PALETTE) {
  828. short *pLevel = ((short *)pTag->pItems + 1);
  829. if ((pTag->nItems < 4) || (pTag->type != IL_TAG_SHORT))
  830. AIL_FATAL_ERROR (IL_ERROR_FILE_MALFORMED_TAG);
  831. pFileImage->p.des.flags |= IL_DITHERED_PALETTE;
  832. pFileImage->p.des.typeInfo.palette.levels[0] = *pLevel++;
  833. pFileImage->p.des.typeInfo.palette.levels[1] = *pLevel++;
  834. pFileImage->p.des.typeInfo.palette.levels[2] = *pLevel++;
  835. }
  836. }
  837. }
  838. break;
  839. } /* END switch tag index */
  840. } /* END each tag */
  841. /* All the tags are parsed and valid so far. Finish filling in *pFileImage;
  842. # levels is inferred from # bits; only supporting byte/sample for now.
  843. */
  844. for (i = 0; i < pFileImage->p.des.nSamplesPerPixel; i++) {
  845. nBits = pFileImage->p.format.nBitsPerSample [i];
  846. if (nBits <= 0)
  847. AIL_FATAL_ERROR (IL_ERROR_FILE_TAG_VALUE); /* error; invalid # bits */
  848. if (nBits > 8)
  849. goto UnsupportedImage; /* not error, not supported */
  850. pFileImage->p.des.nLevelsPerSample [i] = 1 << nBits;
  851. }
  852. pFileImage->p.format.byteOrder = IL_MSB_FIRST; /* arbitrary! */
  853. pFileImage->p.format.rowBitAlign = 8; /* by TIFF definition */
  854. /* If mask image: must be bitonal or discard it. */
  855. if (imageType == maskImage) {
  856. ilFileImagePtr pPrevFileImage = *ppPrevFileImage;
  857. if (!pPrevFileImage /* no image before it */
  858. || pPrevFileImage->p.pMask /* already has mask */
  859. || (pFileImage->p.des.nSamplesPerPixel != 1)
  860. || (pFileImage->p.format.nBitsPerSample[0] != 1))
  861. goto UnsupportedImage; /* reject image */
  862. }
  863. /* If a child image and no previous image, make a main image and add it. */
  864. if ((imageType == childImage) && !*ppPrevFileImage)
  865. imageType = mainImage;
  866. /* Add image to list, make previous image unless a mask image */
  867. pFileImage->imageType = imageType; /* child/mask/mainImage */
  868. ilAddImageToList (pFile, pFileImage, *ppPrevFileImage);
  869. if (imageType != maskImage)
  870. *ppPrevFileImage = pFileImage;
  871. free (pTagAlloc); /* free() as client would */
  872. return IL_OK; /* success; EXIT */
  873. /* Error: free pTagAlloc, but not pFileImage - caller does that. Return error.
  874. Use free(), not IL_FREE(), to free pTagAlloc, just like caller should.
  875. */
  876. AILFatalError:
  877. if (pTagAlloc)
  878. free (pTagAlloc);
  879. return error;
  880. /* Image is not supported; not an error, but don't add to list - free image.
  881. */
  882. UnsupportedImage:
  883. IL_FREE (pFileImage);
  884. return IL_OK;
  885. }
  886. /* ------------------------ ilListFileImages ------------------------------- */
  887. /* Public function: see spec.
  888. */
  889. ilFileImage ilListFileImages (
  890. ilFile file,
  891. unsigned long mustBeZero
  892. )
  893. {
  894. ilError error;
  895. long imageSequenceNumber;
  896. ilByte fourBytes [4];
  897. ilFileOffset IFDOffset;
  898. ilFileImagePtr pFileImage, pPrevFileImage;
  899. ilTIFFHeader header;
  900. ilFilePtr pFile;
  901. ilBool bigEndian;
  902. /* Check if image list already built - if so, return ptr to it.
  903. */
  904. pFile = (ilFilePtr)file;
  905. if (mustBeZero != 0) {
  906. pFile->o.p.context->error = IL_ERROR_PAR_NOT_ZERO;
  907. return (ilFileImage)NULL; /* EXIT */
  908. }
  909. if (pFile->haveImageList) {
  910. pFile->o.p.context->error = IL_OK;
  911. return (ilFileImage)pFile->pFileImageHead; /* EXIT */
  912. }
  913. ilFreeFileList (pFile); /* remove potential invalid image list */
  914. /* Don't have image list - read file and build the list. When done,
  915. pFile->IFDTailPtrOffset points to the "next IFD" ptr which is null, i.e.
  916. the next IFD ptr for the last image in the file. For a file with no images
  917. that would be the ptr in the file header.
  918. First read header to get ptr to first image in file.
  919. */
  920. #ifdef DTLIB
  921. /* Used when this module is compiled by the DtHelp library build */
  922. if (_DtGrSeek (pFile->stream, pFile->offset, 0)) {
  923. #else
  924. if (fseek (pFile->stream, pFile->offset, 0)) {
  925. #endif /* DTLIB */
  926. pFile->o.p.context->error = IL_ERROR_FILE_IO;
  927. return (ilFileImage)NULL;
  928. }
  929. #ifdef DTLIB
  930. /* Used when this module is compiled by the DtHelp library build */
  931. if (!_DtGrRead ((char *)&header, sizeof (header), 1, pFile->stream)) {
  932. #else
  933. if (!fread ((char *)&header, sizeof (header), 1, pFile->stream)) {
  934. #endif /* DTLIB */
  935. pFile->o.p.context->error = IL_ERROR_FILE_IO;
  936. return (ilFileImage)NULL;
  937. }
  938. IL_FLIP_LONG (pFile->bigEndian, header.IFDHeadOffset, IFDOffset);
  939. pFile->haveImageList = TRUE;
  940. pFile->IFDTailPtrOffset = 4; /* point to first IFD ptr in header */
  941. imageSequenceNumber = 0;
  942. bigEndian = pFile->bigEndian;
  943. pPrevFileImage = (ilFileImagePtr)NULL;
  944. /* Overview: read the tag count, allocate room for tags (pBeginTagBuffer) and
  945. read the unflipped tags into it. Allocate space for converted tags
  946. (pFile->pTags) and copy/flip unflipped tags into it.
  947. After the tags comes the offset to next IFD; read and flip it.
  948. */
  949. while (IFDOffset) {
  950. imageSequenceNumber++;
  951. pFileImage = (ilFileImagePtr)IL_MALLOC_ZERO (sizeof (ilFileImageRec));
  952. if (!pFileImage) {
  953. error = IL_ERROR_MALLOC;
  954. goto LFIFatalError;
  955. }
  956. pFileImage->p.file = (ilFile)pFile;
  957. pFileImage->context = pFile->o.p.context;
  958. /* Read and flip the tagCount, @IFDOffset. Point tagOffset at next location.
  959. */
  960. if (!IL_SEEK (pFile, IFDOffset)
  961. || !IL_READ (pFile, 2, fourBytes)) {
  962. error = IL_ERROR_FILE_IO;
  963. goto LFIFatalError;
  964. }
  965. IL_FLIP_SHORT (bigEndian, fourBytes, pFileImage->tagCount);
  966. pFileImage->tagOffset = IFDOffset + 2;
  967. /* Add file image to list, if supported. An error is returned only if
  968. the file image is malformed; not if it is just unsupported.
  969. */
  970. if ((error = ilReadTagsAndAddImageToList (pFile, &pPrevFileImage, pFileImage)))
  971. goto LFIFatalError; /* fatal error returned; EXIT */
  972. /* Read and flip offset to next IFD, at end of tags; save in pFile. */
  973. IFDOffset += pFileImage->tagCount * IL_TAG_SIZE + 2;
  974. pFile->IFDTailPtrOffset = IFDOffset;
  975. if (!IL_SEEK (pFile, IFDOffset)
  976. || !IL_READ (pFile, 4, fourBytes)) {
  977. error = IL_ERROR_FILE_IO;
  978. goto LFIFatalError;
  979. }
  980. IL_FLIP_LONG (bigEndian, fourBytes, IFDOffset);
  981. } /* END one IFD */
  982. /* Images accessed successfully; return ok; */
  983. pFile->o.p.context->error = IL_OK;
  984. return (ilFileImage)pFile->pFileImageHead; /* EXIT */
  985. /* Fatal error: dispose file list and pFileImage if non-null, return "error",
  986. and sequence # of image in file that provoked the error.
  987. */
  988. LFIFatalError:
  989. if (pFileImage)
  990. IL_FREE (pFileImage);
  991. ilFreeFileList (pFile);
  992. pFile->o.p.context->error = error;
  993. pFile->o.p.context->errorInfo = imageSequenceNumber;
  994. return (ilFileImage)NULL;
  995. }
  996. /* ------------------------ ilConnectFile -------------------------------- */
  997. /* Public function: see spec.
  998. Locate a TIFF file at "offset" within "file", and return a handle used
  999. to access this file, or return null and an error code.
  1000. */
  1001. ilFile ilConnectFile (
  1002. ilContext context,
  1003. #ifdef DTLIB
  1004. /* Used when this module is compiled by the DtHelp library build */
  1005. _DtGrStream *stream,
  1006. #else
  1007. FILE *stream,
  1008. #endif /* DTLIB */
  1009. long offset,
  1010. unsigned long mustBeZero
  1011. )
  1012. {
  1013. ilBool bigEndian;
  1014. ilFilePtr pFile;
  1015. ilTIFFHeader header;
  1016. if (mustBeZero != 0) {
  1017. context->error = IL_ERROR_PAR_NOT_ZERO;
  1018. return (ilPipe)NULL;
  1019. }
  1020. /* Read what should be a TIFF file header at "offset" within "file".
  1021. Verify that byte order is "MM" (bigEndian) or "II" (!bigEndian) or error.
  1022. Verify the answer to life, the universe, and everything ...
  1023. */
  1024. #ifdef DTLIB
  1025. /* Used when this module is compiled by the DtHelp library build */
  1026. if (_DtGrSeek (stream, offset, 0)) { /* nonzero means error for fseek */
  1027. #else
  1028. if (fseek (stream, offset, 0)) { /* nonzero means error for fseek */
  1029. #endif /* DTLIB */
  1030. context->error = IL_ERROR_FILE_IO;
  1031. return (ilFile)NULL;
  1032. }
  1033. #ifdef DTLIB
  1034. /* Used when this module is compiled by the DtHelp library build */
  1035. if (!_DtGrRead ((char *)&header, sizeof (header), 1, stream)) {
  1036. #else
  1037. if (!fread ((char *)&header, sizeof (header), 1, stream)) {
  1038. #endif /* DTLIB */
  1039. context->error = IL_ERROR_FILE_IO;
  1040. return (ilFile)NULL;
  1041. }
  1042. if ((header.byteOrder[0] == 'M') && (header.byteOrder[1] == 'M'))
  1043. bigEndian = TRUE;
  1044. else if ((header.byteOrder[0] == 'I') && (header.byteOrder[1] == 'I'))
  1045. bigEndian = FALSE;
  1046. else {
  1047. context->error = IL_ERROR_FILE_NOT_TIFF;
  1048. return (ilFile)NULL;
  1049. }
  1050. if (!(bigEndian && (header.version[0] == 0) && (header.version[1] == 42))
  1051. && !(!bigEndian && (header.version[0] == 42) && (header.version[1] == 0))) {
  1052. context->error = IL_ERROR_FILE_NOT_TIFF;
  1053. return (ilFile)NULL;
  1054. }
  1055. /* File seems to be a valid TIFF file. Create an ilFileRec object, init it
  1056. and return ptr to it.
  1057. */
  1058. pFile = (ilFilePtr)_ilCreateObject (context, IL_FILE, ilFreeFileList, sizeof(ilFileRec));
  1059. if (!pFile)
  1060. return (ilFile)NULL; /* context->error already set */
  1061. pFile->stream = stream;
  1062. pFile->offset = offset;
  1063. pFile->bigEndian = bigEndian;
  1064. pFile->pFileImageHead = pFile->pFileImageTail = (ilFileImagePtr)NULL;
  1065. pFile->haveImageList = FALSE;
  1066. context->error = IL_OK;
  1067. return (ilFile)pFile;
  1068. }