Pack.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * The Python Imaging Library.
  3. * $Id$
  4. *
  5. * code to pack raw data
  6. *
  7. * history:
  8. * 1996-04-30 fl Created
  9. * 1996-05-12 fl Published a few RGB packers
  10. * 1996-11-01 fl More RGB packers (Tk booster stuff)
  11. * 1996-12-30 fl Added P;1, P;2 and P;4 packers
  12. * 1997-06-02 fl Added F (F;32NF) packer
  13. * 1997-08-28 fl Added 1 as L packer
  14. * 1998-02-08 fl Added I packer
  15. * 1998-03-09 fl Added mode field, RGBA/RGBX as RGB packers
  16. * 1998-07-01 fl Added YCbCr support
  17. * 1998-07-12 fl Added I 16 packer
  18. * 1999-02-03 fl Added BGR packers
  19. * 2003-09-26 fl Added LA/PA packers
  20. * 2006-06-22 fl Added CMYK;I packer
  21. *
  22. * Copyright (c) 1997-2006 by Secret Labs AB.
  23. * Copyright (c) 1996-1997 by Fredrik Lundh.
  24. *
  25. * See the README file for information on usage and redistribution.
  26. */
  27. #include "Imaging.h"
  28. #define R 0
  29. #define G 1
  30. #define B 2
  31. #define X 3
  32. #define A 3
  33. #define C 0
  34. #define M 1
  35. #define Y 2
  36. #define K 3
  37. /* byte swapping macros */
  38. #define C16N\
  39. (out[0]=tmp[0], out[1]=tmp[1]);
  40. #define C16S\
  41. (out[1]=tmp[0], out[0]=tmp[1]);
  42. #define C32N\
  43. (out[0]=tmp[0], out[1]=tmp[1], out[2]=tmp[2], out[3]=tmp[3]);
  44. #define C32S\
  45. (out[3]=tmp[0], out[2]=tmp[1], out[1]=tmp[2], out[0]=tmp[3]);
  46. #define C64N\
  47. (out[0]=tmp[0], out[1]=tmp[1], out[2]=tmp[2], out[3]=tmp[3],\
  48. out[4]=tmp[4], out[5]=tmp[5], out[6]=tmp[6], out[7]=tmp[7]);
  49. #define C64S\
  50. (out[7]=tmp[0], out[6]=tmp[1], out[5]=tmp[2], out[4]=tmp[3],\
  51. out[3]=tmp[4], out[2]=tmp[5], out[1]=tmp[6], out[0]=tmp[7]);
  52. #ifdef WORDS_BIGENDIAN
  53. #define C16B C16N
  54. #define C16L C16S
  55. #define C32B C32N
  56. #define C32L C32S
  57. #define C64B C64N
  58. #define C64L C64S
  59. #else
  60. #define C16B C16S
  61. #define C16L C16N
  62. #define C32B C32S
  63. #define C32L C32N
  64. #define C64B C64S
  65. #define C64L C64N
  66. #endif
  67. static void
  68. pack1(UINT8* out, const UINT8* in, int pixels)
  69. {
  70. int i, m, b;
  71. /* bilevel (black is 0) */
  72. b = 0; m = 128;
  73. for (i = 0; i < pixels; i++) {
  74. if (in[i] != 0)
  75. b |= m;
  76. m >>= 1;
  77. if (m == 0) {
  78. *out++ = b;
  79. b = 0; m = 128;
  80. }
  81. }
  82. if (m != 128)
  83. *out++ = b;
  84. }
  85. static void
  86. pack1I(UINT8* out, const UINT8* in, int pixels)
  87. {
  88. int i, m, b;
  89. /* bilevel (black is 1) */
  90. b = 0; m = 128;
  91. for (i = 0; i < pixels; i++) {
  92. if (in[i] == 0)
  93. b |= m;
  94. m >>= 1;
  95. if (m == 0) {
  96. *out++ = b;
  97. b = 0; m = 128;
  98. }
  99. }
  100. if (m != 128)
  101. *out++ = b;
  102. }
  103. static void
  104. pack1R(UINT8* out, const UINT8* in, int pixels)
  105. {
  106. int i, m, b;
  107. /* bilevel, lsb first (black is 0) */
  108. b = 0; m = 1;
  109. for (i = 0; i < pixels; i++) {
  110. if (in[i] != 0)
  111. b |= m;
  112. m <<= 1;
  113. if (m == 256){
  114. *out++ = b;
  115. b = 0; m = 1;
  116. }
  117. }
  118. if (m != 1)
  119. *out++ = b;
  120. }
  121. static void
  122. pack1IR(UINT8* out, const UINT8* in, int pixels)
  123. {
  124. int i, m, b;
  125. /* bilevel, lsb first (black is 1) */
  126. b = 0; m = 1;
  127. for (i = 0; i < pixels; i++) {
  128. if (in[i] == 0)
  129. b |= m;
  130. m <<= 1;
  131. if (m == 256){
  132. *out++ = b;
  133. b = 0; m = 1;
  134. }
  135. }
  136. if (m != 1)
  137. *out++ = b;
  138. }
  139. static void
  140. pack1L(UINT8* out, const UINT8* in, int pixels)
  141. {
  142. int i;
  143. /* bilevel, stored as bytes */
  144. for (i = 0; i < pixels; i++)
  145. out[i] = (in[i] != 0) ? 255 : 0;
  146. }
  147. static void
  148. packP4(UINT8* out, const UINT8* in, int pixels)
  149. {
  150. while (pixels >= 2) {
  151. *out++ = (in[0] << 4) |
  152. (in[1] & 15);
  153. in += 2; pixels -= 2;
  154. }
  155. if (pixels)
  156. out[0] = (in[0] << 4);
  157. }
  158. static void
  159. packP2(UINT8* out, const UINT8* in, int pixels)
  160. {
  161. while (pixels >= 4) {
  162. *out++ = (in[0] << 6) |
  163. ((in[1] & 3) << 4) |
  164. ((in[2] & 3) << 2) |
  165. (in[3] & 3);
  166. in += 4; pixels -= 4;
  167. }
  168. switch (pixels) {
  169. case 3:
  170. out[0] = (in[0] << 6) |
  171. ((in[1] & 3) << 4) |
  172. ((in[2] & 3) << 2);
  173. break;
  174. case 2:
  175. out[0] = (in[0] << 6) |
  176. ((in[1] & 3) << 4);
  177. break;
  178. case 1:
  179. out[0] = (in[0] << 6);
  180. }
  181. }
  182. static void
  183. packL16(UINT8* out, const UINT8* in, int pixels)
  184. {
  185. int i;
  186. /* L -> L;16, e.g: \xff77 -> \x00\xff\x00\x77 */
  187. for (i = 0; i < pixels; i++) {
  188. out[0] = 0;
  189. out[1] = in[i];
  190. out += 2;
  191. }
  192. }
  193. static void
  194. packL16B(UINT8* out, const UINT8* in, int pixels)
  195. {
  196. int i;
  197. /* L -> L;16B, e.g: \xff77 -> \xff\x00\x77\x00 */
  198. for (i = 0; i < pixels; i++) {
  199. out[0] = in[i];
  200. out[1] = 0;
  201. out += 2;
  202. }
  203. }
  204. static void
  205. packLA(UINT8* out, const UINT8* in, int pixels)
  206. {
  207. int i;
  208. /* LA, pixel interleaved */
  209. for (i = 0; i < pixels; i++) {
  210. out[0] = in[R];
  211. out[1] = in[A];
  212. out += 2; in += 4;
  213. }
  214. }
  215. static void
  216. packLAL(UINT8* out, const UINT8* in, int pixels)
  217. {
  218. int i;
  219. /* LA, line interleaved */
  220. for (i = 0; i < pixels; i++) {
  221. out[i] = in[R];
  222. out[i+pixels] = in[A];
  223. in += 4;
  224. }
  225. }
  226. void
  227. ImagingPackRGB(UINT8* out, const UINT8* in, int pixels)
  228. {
  229. int i = 0;
  230. /* RGB triplets */
  231. #ifdef __sparc
  232. /* SPARC CPUs cannot read integers from nonaligned addresses. */
  233. for (; i < pixels; i++) {
  234. out[0] = in[R];
  235. out[1] = in[G];
  236. out[2] = in[B];
  237. out += 3; in += 4;
  238. }
  239. #else
  240. for (; i < pixels-1; i++) {
  241. memcpy(out, in + i * 4, 4);
  242. out += 3;
  243. }
  244. for (; i < pixels; i++) {
  245. out[0] = in[i*4+R];
  246. out[1] = in[i*4+G];
  247. out[2] = in[i*4+B];
  248. out += 3;
  249. }
  250. #endif
  251. }
  252. void
  253. ImagingPackXRGB(UINT8* out, const UINT8* in, int pixels)
  254. {
  255. int i;
  256. /* XRGB, triplets with left padding */
  257. for (i = 0; i < pixels; i++) {
  258. out[0] = 0;
  259. out[1] = in[R];
  260. out[2] = in[G];
  261. out[3] = in[B];
  262. out += 4; in += 4;
  263. }
  264. }
  265. void
  266. ImagingPackBGR(UINT8* out, const UINT8* in, int pixels)
  267. {
  268. int i;
  269. /* RGB, reversed bytes */
  270. for (i = 0; i < pixels; i++) {
  271. out[0] = in[B];
  272. out[1] = in[G];
  273. out[2] = in[R];
  274. out += 3; in += 4;
  275. }
  276. }
  277. void
  278. ImagingPackBGRX(UINT8* out, const UINT8* in, int pixels)
  279. {
  280. int i;
  281. /* BGRX, reversed bytes with right padding */
  282. for (i = 0; i < pixels; i++) {
  283. out[0] = in[B];
  284. out[1] = in[G];
  285. out[2] = in[R];
  286. out[3] = 0;
  287. out += 4; in += 4;
  288. }
  289. }
  290. void
  291. ImagingPackXBGR(UINT8* out, const UINT8* in, int pixels)
  292. {
  293. int i;
  294. /* XBGR, reversed bytes with left padding */
  295. for (i = 0; i < pixels; i++) {
  296. out[0] = 0;
  297. out[1] = in[B];
  298. out[2] = in[G];
  299. out[3] = in[R];
  300. out += 4; in += 4;
  301. }
  302. }
  303. void
  304. ImagingPackBGRA(UINT8* out, const UINT8* in, int pixels)
  305. {
  306. int i;
  307. /* BGRX, reversed bytes with right padding */
  308. for (i = 0; i < pixels; i++) {
  309. out[0] = in[B];
  310. out[1] = in[G];
  311. out[2] = in[R];
  312. out[3] = in[A];
  313. out += 4; in += 4;
  314. }
  315. }
  316. void
  317. ImagingPackABGR(UINT8* out, const UINT8* in, int pixels)
  318. {
  319. int i;
  320. /* XBGR, reversed bytes with left padding */
  321. for (i = 0; i < pixels; i++) {
  322. out[0] = in[A];
  323. out[1] = in[B];
  324. out[2] = in[G];
  325. out[3] = in[R];
  326. out += 4; in += 4;
  327. }
  328. }
  329. void
  330. ImagingPackBGRa(UINT8* out, const UINT8* in, int pixels)
  331. {
  332. int i;
  333. /* BGRa, reversed bytes with premultiplied alpha */
  334. for (i = 0; i < pixels; i++) {
  335. int alpha = out[3] = in[A];
  336. int tmp;
  337. out[0] = MULDIV255(in[B], alpha, tmp);
  338. out[1] = MULDIV255(in[G], alpha, tmp);
  339. out[2] = MULDIV255(in[R], alpha, tmp);
  340. out += 4; in += 4;
  341. }
  342. }
  343. static void
  344. packRGBL(UINT8* out, const UINT8* in, int pixels)
  345. {
  346. int i;
  347. /* RGB, line interleaved */
  348. for (i = 0; i < pixels; i++) {
  349. out[i] = in[R];
  350. out[i+pixels] = in[G];
  351. out[i+pixels+pixels] = in[B];
  352. in += 4;
  353. }
  354. }
  355. static void
  356. packRGBXL(UINT8* out, const UINT8* in, int pixels)
  357. {
  358. int i;
  359. /* RGBX, line interleaved */
  360. for (i = 0; i < pixels; i++) {
  361. out[i] = in[R];
  362. out[i+pixels] = in[G];
  363. out[i+pixels+pixels] = in[B];
  364. out[i+pixels+pixels+pixels] = in[X];
  365. in += 4;
  366. }
  367. }
  368. static void
  369. packI16B(UINT8* out, const UINT8* in_, int pixels)
  370. {
  371. int i;
  372. UINT16 tmp_;
  373. UINT8* tmp = (UINT8*) &tmp_;
  374. for (i = 0; i < pixels; i++) {
  375. INT32 in;
  376. memcpy(&in, in_, sizeof(in));
  377. if (in <= 0)
  378. tmp_ = 0;
  379. else if (in > 65535)
  380. tmp_ = 65535;
  381. else
  382. tmp_ = in;
  383. C16B;
  384. out += 2; in_ += sizeof(in);
  385. }
  386. }
  387. static void
  388. packI16N_I16B(UINT8* out, const UINT8* in, int pixels){
  389. int i;
  390. UINT8* tmp = (UINT8*) in;
  391. for (i = 0; i < pixels; i++) {
  392. C16B;
  393. out += 2; tmp += 2;
  394. }
  395. }
  396. static void
  397. packI16N_I16(UINT8* out, const UINT8* in, int pixels){
  398. int i;
  399. UINT8* tmp = (UINT8*) in;
  400. for (i = 0; i < pixels; i++) {
  401. C16L;
  402. out += 2; tmp += 2;
  403. }
  404. }
  405. static void
  406. packI32S(UINT8* out, const UINT8* in, int pixels)
  407. {
  408. int i;
  409. UINT8* tmp = (UINT8*) in;
  410. for (i = 0; i < pixels; i++) {
  411. C32L;
  412. out += 4; tmp += 4;
  413. }
  414. }
  415. void
  416. ImagingPackLAB(UINT8* out, const UINT8* in, int pixels)
  417. {
  418. int i;
  419. /* LAB triplets */
  420. for (i = 0; i < pixels; i++) {
  421. out[0] = in[0];
  422. out[1] = in[1] ^ 128; /* signed in outside world */
  423. out[2] = in[2] ^ 128;
  424. out += 3; in += 4;
  425. }
  426. }
  427. static void
  428. copy1(UINT8* out, const UINT8* in, int pixels)
  429. {
  430. /* L, P */
  431. memcpy(out, in, pixels);
  432. }
  433. static void
  434. copy2(UINT8* out, const UINT8* in, int pixels)
  435. {
  436. /* I;16, etc */
  437. memcpy(out, in, pixels*2);
  438. }
  439. static void
  440. copy3(UINT8* out, const UINT8* in, int pixels)
  441. {
  442. /* BGR;24, etc */
  443. memcpy(out, in, pixels*3);
  444. }
  445. static void
  446. copy4(UINT8* out, const UINT8* in, int pixels)
  447. {
  448. /* RGBA, CMYK quadruples */
  449. memcpy(out, in, 4*pixels);
  450. }
  451. static void
  452. copy4I(UINT8* out, const UINT8* in, int pixels)
  453. {
  454. /* RGBA, CMYK quadruples, inverted */
  455. int i;
  456. for (i = 0; i < pixels*4; i++)
  457. out[i] = ~in[i];
  458. }
  459. static void
  460. band0(UINT8* out, const UINT8* in, int pixels)
  461. {
  462. int i;
  463. for (i = 0; i < pixels; i++, in += 4)
  464. out[i] = in[0];
  465. }
  466. static void
  467. band1(UINT8* out, const UINT8* in, int pixels)
  468. {
  469. int i;
  470. for (i = 0; i < pixels; i++, in += 4)
  471. out[i] = in[1];
  472. }
  473. static void
  474. band2(UINT8* out, const UINT8* in, int pixels)
  475. {
  476. int i;
  477. for (i = 0; i < pixels; i++, in += 4)
  478. out[i] = in[2];
  479. }
  480. static void
  481. band3(UINT8* out, const UINT8* in, int pixels)
  482. {
  483. int i;
  484. for (i = 0; i < pixels; i++, in += 4)
  485. out[i] = in[3];
  486. }
  487. static struct {
  488. const char* mode;
  489. const char* rawmode;
  490. int bits;
  491. ImagingShuffler pack;
  492. } packers[] = {
  493. /* bilevel */
  494. {"1", "1", 1, pack1},
  495. {"1", "1;I", 1, pack1I},
  496. {"1", "1;R", 1, pack1R},
  497. {"1", "1;IR", 1, pack1IR},
  498. {"1", "L", 8, pack1L},
  499. /* greyscale */
  500. {"L", "L", 8, copy1},
  501. {"L", "L;16", 16, packL16},
  502. {"L", "L;16B", 16, packL16B},
  503. /* greyscale w. alpha */
  504. {"LA", "LA", 16, packLA},
  505. {"LA", "LA;L", 16, packLAL},
  506. /* palette */
  507. {"P", "P;1", 1, pack1},
  508. {"P", "P;2", 2, packP2},
  509. {"P", "P;4", 4, packP4},
  510. {"P", "P", 8, copy1},
  511. /* palette w. alpha */
  512. {"PA", "PA", 16, packLA},
  513. {"PA", "PA;L", 16, packLAL},
  514. /* true colour */
  515. {"RGB", "RGB", 24, ImagingPackRGB},
  516. {"RGB", "RGBX", 32, copy4},
  517. {"RGB", "XRGB", 32, ImagingPackXRGB},
  518. {"RGB", "BGR", 24, ImagingPackBGR},
  519. {"RGB", "BGRX", 32, ImagingPackBGRX},
  520. {"RGB", "XBGR", 32, ImagingPackXBGR},
  521. {"RGB", "RGB;L", 24, packRGBL},
  522. {"RGB", "R", 8, band0},
  523. {"RGB", "G", 8, band1},
  524. {"RGB", "B", 8, band2},
  525. /* true colour w. alpha */
  526. {"RGBA", "RGBA", 32, copy4},
  527. {"RGBA", "RGBA;L", 32, packRGBXL},
  528. {"RGBA", "RGB", 24, ImagingPackRGB},
  529. {"RGBA", "BGR", 24, ImagingPackBGR},
  530. {"RGBA", "BGRA", 32, ImagingPackBGRA},
  531. {"RGBA", "ABGR", 32, ImagingPackABGR},
  532. {"RGBA", "BGRa", 32, ImagingPackBGRa},
  533. {"RGBA", "R", 8, band0},
  534. {"RGBA", "G", 8, band1},
  535. {"RGBA", "B", 8, band2},
  536. {"RGBA", "A", 8, band3},
  537. /* true colour w. alpha premultiplied */
  538. {"RGBa", "RGBa", 32, copy4},
  539. {"RGBa", "BGRa", 32, ImagingPackBGRA},
  540. {"RGBa", "aBGR", 32, ImagingPackABGR},
  541. /* true colour w. padding */
  542. {"RGBX", "RGBX", 32, copy4},
  543. {"RGBX", "RGBX;L", 32, packRGBXL},
  544. {"RGBX", "RGB", 24, ImagingPackRGB},
  545. {"RGBX", "BGR", 24, ImagingPackBGR},
  546. {"RGBX", "BGRX", 32, ImagingPackBGRX},
  547. {"RGBX", "XBGR", 32, ImagingPackXBGR},
  548. {"RGBX", "R", 8, band0},
  549. {"RGBX", "G", 8, band1},
  550. {"RGBX", "B", 8, band2},
  551. {"RGBX", "X", 8, band3},
  552. /* colour separation */
  553. {"CMYK", "CMYK", 32, copy4},
  554. {"CMYK", "CMYK;I", 32, copy4I},
  555. {"CMYK", "CMYK;L", 32, packRGBXL},
  556. {"CMYK", "C", 8, band0},
  557. {"CMYK", "M", 8, band1},
  558. {"CMYK", "Y", 8, band2},
  559. {"CMYK", "K", 8, band3},
  560. /* video (YCbCr) */
  561. {"YCbCr", "YCbCr", 24, ImagingPackRGB},
  562. {"YCbCr", "YCbCr;L", 24, packRGBL},
  563. {"YCbCr", "YCbCrX", 32, copy4},
  564. {"YCbCr", "YCbCrK", 32, copy4},
  565. {"YCbCr", "Y", 8, band0},
  566. {"YCbCr", "Cb", 8, band1},
  567. {"YCbCr", "Cr", 8, band2},
  568. /* LAB Color */
  569. {"LAB", "LAB", 24, ImagingPackLAB},
  570. {"LAB", "L", 8, band0},
  571. {"LAB", "A", 8, band1},
  572. {"LAB", "B", 8, band2},
  573. /* HSV */
  574. {"HSV", "HSV", 24, ImagingPackRGB},
  575. {"HSV", "H", 8, band0},
  576. {"HSV", "S", 8, band1},
  577. {"HSV", "V", 8, band2},
  578. /* integer */
  579. {"I", "I", 32, copy4},
  580. {"I", "I;16B", 16, packI16B},
  581. {"I", "I;32S", 32, packI32S},
  582. {"I", "I;32NS", 32, copy4},
  583. /* floating point */
  584. {"F", "F", 32, copy4},
  585. {"F", "F;32F", 32, packI32S},
  586. {"F", "F;32NF", 32, copy4},
  587. /* storage modes */
  588. {"I;16", "I;16", 16, copy2},
  589. {"I;16", "I;16B", 16, packI16N_I16B},
  590. {"I;16B", "I;16B", 16, copy2},
  591. {"I;16L", "I;16L", 16, copy2},
  592. {"I;16", "I;16N", 16, packI16N_I16}, // LibTiff native->image endian.
  593. {"I;16L", "I;16N", 16, packI16N_I16},
  594. {"I;16B", "I;16N", 16, packI16N_I16B},
  595. {"BGR;15", "BGR;15", 16, copy2},
  596. {"BGR;16", "BGR;16", 16, copy2},
  597. {"BGR;24", "BGR;24", 24, copy3},
  598. {NULL} /* sentinel */
  599. };
  600. ImagingShuffler
  601. ImagingFindPacker(const char* mode, const char* rawmode, int* bits_out)
  602. {
  603. int i;
  604. /* find a suitable pixel packer */
  605. for (i = 0; packers[i].rawmode; i++)
  606. if (strcmp(packers[i].mode, mode) == 0 &&
  607. strcmp(packers[i].rawmode, rawmode) == 0) {
  608. if (bits_out)
  609. *bits_out = packers[i].bits;
  610. return packers[i].pack;
  611. }
  612. return NULL;
  613. }