Unpack.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * code to unpack raw data from various file formats
  6. *
  7. * history:
  8. * 1996-03-07 fl Created (from various decoders)
  9. * 1996-04-19 fl Added band unpackers
  10. * 1996-05-12 fl Published RGB unpackers
  11. * 1996-05-27 fl Added nibble unpacker
  12. * 1996-12-10 fl Added complete set of PNG unpackers
  13. * 1996-12-29 fl Set alpha byte in RGB unpackers
  14. * 1997-01-05 fl Added remaining TGA unpackers
  15. * 1997-01-18 fl Added inverting band unpackers
  16. * 1997-01-25 fl Added FlashPix unpackers
  17. * 1997-05-31 fl Added floating point unpackers
  18. * 1998-02-08 fl Added I unpacker
  19. * 1998-07-01 fl Added YCbCr unpacker
  20. * 1998-07-02 fl Added full set of integer unpackers
  21. * 1998-12-29 fl Added mode field, I;16 unpackers
  22. * 1998-12-30 fl Added RGBX modes
  23. * 1999-02-04 fl Fixed I;16 unpackers
  24. * 2003-05-13 fl Added L/RGB reversed unpackers
  25. * 2003-09-26 fl Added LA/PA and RGBa->RGB unpackers
  26. *
  27. * Copyright (c) 1997-2003 by Secret Labs AB.
  28. * Copyright (c) 1996-1997 by Fredrik Lundh.
  29. *
  30. * See the README file for information on usage and redistribution.
  31. */
  32. #include "Imaging.h"
  33. #include "Convert.h"
  34. #define R 0
  35. #define G 1
  36. #define B 2
  37. #define X 3
  38. #define A 3
  39. #define C 0
  40. #define M 1
  41. #define Y 2
  42. #define K 3
  43. /* byte-swapping macros */
  44. #define C16N (tmp[0] = in[0], tmp[1] = in[1]);
  45. #define C16S (tmp[1] = in[0], tmp[0] = in[1]);
  46. #define C32N (tmp[0] = in[0], tmp[1] = in[1], tmp[2] = in[2], tmp[3] = in[3]);
  47. #define C32S (tmp[3] = in[0], tmp[2] = in[1], tmp[1] = in[2], tmp[0] = in[3]);
  48. #define C64N \
  49. (tmp[0] = in[0], \
  50. tmp[1] = in[1], \
  51. tmp[2] = in[2], \
  52. tmp[3] = in[3], \
  53. tmp[4] = in[4], \
  54. tmp[5] = in[5], \
  55. tmp[6] = in[6], \
  56. tmp[7] = in[7]);
  57. #define C64S \
  58. (tmp[7] = in[0], \
  59. tmp[6] = in[1], \
  60. tmp[5] = in[2], \
  61. tmp[4] = in[3], \
  62. tmp[3] = in[4], \
  63. tmp[2] = in[5], \
  64. tmp[1] = in[6], \
  65. tmp[0] = in[7]);
  66. #ifdef WORDS_BIGENDIAN
  67. #define C16B C16N
  68. #define C16L C16S
  69. #define C32B C32N
  70. #define C32L C32S
  71. #define C64B C64N
  72. #define C64L C64S
  73. #else
  74. #define C16B C16S
  75. #define C16L C16N
  76. #define C32B C32S
  77. #define C32L C32N
  78. #define C64B C64S
  79. #define C64L C64N
  80. #endif
  81. /* bit-swapping */
  82. static UINT8 BITFLIP[] = {
  83. 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  84. 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  85. 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  86. 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  87. 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  88. 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  89. 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  90. 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  91. 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  92. 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  93. 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  94. 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  95. 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  96. 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  97. 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  98. 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255};
  99. /* Unpack to "1" image */
  100. static void
  101. unpack1(UINT8 *out, const UINT8 *in, int pixels) {
  102. /* bits (msb first, white is non-zero) */
  103. while (pixels > 0) {
  104. UINT8 byte = *in++;
  105. switch (pixels) {
  106. default:
  107. *out++ = (byte & 128) ? 255 : 0;
  108. byte <<= 1;
  109. case 7:
  110. *out++ = (byte & 128) ? 255 : 0;
  111. byte <<= 1;
  112. case 6:
  113. *out++ = (byte & 128) ? 255 : 0;
  114. byte <<= 1;
  115. case 5:
  116. *out++ = (byte & 128) ? 255 : 0;
  117. byte <<= 1;
  118. case 4:
  119. *out++ = (byte & 128) ? 255 : 0;
  120. byte <<= 1;
  121. case 3:
  122. *out++ = (byte & 128) ? 255 : 0;
  123. byte <<= 1;
  124. case 2:
  125. *out++ = (byte & 128) ? 255 : 0;
  126. byte <<= 1;
  127. case 1:
  128. *out++ = (byte & 128) ? 255 : 0;
  129. }
  130. pixels -= 8;
  131. }
  132. }
  133. static void
  134. unpack1I(UINT8 *out, const UINT8 *in, int pixels) {
  135. /* bits (msb first, white is zero) */
  136. while (pixels > 0) {
  137. UINT8 byte = *in++;
  138. switch (pixels) {
  139. default:
  140. *out++ = (byte & 128) ? 0 : 255;
  141. byte <<= 1;
  142. case 7:
  143. *out++ = (byte & 128) ? 0 : 255;
  144. byte <<= 1;
  145. case 6:
  146. *out++ = (byte & 128) ? 0 : 255;
  147. byte <<= 1;
  148. case 5:
  149. *out++ = (byte & 128) ? 0 : 255;
  150. byte <<= 1;
  151. case 4:
  152. *out++ = (byte & 128) ? 0 : 255;
  153. byte <<= 1;
  154. case 3:
  155. *out++ = (byte & 128) ? 0 : 255;
  156. byte <<= 1;
  157. case 2:
  158. *out++ = (byte & 128) ? 0 : 255;
  159. byte <<= 1;
  160. case 1:
  161. *out++ = (byte & 128) ? 0 : 255;
  162. }
  163. pixels -= 8;
  164. }
  165. }
  166. static void
  167. unpack1R(UINT8 *out, const UINT8 *in, int pixels) {
  168. /* bits (lsb first, white is non-zero) */
  169. while (pixels > 0) {
  170. UINT8 byte = *in++;
  171. switch (pixels) {
  172. default:
  173. *out++ = (byte & 1) ? 255 : 0;
  174. byte >>= 1;
  175. case 7:
  176. *out++ = (byte & 1) ? 255 : 0;
  177. byte >>= 1;
  178. case 6:
  179. *out++ = (byte & 1) ? 255 : 0;
  180. byte >>= 1;
  181. case 5:
  182. *out++ = (byte & 1) ? 255 : 0;
  183. byte >>= 1;
  184. case 4:
  185. *out++ = (byte & 1) ? 255 : 0;
  186. byte >>= 1;
  187. case 3:
  188. *out++ = (byte & 1) ? 255 : 0;
  189. byte >>= 1;
  190. case 2:
  191. *out++ = (byte & 1) ? 255 : 0;
  192. byte >>= 1;
  193. case 1:
  194. *out++ = (byte & 1) ? 255 : 0;
  195. }
  196. pixels -= 8;
  197. }
  198. }
  199. static void
  200. unpack1IR(UINT8 *out, const UINT8 *in, int pixels) {
  201. /* bits (lsb first, white is zero) */
  202. while (pixels > 0) {
  203. UINT8 byte = *in++;
  204. switch (pixels) {
  205. default:
  206. *out++ = (byte & 1) ? 0 : 255;
  207. byte >>= 1;
  208. case 7:
  209. *out++ = (byte & 1) ? 0 : 255;
  210. byte >>= 1;
  211. case 6:
  212. *out++ = (byte & 1) ? 0 : 255;
  213. byte >>= 1;
  214. case 5:
  215. *out++ = (byte & 1) ? 0 : 255;
  216. byte >>= 1;
  217. case 4:
  218. *out++ = (byte & 1) ? 0 : 255;
  219. byte >>= 1;
  220. case 3:
  221. *out++ = (byte & 1) ? 0 : 255;
  222. byte >>= 1;
  223. case 2:
  224. *out++ = (byte & 1) ? 0 : 255;
  225. byte >>= 1;
  226. case 1:
  227. *out++ = (byte & 1) ? 0 : 255;
  228. }
  229. pixels -= 8;
  230. }
  231. }
  232. static void
  233. unpack18(UINT8 *out, const UINT8 *in, int pixels) {
  234. /* Unpack a '|b1' image, which is a numpy boolean.
  235. 1 == true, 0==false, in bytes */
  236. int i;
  237. for (i = 0; i < pixels; i++) {
  238. out[i] = in[i] > 0 ? 255 : 0;
  239. }
  240. }
  241. /* Unpack to "L" image */
  242. static void
  243. unpackL2(UINT8 *out, const UINT8 *in, int pixels) {
  244. /* nibbles (msb first, white is non-zero) */
  245. while (pixels > 0) {
  246. UINT8 byte = *in++;
  247. switch (pixels) {
  248. default:
  249. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  250. byte <<= 2;
  251. case 3:
  252. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  253. byte <<= 2;
  254. case 2:
  255. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  256. byte <<= 2;
  257. case 1:
  258. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  259. }
  260. pixels -= 4;
  261. }
  262. }
  263. static void
  264. unpackL2I(UINT8 *out, const UINT8 *in, int pixels) {
  265. /* nibbles (msb first, white is zero) */
  266. while (pixels > 0) {
  267. UINT8 byte = *in++;
  268. switch (pixels) {
  269. default:
  270. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  271. byte <<= 2;
  272. case 3:
  273. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  274. byte <<= 2;
  275. case 2:
  276. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  277. byte <<= 2;
  278. case 1:
  279. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  280. }
  281. pixels -= 4;
  282. }
  283. }
  284. static void
  285. unpackL2R(UINT8 *out, const UINT8 *in, int pixels) {
  286. /* nibbles (bit order reversed, white is non-zero) */
  287. while (pixels > 0) {
  288. UINT8 byte = *in++;
  289. byte = BITFLIP[byte];
  290. switch (pixels) {
  291. default:
  292. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  293. byte <<= 2;
  294. case 3:
  295. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  296. byte <<= 2;
  297. case 2:
  298. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  299. byte <<= 2;
  300. case 1:
  301. *out++ = ((byte >> 6) & 0x03U) * 0x55U;
  302. }
  303. pixels -= 4;
  304. }
  305. }
  306. static void
  307. unpackL2IR(UINT8 *out, const UINT8 *in, int pixels) {
  308. /* nibbles (bit order reversed, white is zero) */
  309. while (pixels > 0) {
  310. UINT8 byte = *in++;
  311. byte = BITFLIP[byte];
  312. switch (pixels) {
  313. default:
  314. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  315. byte <<= 2;
  316. case 3:
  317. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  318. byte <<= 2;
  319. case 2:
  320. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  321. byte <<= 2;
  322. case 1:
  323. *out++ = 0xFFU - (UINT8)(((byte >> 6) & 0x03U) * 0x55U);
  324. }
  325. pixels -= 4;
  326. }
  327. }
  328. static void
  329. unpackL4(UINT8 *out, const UINT8 *in, int pixels) {
  330. /* nibbles (msb first, white is non-zero) */
  331. while (pixels > 0) {
  332. UINT8 byte = *in++;
  333. switch (pixels) {
  334. default:
  335. *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
  336. byte <<= 4;
  337. case 1:
  338. *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
  339. }
  340. pixels -= 2;
  341. }
  342. }
  343. static void
  344. unpackL4I(UINT8 *out, const UINT8 *in, int pixels) {
  345. /* nibbles (msb first, white is zero) */
  346. while (pixels > 0) {
  347. UINT8 byte = *in++;
  348. switch (pixels) {
  349. default:
  350. *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
  351. byte <<= 4;
  352. case 1:
  353. *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
  354. }
  355. pixels -= 2;
  356. }
  357. }
  358. static void
  359. unpackL4R(UINT8 *out, const UINT8 *in, int pixels) {
  360. /* nibbles (bit order reversed, white is non-zero) */
  361. while (pixels > 0) {
  362. UINT8 byte = *in++;
  363. byte = BITFLIP[byte];
  364. switch (pixels) {
  365. default:
  366. *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
  367. byte <<= 4;
  368. case 1:
  369. *out++ = ((byte >> 4) & 0x0FU) * 0x11U;
  370. }
  371. pixels -= 2;
  372. }
  373. }
  374. static void
  375. unpackL4IR(UINT8 *out, const UINT8 *in, int pixels) {
  376. /* nibbles (bit order reversed, white is zero) */
  377. while (pixels > 0) {
  378. UINT8 byte = *in++;
  379. byte = BITFLIP[byte];
  380. switch (pixels) {
  381. default:
  382. *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
  383. byte <<= 4;
  384. case 1:
  385. *out++ = 0xFFU - (UINT8)(((byte >> 4) & 0x0FU) * 0x11U);
  386. }
  387. pixels -= 2;
  388. }
  389. }
  390. static void
  391. unpackLA(UINT8 *_out, const UINT8 *in, int pixels) {
  392. int i;
  393. /* LA, pixel interleaved */
  394. for (i = 0; i < pixels; i++) {
  395. UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[1]);
  396. memcpy(_out, &iv, sizeof(iv));
  397. in += 2;
  398. _out += 4;
  399. }
  400. }
  401. static void
  402. unpackLAL(UINT8 *_out, const UINT8 *in, int pixels) {
  403. int i;
  404. /* LA, line interleaved */
  405. for (i = 0; i < pixels; i++, _out += 4) {
  406. UINT32 iv = MAKE_UINT32(in[i], in[i], in[i], in[i + pixels]);
  407. memcpy(_out, &iv, sizeof(iv));
  408. }
  409. }
  410. static void
  411. unpackLI(UINT8 *out, const UINT8 *in, int pixels) {
  412. /* negative */
  413. int i;
  414. for (i = 0; i < pixels; i++) {
  415. out[i] = ~in[i];
  416. }
  417. }
  418. static void
  419. unpackLR(UINT8 *out, const UINT8 *in, int pixels) {
  420. int i;
  421. /* RGB, bit reversed */
  422. for (i = 0; i < pixels; i++) {
  423. out[i] = BITFLIP[in[i]];
  424. }
  425. }
  426. static void
  427. unpackL16(UINT8 *out, const UINT8 *in, int pixels) {
  428. /* int16 (upper byte, little endian) */
  429. int i;
  430. for (i = 0; i < pixels; i++) {
  431. out[i] = in[1];
  432. in += 2;
  433. }
  434. }
  435. static void
  436. unpackL16B(UINT8 *out, const UINT8 *in, int pixels) {
  437. int i;
  438. /* int16 (upper byte, big endian) */
  439. for (i = 0; i < pixels; i++) {
  440. out[i] = in[0];
  441. in += 2;
  442. }
  443. }
  444. /* Unpack to "P" image */
  445. static void
  446. unpackP1(UINT8 *out, const UINT8 *in, int pixels) {
  447. /* bits */
  448. while (pixels > 0) {
  449. UINT8 byte = *in++;
  450. switch (pixels) {
  451. default:
  452. *out++ = (byte >> 7) & 1;
  453. byte <<= 1;
  454. case 7:
  455. *out++ = (byte >> 7) & 1;
  456. byte <<= 1;
  457. case 6:
  458. *out++ = (byte >> 7) & 1;
  459. byte <<= 1;
  460. case 5:
  461. *out++ = (byte >> 7) & 1;
  462. byte <<= 1;
  463. case 4:
  464. *out++ = (byte >> 7) & 1;
  465. byte <<= 1;
  466. case 3:
  467. *out++ = (byte >> 7) & 1;
  468. byte <<= 1;
  469. case 2:
  470. *out++ = (byte >> 7) & 1;
  471. byte <<= 1;
  472. case 1:
  473. *out++ = (byte >> 7) & 1;
  474. }
  475. pixels -= 8;
  476. }
  477. }
  478. static void
  479. unpackP2(UINT8 *out, const UINT8 *in, int pixels) {
  480. /* bit pairs */
  481. while (pixels > 0) {
  482. UINT8 byte = *in++;
  483. switch (pixels) {
  484. default:
  485. *out++ = (byte >> 6) & 3;
  486. byte <<= 2;
  487. case 3:
  488. *out++ = (byte >> 6) & 3;
  489. byte <<= 2;
  490. case 2:
  491. *out++ = (byte >> 6) & 3;
  492. byte <<= 2;
  493. case 1:
  494. *out++ = (byte >> 6) & 3;
  495. }
  496. pixels -= 4;
  497. }
  498. }
  499. static void
  500. unpackP4(UINT8 *out, const UINT8 *in, int pixels) {
  501. /* nibbles */
  502. while (pixels > 0) {
  503. UINT8 byte = *in++;
  504. switch (pixels) {
  505. default:
  506. *out++ = (byte >> 4) & 15;
  507. byte <<= 4;
  508. case 1:
  509. *out++ = (byte >> 4) & 15;
  510. }
  511. pixels -= 2;
  512. }
  513. }
  514. static void
  515. unpackP2L(UINT8 *out, const UINT8 *in, int pixels) {
  516. int i, j, m, s;
  517. /* bit layers */
  518. m = 128;
  519. s = (pixels + 7) / 8;
  520. for (i = j = 0; i < pixels; i++) {
  521. out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0);
  522. if ((m >>= 1) == 0) {
  523. m = 128;
  524. j++;
  525. }
  526. }
  527. }
  528. static void
  529. unpackP4L(UINT8 *out, const UINT8 *in, int pixels) {
  530. int i, j, m, s;
  531. /* bit layers (trust the optimizer ;-) */
  532. m = 128;
  533. s = (pixels + 7) / 8;
  534. for (i = j = 0; i < pixels; i++) {
  535. out[i] = ((in[j] & m) ? 1 : 0) + ((in[j + s] & m) ? 2 : 0) +
  536. ((in[j + 2 * s] & m) ? 4 : 0) + ((in[j + 3 * s] & m) ? 8 : 0);
  537. if ((m >>= 1) == 0) {
  538. m = 128;
  539. j++;
  540. }
  541. }
  542. }
  543. /* Unpack to "RGB" image */
  544. void
  545. ImagingUnpackRGB(UINT8 *_out, const UINT8 *in, int pixels) {
  546. int i = 0;
  547. /* RGB triplets */
  548. for (; i < pixels - 1; i++) {
  549. UINT32 iv;
  550. memcpy(&iv, in, sizeof(iv));
  551. iv |= MASK_UINT32_CHANNEL_3;
  552. memcpy(_out, &iv, sizeof(iv));
  553. in += 3;
  554. _out += 4;
  555. }
  556. for (; i < pixels; i++) {
  557. UINT32 iv = MAKE_UINT32(in[0], in[1], in[2], 255);
  558. memcpy(_out, &iv, sizeof(iv));
  559. in += 3;
  560. _out += 4;
  561. }
  562. }
  563. void
  564. unpackRGB16L(UINT8 *_out, const UINT8 *in, int pixels) {
  565. int i;
  566. /* 16-bit RGB triplets, little-endian order */
  567. for (i = 0; i < pixels; i++) {
  568. UINT32 iv = MAKE_UINT32(in[1], in[3], in[5], 255);
  569. memcpy(_out, &iv, sizeof(iv));
  570. in += 6;
  571. _out += 4;
  572. }
  573. }
  574. void
  575. unpackRGB16B(UINT8 *_out, const UINT8 *in, int pixels) {
  576. int i;
  577. /* 16-bit RGB triplets, big-endian order */
  578. for (i = 0; i < pixels; i++) {
  579. UINT32 iv = MAKE_UINT32(in[0], in[2], in[4], 255);
  580. memcpy(_out, &iv, sizeof(iv));
  581. in += 6;
  582. _out += 4;
  583. }
  584. }
  585. static void
  586. unpackRGBL(UINT8 *_out, const UINT8 *in, int pixels) {
  587. int i;
  588. /* RGB, line interleaved */
  589. for (i = 0; i < pixels; i++, _out += 4) {
  590. UINT32 iv = MAKE_UINT32(in[i], in[i + pixels], in[i + pixels + pixels], 255);
  591. memcpy(_out, &iv, sizeof(iv));
  592. }
  593. }
  594. static void
  595. unpackRGBR(UINT8 *_out, const UINT8 *in, int pixels) {
  596. int i;
  597. /* RGB, bit reversed */
  598. for (i = 0; i < pixels; i++) {
  599. UINT32 iv = MAKE_UINT32(BITFLIP[in[0]], BITFLIP[in[1]], BITFLIP[in[2]], 255);
  600. memcpy(_out, &iv, sizeof(iv));
  601. in += 3;
  602. _out += 4;
  603. }
  604. }
  605. void
  606. ImagingUnpackBGR(UINT8 *_out, const UINT8 *in, int pixels) {
  607. int i;
  608. /* RGB, reversed bytes */
  609. for (i = 0; i < pixels; i++) {
  610. UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], 255);
  611. memcpy(_out, &iv, sizeof(iv));
  612. in += 3;
  613. _out += 4;
  614. }
  615. }
  616. void
  617. ImagingUnpackRGB15(UINT8 *out, const UINT8 *in, int pixels) {
  618. int i, pixel;
  619. /* RGB, 5 bits per pixel */
  620. for (i = 0; i < pixels; i++) {
  621. pixel = in[0] + (in[1] << 8);
  622. out[R] = (pixel & 31) * 255 / 31;
  623. out[G] = ((pixel >> 5) & 31) * 255 / 31;
  624. out[B] = ((pixel >> 10) & 31) * 255 / 31;
  625. out[A] = 255;
  626. out += 4;
  627. in += 2;
  628. }
  629. }
  630. void
  631. ImagingUnpackRGBA15(UINT8 *out, const UINT8 *in, int pixels) {
  632. int i, pixel;
  633. /* RGB, 5/5/5/1 bits per pixel */
  634. for (i = 0; i < pixels; i++) {
  635. pixel = in[0] + (in[1] << 8);
  636. out[R] = (pixel & 31) * 255 / 31;
  637. out[G] = ((pixel >> 5) & 31) * 255 / 31;
  638. out[B] = ((pixel >> 10) & 31) * 255 / 31;
  639. out[A] = (pixel >> 15) * 255;
  640. out += 4;
  641. in += 2;
  642. }
  643. }
  644. void
  645. ImagingUnpackBGR15(UINT8 *out, const UINT8 *in, int pixels) {
  646. int i, pixel;
  647. /* RGB, reversed bytes, 5 bits per pixel */
  648. for (i = 0; i < pixels; i++) {
  649. pixel = in[0] + (in[1] << 8);
  650. out[B] = (pixel & 31) * 255 / 31;
  651. out[G] = ((pixel >> 5) & 31) * 255 / 31;
  652. out[R] = ((pixel >> 10) & 31) * 255 / 31;
  653. out[A] = 255;
  654. out += 4;
  655. in += 2;
  656. }
  657. }
  658. void
  659. ImagingUnpackBGRA15(UINT8 *out, const UINT8 *in, int pixels) {
  660. int i, pixel;
  661. /* RGB, rearranged channels, 5/5/5/1 bits per pixel */
  662. for (i = 0; i < pixels; i++) {
  663. pixel = in[0] + (in[1] << 8);
  664. out[B] = (pixel & 31) * 255 / 31;
  665. out[G] = ((pixel >> 5) & 31) * 255 / 31;
  666. out[R] = ((pixel >> 10) & 31) * 255 / 31;
  667. out[A] = (pixel >> 15) * 255;
  668. out += 4;
  669. in += 2;
  670. }
  671. }
  672. void
  673. ImagingUnpackRGB16(UINT8 *out, const UINT8 *in, int pixels) {
  674. int i, pixel;
  675. /* RGB, 5/6/5 bits per pixel */
  676. for (i = 0; i < pixels; i++) {
  677. pixel = in[0] + (in[1] << 8);
  678. out[R] = (pixel & 31) * 255 / 31;
  679. out[G] = ((pixel >> 5) & 63) * 255 / 63;
  680. out[B] = ((pixel >> 11) & 31) * 255 / 31;
  681. out[A] = 255;
  682. out += 4;
  683. in += 2;
  684. }
  685. }
  686. void
  687. ImagingUnpackBGR16(UINT8 *out, const UINT8 *in, int pixels) {
  688. int i, pixel;
  689. /* RGB, reversed bytes, 5/6/5 bits per pixel */
  690. for (i = 0; i < pixels; i++) {
  691. pixel = in[0] + (in[1] << 8);
  692. out[B] = (pixel & 31) * 255 / 31;
  693. out[G] = ((pixel >> 5) & 63) * 255 / 63;
  694. out[R] = ((pixel >> 11) & 31) * 255 / 31;
  695. out[A] = 255;
  696. out += 4;
  697. in += 2;
  698. }
  699. }
  700. void
  701. ImagingUnpackRGB4B(UINT8 *out, const UINT8 *in, int pixels) {
  702. int i, pixel;
  703. /* RGB, 4 bits per pixel */
  704. for (i = 0; i < pixels; i++) {
  705. pixel = in[0] + (in[1] << 8);
  706. out[R] = (pixel & 15) * 17;
  707. out[G] = ((pixel >> 4) & 15) * 17;
  708. out[B] = ((pixel >> 8) & 15) * 17;
  709. out[A] = 255;
  710. out += 4;
  711. in += 2;
  712. }
  713. }
  714. void
  715. ImagingUnpackRGBA4B(UINT8 *out, const UINT8 *in, int pixels) {
  716. int i, pixel;
  717. /* RGBA, 4 bits per pixel */
  718. for (i = 0; i < pixels; i++) {
  719. pixel = in[0] + (in[1] << 8);
  720. out[R] = (pixel & 15) * 17;
  721. out[G] = ((pixel >> 4) & 15) * 17;
  722. out[B] = ((pixel >> 8) & 15) * 17;
  723. out[A] = ((pixel >> 12) & 15) * 17;
  724. out += 4;
  725. in += 2;
  726. }
  727. }
  728. static void
  729. ImagingUnpackBGRX(UINT8 *_out, const UINT8 *in, int pixels) {
  730. int i;
  731. /* RGB, reversed bytes with padding */
  732. for (i = 0; i < pixels; i++) {
  733. UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], 255);
  734. memcpy(_out, &iv, sizeof(iv));
  735. in += 4;
  736. _out += 4;
  737. }
  738. }
  739. static void
  740. ImagingUnpackXRGB(UINT8 *_out, const UINT8 *in, int pixels) {
  741. int i;
  742. /* RGB, leading pad */
  743. for (i = 0; i < pixels; i++) {
  744. UINT32 iv = MAKE_UINT32(in[1], in[2], in[3], 255);
  745. memcpy(_out, &iv, sizeof(iv));
  746. in += 4;
  747. _out += 4;
  748. }
  749. }
  750. static void
  751. ImagingUnpackXBGR(UINT8 *_out, const UINT8 *in, int pixels) {
  752. int i;
  753. /* RGB, reversed bytes, leading pad */
  754. for (i = 0; i < pixels; i++) {
  755. UINT32 iv = MAKE_UINT32(in[3], in[2], in[1], 255);
  756. memcpy(_out, &iv, sizeof(iv));
  757. in += 4;
  758. _out += 4;
  759. }
  760. }
  761. /* Unpack to "RGBA" image */
  762. static void
  763. unpackRGBALA(UINT8 *_out, const UINT8 *in, int pixels) {
  764. int i;
  765. /* grayscale with alpha */
  766. for (i = 0; i < pixels; i++) {
  767. UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[1]);
  768. memcpy(_out, &iv, sizeof(iv));
  769. in += 2;
  770. _out += 4;
  771. }
  772. }
  773. static void
  774. unpackRGBALA16B(UINT8 *_out, const UINT8 *in, int pixels) {
  775. int i;
  776. /* 16-bit grayscale with alpha, big-endian */
  777. for (i = 0; i < pixels; i++) {
  778. UINT32 iv = MAKE_UINT32(in[0], in[0], in[0], in[2]);
  779. memcpy(_out, &iv, sizeof(iv));
  780. in += 4;
  781. _out += 4;
  782. }
  783. }
  784. static void
  785. unpackRGBa16L(UINT8 *_out, const UINT8 *in, int pixels) {
  786. int i;
  787. /* premultiplied 16-bit RGBA, little-endian */
  788. for (i = 0; i < pixels; i++) {
  789. int a = in[7];
  790. UINT32 iv;
  791. if (!a) {
  792. iv = 0;
  793. } else if (a == 255) {
  794. iv = MAKE_UINT32(in[1], in[3], in[5], a);
  795. } else {
  796. iv = MAKE_UINT32(
  797. CLIP8(in[1] * 255 / a),
  798. CLIP8(in[3] * 255 / a),
  799. CLIP8(in[5] * 255 / a),
  800. a);
  801. }
  802. memcpy(_out, &iv, sizeof(iv));
  803. in += 8;
  804. _out += 4;
  805. }
  806. }
  807. static void
  808. unpackRGBa16B(UINT8 *_out, const UINT8 *in, int pixels) {
  809. int i;
  810. /* premultiplied 16-bit RGBA, big-endian */
  811. for (i = 0; i < pixels; i++) {
  812. int a = in[6];
  813. UINT32 iv;
  814. if (!a) {
  815. iv = 0;
  816. } else if (a == 255) {
  817. iv = MAKE_UINT32(in[0], in[2], in[4], a);
  818. } else {
  819. iv = MAKE_UINT32(
  820. CLIP8(in[0] * 255 / a),
  821. CLIP8(in[2] * 255 / a),
  822. CLIP8(in[4] * 255 / a),
  823. a);
  824. }
  825. memcpy(_out, &iv, sizeof(iv));
  826. in += 8;
  827. _out += 4;
  828. }
  829. }
  830. static void
  831. unpackRGBa(UINT8 *_out, const UINT8 *in, int pixels) {
  832. int i;
  833. /* premultiplied RGBA */
  834. for (i = 0; i < pixels; i++) {
  835. int a = in[3];
  836. UINT32 iv;
  837. if (!a) {
  838. iv = 0;
  839. } else if (a == 255) {
  840. iv = MAKE_UINT32(in[0], in[1], in[2], a);
  841. } else {
  842. iv = MAKE_UINT32(
  843. CLIP8(in[0] * 255 / a),
  844. CLIP8(in[1] * 255 / a),
  845. CLIP8(in[2] * 255 / a),
  846. a);
  847. }
  848. memcpy(_out, &iv, sizeof(iv));
  849. in += 4;
  850. _out += 4;
  851. }
  852. }
  853. static void
  854. unpackRGBaskip1(UINT8 *_out, const UINT8 *in, int pixels) {
  855. int i;
  856. UINT32 *out = (UINT32 *)_out;
  857. /* premultiplied RGBA */
  858. for (i = 0; i < pixels; i++) {
  859. int a = in[3];
  860. if (!a) {
  861. out[i] = 0;
  862. } else if (a == 255) {
  863. out[i] = MAKE_UINT32(in[0], in[1], in[2], a);
  864. } else {
  865. out[i] = MAKE_UINT32(
  866. CLIP8(in[0] * 255 / a),
  867. CLIP8(in[1] * 255 / a),
  868. CLIP8(in[2] * 255 / a),
  869. a);
  870. }
  871. in += 5;
  872. }
  873. }
  874. static void
  875. unpackRGBaskip2(UINT8 *_out, const UINT8 *in, int pixels) {
  876. int i;
  877. UINT32 *out = (UINT32 *)_out;
  878. /* premultiplied RGBA */
  879. for (i = 0; i < pixels; i++) {
  880. int a = in[3];
  881. if (!a) {
  882. out[i] = 0;
  883. } else if (a == 255) {
  884. out[i] = MAKE_UINT32(in[0], in[1], in[2], a);
  885. } else {
  886. out[i] = MAKE_UINT32(
  887. CLIP8(in[0] * 255 / a),
  888. CLIP8(in[1] * 255 / a),
  889. CLIP8(in[2] * 255 / a),
  890. a);
  891. }
  892. in += 6;
  893. }
  894. }
  895. static void
  896. unpackBGRa(UINT8 *_out, const UINT8 *in, int pixels) {
  897. int i;
  898. /* premultiplied BGRA */
  899. for (i = 0; i < pixels; i++) {
  900. int a = in[3];
  901. UINT32 iv;
  902. if (!a) {
  903. iv = 0;
  904. } else if (a == 255) {
  905. iv = MAKE_UINT32(in[2], in[1], in[0], a);
  906. } else {
  907. iv = MAKE_UINT32(
  908. CLIP8(in[2] * 255 / a),
  909. CLIP8(in[1] * 255 / a),
  910. CLIP8(in[0] * 255 / a),
  911. a);
  912. }
  913. memcpy(_out, &iv, sizeof(iv));
  914. in += 4;
  915. _out += 4;
  916. }
  917. }
  918. static void
  919. unpackRGBAI(UINT8 *out, const UINT8 *in, int pixels) {
  920. int i;
  921. /* RGBA, inverted RGB bytes (FlashPix) */
  922. for (i = 0; i < pixels; i++) {
  923. out[R] = ~in[0];
  924. out[G] = ~in[1];
  925. out[B] = ~in[2];
  926. out[A] = in[3];
  927. out += 4;
  928. in += 4;
  929. }
  930. }
  931. static void
  932. unpackRGBAL(UINT8 *_out, const UINT8 *in, int pixels) {
  933. int i;
  934. /* RGBA, line interleaved */
  935. for (i = 0; i < pixels; i++, _out += 4) {
  936. UINT32 iv = MAKE_UINT32(
  937. in[i],
  938. in[i + pixels],
  939. in[i + pixels + pixels],
  940. in[i + pixels + pixels + pixels]);
  941. memcpy(_out, &iv, sizeof(iv));
  942. }
  943. }
  944. void
  945. unpackRGBA16L(UINT8 *_out, const UINT8 *in, int pixels) {
  946. int i;
  947. /* 16-bit RGBA, little-endian order */
  948. for (i = 0; i < pixels; i++, _out += 4) {
  949. UINT32 iv = MAKE_UINT32(in[1], in[3], in[5], in[7]);
  950. memcpy(_out, &iv, sizeof(iv));
  951. in += 8;
  952. }
  953. }
  954. void
  955. unpackRGBA16B(UINT8 *_out, const UINT8 *in, int pixels) {
  956. int i;
  957. /* 16-bit RGBA, big-endian order */
  958. for (i = 0; i < pixels; i++, _out += 4) {
  959. UINT32 iv = MAKE_UINT32(in[0], in[2], in[4], in[6]);
  960. memcpy(_out, &iv, sizeof(iv));
  961. in += 8;
  962. }
  963. }
  964. static void
  965. unpackARGB(UINT8 *_out, const UINT8 *in, int pixels) {
  966. int i;
  967. /* RGBA, leading pad */
  968. for (i = 0; i < pixels; i++) {
  969. UINT32 iv = MAKE_UINT32(in[1], in[2], in[3], in[0]);
  970. memcpy(_out, &iv, sizeof(iv));
  971. in += 4;
  972. _out += 4;
  973. }
  974. }
  975. static void
  976. unpackABGR(UINT8 *_out, const UINT8 *in, int pixels) {
  977. int i;
  978. /* RGBA, reversed bytes */
  979. for (i = 0; i < pixels; i++) {
  980. UINT32 iv = MAKE_UINT32(in[3], in[2], in[1], in[0]);
  981. memcpy(_out, &iv, sizeof(iv));
  982. in += 4;
  983. _out += 4;
  984. }
  985. }
  986. static void
  987. unpackBGRA(UINT8 *_out, const UINT8 *in, int pixels) {
  988. int i;
  989. /* RGBA, rearranged channels */
  990. for (i = 0; i < pixels; i++) {
  991. UINT32 iv = MAKE_UINT32(in[2], in[1], in[0], in[3]);
  992. memcpy(_out, &iv, sizeof(iv));
  993. in += 4;
  994. _out += 4;
  995. }
  996. }
  997. static void
  998. unpackBGRA16L(UINT8 *_out, const UINT8 *in, int pixels) {
  999. int i;
  1000. /* 16-bit RGBA, little-endian order, rearranged channels */
  1001. for (i = 0; i < pixels; i++) {
  1002. UINT32 iv = MAKE_UINT32(in[5], in[3], in[1], in[7]);
  1003. memcpy(_out, &iv, sizeof(iv));
  1004. in += 8;
  1005. _out += 4;
  1006. }
  1007. }
  1008. static void
  1009. unpackBGRA16B(UINT8 *_out, const UINT8 *in, int pixels) {
  1010. int i;
  1011. /* 16-bit RGBA, big-endian order, rearranged channels */
  1012. for (i = 0; i < pixels; i++) {
  1013. UINT32 iv = MAKE_UINT32(in[4], in[2], in[0], in[6]);
  1014. memcpy(_out, &iv, sizeof(iv));
  1015. in += 8;
  1016. _out += 4;
  1017. }
  1018. }
  1019. /* Unpack to "CMYK" image */
  1020. static void
  1021. unpackCMYKI(UINT8 *_out, const UINT8 *in, int pixels) {
  1022. int i;
  1023. /* CMYK, inverted bytes (Photoshop 2.5) */
  1024. for (i = 0; i < pixels; i++) {
  1025. UINT32 iv = ~MAKE_UINT32(in[0], in[1], in[2], in[3]);
  1026. memcpy(_out, &iv, sizeof(iv));
  1027. in += 4;
  1028. _out += 4;
  1029. }
  1030. }
  1031. /* Unpack to "LAB" image */
  1032. /* There are two representations of LAB images for whatever precision:
  1033. L: Uint (in PS, it's 0-100)
  1034. A: Int (in ps, -128 .. 128, or elsewhere 0..255, with 128 as middle.
  1035. Channels in PS display a 0 value as middle gray,
  1036. LCMS appears to use 128 as the 0 value for these channels)
  1037. B: Int (as above)
  1038. Since we don't have any signed ints, we're going with the shifted versions
  1039. internally, and we'll unshift for saving and whatnot.
  1040. */
  1041. void
  1042. ImagingUnpackLAB(UINT8 *out, const UINT8 *in, int pixels) {
  1043. int i;
  1044. /* LAB triplets */
  1045. for (i = 0; i < pixels; i++) {
  1046. out[0] = in[0];
  1047. out[1] = in[1] ^ 128; /* signed in outside world */
  1048. out[2] = in[2] ^ 128;
  1049. out[3] = 255;
  1050. out += 4;
  1051. in += 3;
  1052. }
  1053. }
  1054. static void
  1055. unpackI16N_I16B(UINT8 *out, const UINT8 *in, int pixels) {
  1056. int i;
  1057. UINT8 *tmp = (UINT8 *)out;
  1058. for (i = 0; i < pixels; i++) {
  1059. C16B;
  1060. in += 2;
  1061. tmp += 2;
  1062. }
  1063. }
  1064. static void
  1065. unpackI16N_I16(UINT8 *out, const UINT8 *in, int pixels) {
  1066. int i;
  1067. UINT8 *tmp = (UINT8 *)out;
  1068. for (i = 0; i < pixels; i++) {
  1069. C16L;
  1070. in += 2;
  1071. tmp += 2;
  1072. }
  1073. }
  1074. static void
  1075. unpackI16B_I16(UINT8 *out, const UINT8 *in, int pixels) {
  1076. int i;
  1077. for (i = 0; i < pixels; i++) {
  1078. out[0] = in[1];
  1079. out[1] = in[0];
  1080. in += 2;
  1081. out += 2;
  1082. }
  1083. }
  1084. static void
  1085. unpackI16R_I16(UINT8 *out, const UINT8 *in, int pixels) {
  1086. int i;
  1087. for (i = 0; i < pixels; i++) {
  1088. out[0] = BITFLIP[in[0]];
  1089. out[1] = BITFLIP[in[1]];
  1090. in += 2;
  1091. out += 2;
  1092. }
  1093. }
  1094. static void
  1095. unpackI12_I16(UINT8 *out, const UINT8 *in, int pixels) {
  1096. /* Fillorder 1/MSB -> LittleEndian, for 12bit integer grayscale tiffs.
  1097. According to the TIFF spec:
  1098. FillOrder = 2 should be used only when BitsPerSample = 1 and
  1099. the data is either uncompressed or compressed using CCITT 1D
  1100. or 2D compression, to avoid potentially ambiguous situations.
  1101. Yeah. I thought so. We'll see how well people read the spec.
  1102. We've got several fillorder=2 modes in TiffImagePlugin.py
  1103. There's no spec I can find. It appears that the in storage
  1104. layout is: 00 80 00 ... -> (128 , 0 ...). The samples are
  1105. stored in a single big bitian 12bit block, but need to be
  1106. pulled out to little endian format to be stored in a 2 byte
  1107. int.
  1108. */
  1109. int i;
  1110. UINT16 pixel;
  1111. #ifdef WORDS_BIGENDIAN
  1112. UINT8 *tmp = (UINT8 *)&pixel;
  1113. #endif
  1114. for (i = 0; i < pixels - 1; i += 2) {
  1115. pixel = (((UINT16)in[0]) << 4) + (in[1] >> 4);
  1116. #ifdef WORDS_BIGENDIAN
  1117. out[0] = tmp[1];
  1118. out[1] = tmp[0];
  1119. #else
  1120. memcpy(out, &pixel, sizeof(pixel));
  1121. #endif
  1122. out += 2;
  1123. pixel = (((UINT16)(in[1] & 0x0F)) << 8) + in[2];
  1124. #ifdef WORDS_BIGENDIAN
  1125. out[0] = tmp[1];
  1126. out[1] = tmp[0];
  1127. #else
  1128. memcpy(out, &pixel, sizeof(pixel));
  1129. #endif
  1130. in += 3;
  1131. out += 2;
  1132. }
  1133. if (i == pixels - 1) {
  1134. pixel = (((UINT16)in[0]) << 4) + (in[1] >> 4);
  1135. #ifdef WORDS_BIGENDIAN
  1136. out[0] = tmp[1];
  1137. out[1] = tmp[0];
  1138. #else
  1139. memcpy(out, &pixel, sizeof(pixel));
  1140. #endif
  1141. }
  1142. }
  1143. static void
  1144. copy1(UINT8 *out, const UINT8 *in, int pixels) {
  1145. /* L, P */
  1146. memcpy(out, in, pixels);
  1147. }
  1148. static void
  1149. copy2(UINT8 *out, const UINT8 *in, int pixels) {
  1150. /* I;16 */
  1151. memcpy(out, in, pixels * 2);
  1152. }
  1153. static void
  1154. copy3(UINT8 *out, const UINT8 *in, int pixels) {
  1155. /* BGR;24 */
  1156. memcpy(out, in, pixels * 3);
  1157. }
  1158. static void
  1159. copy4(UINT8 *out, const UINT8 *in, int pixels) {
  1160. /* RGBA, CMYK quadruples */
  1161. memcpy(out, in, 4 * pixels);
  1162. }
  1163. static void
  1164. copy4skip1(UINT8 *_out, const UINT8 *in, int pixels) {
  1165. int i;
  1166. for (i = 0; i < pixels; i++) {
  1167. memcpy(_out, in, 4);
  1168. in += 5;
  1169. _out += 4;
  1170. }
  1171. }
  1172. static void
  1173. copy4skip2(UINT8 *_out, const UINT8 *in, int pixels) {
  1174. int i;
  1175. for (i = 0; i < pixels; i++) {
  1176. memcpy(_out, in, 4);
  1177. in += 6;
  1178. _out += 4;
  1179. }
  1180. }
  1181. /* Unpack to "I" and "F" images */
  1182. #define UNPACK_RAW(NAME, GET, INTYPE, OUTTYPE) \
  1183. static void NAME(UINT8 *out_, const UINT8 *in, int pixels) { \
  1184. int i; \
  1185. OUTTYPE *out = (OUTTYPE *)out_; \
  1186. for (i = 0; i < pixels; i++, in += sizeof(INTYPE)) { \
  1187. out[i] = (OUTTYPE)((INTYPE)GET); \
  1188. } \
  1189. }
  1190. #define UNPACK(NAME, COPY, INTYPE, OUTTYPE) \
  1191. static void NAME(UINT8 *out_, const UINT8 *in, int pixels) { \
  1192. int i; \
  1193. OUTTYPE *out = (OUTTYPE *)out_; \
  1194. INTYPE tmp_; \
  1195. UINT8 *tmp = (UINT8 *)&tmp_; \
  1196. for (i = 0; i < pixels; i++, in += sizeof(INTYPE)) { \
  1197. COPY; \
  1198. out[i] = (OUTTYPE)tmp_; \
  1199. } \
  1200. }
  1201. UNPACK_RAW(unpackI8, in[0], UINT8, INT32)
  1202. UNPACK_RAW(unpackI8S, in[0], INT8, INT32)
  1203. UNPACK(unpackI16, C16L, UINT16, INT32)
  1204. UNPACK(unpackI16S, C16L, INT16, INT32)
  1205. UNPACK(unpackI16B, C16B, UINT16, INT32)
  1206. UNPACK(unpackI16BS, C16B, INT16, INT32)
  1207. UNPACK(unpackI16N, C16N, UINT16, INT32)
  1208. UNPACK(unpackI16NS, C16N, INT16, INT32)
  1209. UNPACK(unpackI32, C32L, UINT32, INT32)
  1210. UNPACK(unpackI32S, C32L, INT32, INT32)
  1211. UNPACK(unpackI32B, C32B, UINT32, INT32)
  1212. UNPACK(unpackI32BS, C32B, INT32, INT32)
  1213. UNPACK(unpackI32N, C32N, UINT32, INT32)
  1214. UNPACK(unpackI32NS, C32N, INT32, INT32)
  1215. UNPACK_RAW(unpackF8, in[0], UINT8, FLOAT32)
  1216. UNPACK_RAW(unpackF8S, in[0], INT8, FLOAT32)
  1217. UNPACK(unpackF16, C16L, UINT16, FLOAT32)
  1218. UNPACK(unpackF16S, C16L, INT16, FLOAT32)
  1219. UNPACK(unpackF16B, C16B, UINT16, FLOAT32)
  1220. UNPACK(unpackF16BS, C16B, INT16, FLOAT32)
  1221. UNPACK(unpackF16N, C16N, UINT16, FLOAT32)
  1222. UNPACK(unpackF16NS, C16N, INT16, FLOAT32)
  1223. UNPACK(unpackF32, C32L, UINT32, FLOAT32)
  1224. UNPACK(unpackF32S, C32L, INT32, FLOAT32)
  1225. UNPACK(unpackF32B, C32B, UINT32, FLOAT32)
  1226. UNPACK(unpackF32BS, C32B, INT32, FLOAT32)
  1227. UNPACK(unpackF32N, C32N, UINT32, FLOAT32)
  1228. UNPACK(unpackF32NS, C32N, INT32, FLOAT32)
  1229. UNPACK(unpackF32F, C32L, FLOAT32, FLOAT32)
  1230. UNPACK(unpackF32BF, C32B, FLOAT32, FLOAT32)
  1231. UNPACK(unpackF32NF, C32N, FLOAT32, FLOAT32)
  1232. #ifdef FLOAT64
  1233. UNPACK(unpackF64F, C64L, FLOAT64, FLOAT32)
  1234. UNPACK(unpackF64BF, C64B, FLOAT64, FLOAT32)
  1235. UNPACK(unpackF64NF, C64N, FLOAT64, FLOAT32)
  1236. #endif
  1237. /* Misc. unpackers */
  1238. static void
  1239. band0(UINT8 *out, const UINT8 *in, int pixels) {
  1240. int i;
  1241. /* band 0 only */
  1242. for (i = 0; i < pixels; i++) {
  1243. out[0] = in[i];
  1244. out += 4;
  1245. }
  1246. }
  1247. static void
  1248. band1(UINT8 *out, const UINT8 *in, int pixels) {
  1249. int i;
  1250. /* band 1 only */
  1251. for (i = 0; i < pixels; i++) {
  1252. out[1] = in[i];
  1253. out += 4;
  1254. }
  1255. }
  1256. static void
  1257. band2(UINT8 *out, const UINT8 *in, int pixels) {
  1258. int i;
  1259. /* band 2 only */
  1260. for (i = 0; i < pixels; i++) {
  1261. out[2] = in[i];
  1262. out += 4;
  1263. }
  1264. }
  1265. static void
  1266. band3(UINT8 *out, const UINT8 *in, int pixels) {
  1267. /* band 3 only */
  1268. int i;
  1269. for (i = 0; i < pixels; i++) {
  1270. out[3] = in[i];
  1271. out += 4;
  1272. }
  1273. }
  1274. static void
  1275. band0I(UINT8 *out, const UINT8 *in, int pixels) {
  1276. int i;
  1277. /* band 0 only */
  1278. for (i = 0; i < pixels; i++) {
  1279. out[0] = ~in[i];
  1280. out += 4;
  1281. }
  1282. }
  1283. static void
  1284. band1I(UINT8 *out, const UINT8 *in, int pixels) {
  1285. int i;
  1286. /* band 1 only */
  1287. for (i = 0; i < pixels; i++) {
  1288. out[1] = ~in[i];
  1289. out += 4;
  1290. }
  1291. }
  1292. static void
  1293. band2I(UINT8 *out, const UINT8 *in, int pixels) {
  1294. int i;
  1295. /* band 2 only */
  1296. for (i = 0; i < pixels; i++) {
  1297. out[2] = ~in[i];
  1298. out += 4;
  1299. }
  1300. }
  1301. static void
  1302. band3I(UINT8 *out, const UINT8 *in, int pixels) {
  1303. /* band 3 only */
  1304. int i;
  1305. for (i = 0; i < pixels; i++) {
  1306. out[3] = ~in[i];
  1307. out += 4;
  1308. }
  1309. }
  1310. static void
  1311. band016B(UINT8* out, const UINT8* in, int pixels)
  1312. {
  1313. int i;
  1314. /* band 0 only, big endian */
  1315. for (i = 0; i < pixels; i++) {
  1316. out[0] = in[0];
  1317. out += 4; in += 2;
  1318. }
  1319. }
  1320. static void
  1321. band116B(UINT8* out, const UINT8* in, int pixels)
  1322. {
  1323. int i;
  1324. /* band 1 only, big endian */
  1325. for (i = 0; i < pixels; i++) {
  1326. out[1] = in[0];
  1327. out += 4; in += 2;
  1328. }
  1329. }
  1330. static void
  1331. band216B(UINT8* out, const UINT8* in, int pixels)
  1332. {
  1333. int i;
  1334. /* band 2 only, big endian */
  1335. for (i = 0; i < pixels; i++) {
  1336. out[2] = in[0];
  1337. out += 4; in += 2;
  1338. }
  1339. }
  1340. static void
  1341. band316B(UINT8* out, const UINT8* in, int pixels)
  1342. {
  1343. int i;
  1344. /* band 3 only, big endian */
  1345. for (i = 0; i < pixels; i++) {
  1346. out[3] = in[0];
  1347. out += 4; in += 2;
  1348. }
  1349. }
  1350. static void
  1351. band016L(UINT8* out, const UINT8* in, int pixels)
  1352. {
  1353. int i;
  1354. /* band 0 only, little endian */
  1355. for (i = 0; i < pixels; i++) {
  1356. out[0] = in[1];
  1357. out += 4; in += 2;
  1358. }
  1359. }
  1360. static void
  1361. band116L(UINT8* out, const UINT8* in, int pixels)
  1362. {
  1363. int i;
  1364. /* band 1 only, little endian */
  1365. for (i = 0; i < pixels; i++) {
  1366. out[1] = in[1];
  1367. out += 4; in += 2;
  1368. }
  1369. }
  1370. static void
  1371. band216L(UINT8* out, const UINT8* in, int pixels)
  1372. {
  1373. int i;
  1374. /* band 2 only, little endian */
  1375. for (i = 0; i < pixels; i++) {
  1376. out[2] = in[1];
  1377. out += 4; in += 2;
  1378. }
  1379. }
  1380. static void
  1381. band316L(UINT8* out, const UINT8* in, int pixels)
  1382. {
  1383. int i;
  1384. /* band 3 only, little endian */
  1385. for (i = 0; i < pixels; i++) {
  1386. out[3] = in[1];
  1387. out += 4; in += 2;
  1388. }
  1389. }
  1390. static struct {
  1391. const char *mode;
  1392. const char *rawmode;
  1393. int bits;
  1394. ImagingShuffler unpack;
  1395. } unpackers[] = {
  1396. /* raw mode syntax is "<mode>;<bits><flags>" where "bits" defaults
  1397. depending on mode (1 for "1", 8 for "P" and "L", etc), and
  1398. "flags" should be given in alphabetical order. if both bits
  1399. and flags have their default values, the ; should be left out */
  1400. /* flags: "I" inverted data; "R" reversed bit order; "B" big
  1401. endian byte order (default is little endian); "L" line
  1402. interleave, "S" signed, "F" floating point */
  1403. /* exception: rawmodes "I" and "F" are always native endian byte order */
  1404. /* bilevel */
  1405. {"1", "1", 1, unpack1},
  1406. {"1", "1;I", 1, unpack1I},
  1407. {"1", "1;R", 1, unpack1R},
  1408. {"1", "1;IR", 1, unpack1IR},
  1409. {"1", "1;8", 8, unpack18},
  1410. /* grayscale */
  1411. {"L", "L;2", 2, unpackL2},
  1412. {"L", "L;2I", 2, unpackL2I},
  1413. {"L", "L;2R", 2, unpackL2R},
  1414. {"L", "L;2IR", 2, unpackL2IR},
  1415. {"L", "L;4", 4, unpackL4},
  1416. {"L", "L;4I", 4, unpackL4I},
  1417. {"L", "L;4R", 4, unpackL4R},
  1418. {"L", "L;4IR", 4, unpackL4IR},
  1419. {"L", "L", 8, copy1},
  1420. {"L", "L;I", 8, unpackLI},
  1421. {"L", "L;R", 8, unpackLR},
  1422. {"L", "L;16", 16, unpackL16},
  1423. {"L", "L;16B", 16, unpackL16B},
  1424. /* grayscale w. alpha */
  1425. {"LA", "LA", 16, unpackLA},
  1426. {"LA", "LA;L", 16, unpackLAL},
  1427. /* grayscale w. alpha premultiplied */
  1428. {"La", "La", 16, unpackLA},
  1429. /* palette */
  1430. {"P", "P;1", 1, unpackP1},
  1431. {"P", "P;2", 2, unpackP2},
  1432. {"P", "P;2L", 2, unpackP2L},
  1433. {"P", "P;4", 4, unpackP4},
  1434. {"P", "P;4L", 4, unpackP4L},
  1435. {"P", "P", 8, copy1},
  1436. {"P", "P;R", 8, unpackLR},
  1437. {"P", "L", 8, copy1},
  1438. /* palette w. alpha */
  1439. {"PA", "PA", 16, unpackLA},
  1440. {"PA", "PA;L", 16, unpackLAL},
  1441. {"PA", "LA", 16, unpackLA},
  1442. /* true colour */
  1443. {"RGB", "RGB", 24, ImagingUnpackRGB},
  1444. {"RGB", "RGB;L", 24, unpackRGBL},
  1445. {"RGB", "RGB;R", 24, unpackRGBR},
  1446. {"RGB", "RGB;16L", 48, unpackRGB16L},
  1447. {"RGB", "RGB;16B", 48, unpackRGB16B},
  1448. {"RGB", "BGR", 24, ImagingUnpackBGR},
  1449. {"RGB", "RGB;15", 16, ImagingUnpackRGB15},
  1450. {"RGB", "BGR;15", 16, ImagingUnpackBGR15},
  1451. {"RGB", "RGB;16", 16, ImagingUnpackRGB16},
  1452. {"RGB", "BGR;16", 16, ImagingUnpackBGR16},
  1453. {"RGB", "RGB;4B", 16, ImagingUnpackRGB4B},
  1454. {"RGB", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */
  1455. {"RGB", "RGBX", 32, copy4},
  1456. {"RGB", "RGBX;L", 32, unpackRGBAL},
  1457. {"RGB", "RGBA;L", 32, unpackRGBAL},
  1458. {"RGB", "RGBA;15", 16, ImagingUnpackRGBA15},
  1459. {"RGB", "BGRX", 32, ImagingUnpackBGRX},
  1460. {"RGB", "XRGB", 32, ImagingUnpackXRGB},
  1461. {"RGB", "XBGR", 32, ImagingUnpackXBGR},
  1462. {"RGB", "YCC;P", 24, ImagingUnpackYCC},
  1463. {"RGB", "R", 8, band0},
  1464. {"RGB", "G", 8, band1},
  1465. {"RGB", "B", 8, band2},
  1466. {"RGB", "R;16L", 16, band016L},
  1467. {"RGB", "G;16L", 16, band116L},
  1468. {"RGB", "B;16L", 16, band216L},
  1469. {"RGB", "R;16B", 16, band016B},
  1470. {"RGB", "G;16B", 16, band116B},
  1471. {"RGB", "B;16B", 16, band216B},
  1472. {"RGB", "CMYK", 32, cmyk2rgb},
  1473. {"BGR;15", "BGR;15", 16, copy2},
  1474. {"BGR;16", "BGR;16", 16, copy2},
  1475. {"BGR;24", "BGR;24", 24, copy3},
  1476. /* true colour w. alpha */
  1477. {"RGBA", "LA", 16, unpackRGBALA},
  1478. {"RGBA", "LA;16B", 32, unpackRGBALA16B},
  1479. {"RGBA", "RGBA", 32, copy4},
  1480. {"RGBA", "RGBAX", 40, copy4skip1},
  1481. {"RGBA", "RGBAXX", 48, copy4skip2},
  1482. {"RGBA", "RGBa", 32, unpackRGBa},
  1483. {"RGBA", "RGBaX", 40, unpackRGBaskip1},
  1484. {"RGBA", "RGBaXX", 48, unpackRGBaskip2},
  1485. {"RGBA", "RGBa;16L", 64, unpackRGBa16L},
  1486. {"RGBA", "RGBa;16B", 64, unpackRGBa16B},
  1487. {"RGBA", "BGRa", 32, unpackBGRa},
  1488. {"RGBA", "RGBA;I", 32, unpackRGBAI},
  1489. {"RGBA", "RGBA;L", 32, unpackRGBAL},
  1490. {"RGBA", "RGBA;15", 16, ImagingUnpackRGBA15},
  1491. {"RGBA", "BGRA;15", 16, ImagingUnpackBGRA15},
  1492. {"RGBA", "RGBA;4B", 16, ImagingUnpackRGBA4B},
  1493. {"RGBA", "RGBA;16L", 64, unpackRGBA16L},
  1494. {"RGBA", "RGBA;16B", 64, unpackRGBA16B},
  1495. {"RGBA", "BGRA", 32, unpackBGRA},
  1496. {"RGBA", "BGRA;16L", 64, unpackBGRA16L},
  1497. {"RGBA", "BGRA;16B", 64, unpackBGRA16B},
  1498. {"RGBA", "ARGB", 32, unpackARGB},
  1499. {"RGBA", "ABGR", 32, unpackABGR},
  1500. {"RGBA", "YCCA;P", 32, ImagingUnpackYCCA},
  1501. {"RGBA", "R", 8, band0},
  1502. {"RGBA", "G", 8, band1},
  1503. {"RGBA", "B", 8, band2},
  1504. {"RGBA", "A", 8, band3},
  1505. {"RGBA", "R;16L", 16, band016L},
  1506. {"RGBA", "G;16L", 16, band116L},
  1507. {"RGBA", "B;16L", 16, band216L},
  1508. {"RGBA", "A;16L", 16, band316L},
  1509. {"RGBA", "R;16B", 16, band016B},
  1510. {"RGBA", "G;16B", 16, band116B},
  1511. {"RGBA", "B;16B", 16, band216B},
  1512. {"RGBA", "A;16B", 16, band316B},
  1513. #ifdef WORDS_BIGENDIAN
  1514. {"RGB", "RGB;16N", 48, unpackRGB16B},
  1515. {"RGBA", "RGBa;16N", 64, unpackRGBa16B},
  1516. {"RGBA", "RGBA;16N", 64, unpackRGBA16B},
  1517. {"RGBX", "RGBX;16N", 64, unpackRGBA16B},
  1518. {"RGB", "R;16N", 16, band016B},
  1519. {"RGB", "G;16N", 16, band116B},
  1520. {"RGB", "B;16N", 16, band216B},
  1521. {"RGBA", "R;16N", 16, band016B},
  1522. {"RGBA", "G;16N", 16, band116B},
  1523. {"RGBA", "B;16N", 16, band216B},
  1524. {"RGBA", "A;16N", 16, band316B},
  1525. #else
  1526. {"RGB", "RGB;16N", 48, unpackRGB16L},
  1527. {"RGBA", "RGBa;16N", 64, unpackRGBa16L},
  1528. {"RGBA", "RGBA;16N", 64, unpackRGBA16L},
  1529. {"RGBX", "RGBX;16N", 64, unpackRGBA16L},
  1530. {"RGB", "R;16N", 16, band016L},
  1531. {"RGB", "G;16N", 16, band116L},
  1532. {"RGB", "B;16N", 16, band216L},
  1533. {"RGBA", "R;16N", 16, band016L},
  1534. {"RGBA", "G;16N", 16, band116L},
  1535. {"RGBA", "B;16N", 16, band216L},
  1536. {"RGBA", "A;16N", 16, band316L},
  1537. #endif
  1538. /* true colour w. alpha premultiplied */
  1539. {"RGBa", "RGBa", 32, copy4},
  1540. {"RGBa", "BGRa", 32, unpackBGRA},
  1541. {"RGBa", "aRGB", 32, unpackARGB},
  1542. {"RGBa", "aBGR", 32, unpackABGR},
  1543. /* true colour w. padding */
  1544. {"RGBX", "RGB", 24, ImagingUnpackRGB},
  1545. {"RGBX", "RGB;L", 24, unpackRGBL},
  1546. {"RGBX", "RGB;16B", 48, unpackRGB16B},
  1547. {"RGBX", "BGR", 24, ImagingUnpackBGR},
  1548. {"RGBX", "RGB;15", 16, ImagingUnpackRGB15},
  1549. {"RGBX", "BGR;15", 16, ImagingUnpackBGR15},
  1550. {"RGBX", "RGB;4B", 16, ImagingUnpackRGB4B},
  1551. {"RGBX", "BGR;5", 16, ImagingUnpackBGR15}, /* compat */
  1552. {"RGBX", "RGBX", 32, copy4},
  1553. {"RGBX", "RGBXX", 40, copy4skip1},
  1554. {"RGBX", "RGBXXX", 48, copy4skip2},
  1555. {"RGBX", "RGBX;L", 32, unpackRGBAL},
  1556. {"RGBX", "RGBX;16L", 64, unpackRGBA16L},
  1557. {"RGBX", "RGBX;16B", 64, unpackRGBA16B},
  1558. {"RGBX", "BGRX", 32, ImagingUnpackBGRX},
  1559. {"RGBX", "XRGB", 32, ImagingUnpackXRGB},
  1560. {"RGBX", "XBGR", 32, ImagingUnpackXBGR},
  1561. {"RGBX", "YCC;P", 24, ImagingUnpackYCC},
  1562. {"RGBX", "R", 8, band0},
  1563. {"RGBX", "G", 8, band1},
  1564. {"RGBX", "B", 8, band2},
  1565. {"RGBX", "X", 8, band3},
  1566. /* colour separation */
  1567. {"CMYK", "CMYK", 32, copy4},
  1568. {"CMYK", "CMYKX", 40, copy4skip1},
  1569. {"CMYK", "CMYKXX", 48, copy4skip2},
  1570. {"CMYK", "CMYK;I", 32, unpackCMYKI},
  1571. {"CMYK", "CMYK;L", 32, unpackRGBAL},
  1572. {"CMYK", "CMYK;16L", 64, unpackRGBA16L},
  1573. {"CMYK", "CMYK;16B", 64, unpackRGBA16B},
  1574. {"CMYK", "C", 8, band0},
  1575. {"CMYK", "M", 8, band1},
  1576. {"CMYK", "Y", 8, band2},
  1577. {"CMYK", "K", 8, band3},
  1578. {"CMYK", "C;I", 8, band0I},
  1579. {"CMYK", "M;I", 8, band1I},
  1580. {"CMYK", "Y;I", 8, band2I},
  1581. {"CMYK", "K;I", 8, band3I},
  1582. #ifdef WORDS_BIGENDIAN
  1583. {"CMYK", "CMYK;16N", 64, unpackRGBA16B},
  1584. #else
  1585. {"CMYK", "CMYK;16N", 64, unpackRGBA16L},
  1586. #endif
  1587. /* video (YCbCr) */
  1588. {"YCbCr", "YCbCr", 24, ImagingUnpackRGB},
  1589. {"YCbCr", "YCbCr;L", 24, unpackRGBL},
  1590. {"YCbCr", "YCbCrX", 32, copy4},
  1591. {"YCbCr", "YCbCrK", 32, copy4},
  1592. /* LAB Color */
  1593. {"LAB", "LAB", 24, ImagingUnpackLAB},
  1594. {"LAB", "L", 8, band0},
  1595. {"LAB", "A", 8, band1},
  1596. {"LAB", "B", 8, band2},
  1597. /* HSV Color */
  1598. {"HSV", "HSV", 24, ImagingUnpackRGB},
  1599. {"HSV", "H", 8, band0},
  1600. {"HSV", "S", 8, band1},
  1601. {"HSV", "V", 8, band2},
  1602. /* integer variations */
  1603. {"I", "I", 32, copy4},
  1604. {"I", "I;8", 8, unpackI8},
  1605. {"I", "I;8S", 8, unpackI8S},
  1606. {"I", "I;16", 16, unpackI16},
  1607. {"I", "I;16S", 16, unpackI16S},
  1608. {"I", "I;16B", 16, unpackI16B},
  1609. {"I", "I;16BS", 16, unpackI16BS},
  1610. {"I", "I;16N", 16, unpackI16N},
  1611. {"I", "I;16NS", 16, unpackI16NS},
  1612. {"I", "I;32", 32, unpackI32},
  1613. {"I", "I;32S", 32, unpackI32S},
  1614. {"I", "I;32B", 32, unpackI32B},
  1615. {"I", "I;32BS", 32, unpackI32BS},
  1616. {"I", "I;32N", 32, unpackI32N},
  1617. {"I", "I;32NS", 32, unpackI32NS},
  1618. /* floating point variations */
  1619. {"F", "F", 32, copy4},
  1620. {"F", "F;8", 8, unpackF8},
  1621. {"F", "F;8S", 8, unpackF8S},
  1622. {"F", "F;16", 16, unpackF16},
  1623. {"F", "F;16S", 16, unpackF16S},
  1624. {"F", "F;16B", 16, unpackF16B},
  1625. {"F", "F;16BS", 16, unpackF16BS},
  1626. {"F", "F;16N", 16, unpackF16N},
  1627. {"F", "F;16NS", 16, unpackF16NS},
  1628. {"F", "F;32", 32, unpackF32},
  1629. {"F", "F;32S", 32, unpackF32S},
  1630. {"F", "F;32B", 32, unpackF32B},
  1631. {"F", "F;32BS", 32, unpackF32BS},
  1632. {"F", "F;32N", 32, unpackF32N},
  1633. {"F", "F;32NS", 32, unpackF32NS},
  1634. {"F", "F;32F", 32, unpackF32F},
  1635. {"F", "F;32BF", 32, unpackF32BF},
  1636. {"F", "F;32NF", 32, unpackF32NF},
  1637. #ifdef FLOAT64
  1638. {"F", "F;64F", 64, unpackF64F},
  1639. {"F", "F;64BF", 64, unpackF64BF},
  1640. {"F", "F;64NF", 64, unpackF64NF},
  1641. #endif
  1642. /* storage modes */
  1643. {"I;16", "I;16", 16, copy2},
  1644. {"I;16B", "I;16B", 16, copy2},
  1645. {"I;16L", "I;16L", 16, copy2},
  1646. {"I;16N", "I;16N", 16, copy2},
  1647. {"I;16", "I;16B", 16, unpackI16B_I16},
  1648. {"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
  1649. {"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
  1650. {"I;16B", "I;16N", 16, unpackI16N_I16B},
  1651. {"I;16", "I;16R", 16, unpackI16R_I16},
  1652. {"I;16", "I;12", 12, unpackI12_I16}, // 12 bit Tiffs stored in 16bits.
  1653. {NULL} /* sentinel */
  1654. };
  1655. ImagingShuffler
  1656. ImagingFindUnpacker(const char *mode, const char *rawmode, int *bits_out) {
  1657. int i;
  1658. /* find a suitable pixel unpacker */
  1659. for (i = 0; unpackers[i].rawmode; i++) {
  1660. if (strcmp(unpackers[i].mode, mode) == 0 &&
  1661. strcmp(unpackers[i].rawmode, rawmode) == 0) {
  1662. if (bits_out) {
  1663. *bits_out = unpackers[i].bits;
  1664. }
  1665. return unpackers[i].unpack;
  1666. }
  1667. }
  1668. /* FIXME: configure a general unpacker based on the type codes... */
  1669. return NULL;
  1670. }