Unpack.c 44 KB

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