Point.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * point (pixel) translation
  6. *
  7. * history:
  8. * 1995-11-27 fl Created
  9. * 1996-03-31 fl Fixed colour support
  10. * 1996-08-13 fl Support 8-bit to "1" thresholding
  11. * 1997-05-31 fl Added floating point transform
  12. * 1998-07-02 fl Added integer point transform
  13. * 1998-07-17 fl Support L to anything lookup
  14. * 2004-12-18 fl Refactored; added I to L lookup
  15. *
  16. * Copyright (c) 1997-2004 by Secret Labs AB.
  17. * Copyright (c) 1995-2004 by Fredrik Lundh.
  18. *
  19. * See the README file for information on usage and redistribution.
  20. */
  21. #include "Imaging.h"
  22. typedef struct {
  23. const void* table;
  24. } im_point_context;
  25. static void
  26. im_point_8_8(Imaging imOut, Imaging imIn, im_point_context* context)
  27. {
  28. int x, y;
  29. /* 8-bit source, 8-bit destination */
  30. UINT8* table = (UINT8*) context->table;
  31. for (y = 0; y < imIn->ysize; y++) {
  32. UINT8* in = imIn->image8[y];
  33. UINT8* out = imOut->image8[y];
  34. for (x = 0; x < imIn->xsize; x++)
  35. out[x] = table[in[x]];
  36. }
  37. }
  38. static void
  39. im_point_2x8_2x8(Imaging imOut, Imaging imIn, im_point_context* context)
  40. {
  41. int x, y;
  42. /* 2x8-bit source, 2x8-bit destination */
  43. UINT8* table = (UINT8*) context->table;
  44. for (y = 0; y < imIn->ysize; y++) {
  45. UINT8* in = (UINT8*) imIn->image[y];
  46. UINT8* out = (UINT8*) imOut->image[y];
  47. for (x = 0; x < imIn->xsize; x++) {
  48. out[0] = table[in[0]];
  49. out[3] = table[in[3]+256];
  50. in += 4; out += 4;
  51. }
  52. }
  53. }
  54. static void
  55. im_point_3x8_3x8(Imaging imOut, Imaging imIn, im_point_context* context)
  56. {
  57. int x, y;
  58. /* 3x8-bit source, 3x8-bit destination */
  59. UINT8* table = (UINT8*) context->table;
  60. for (y = 0; y < imIn->ysize; y++) {
  61. UINT8* in = (UINT8*) imIn->image[y];
  62. UINT8* out = (UINT8*) imOut->image[y];
  63. for (x = 0; x < imIn->xsize; x++) {
  64. out[0] = table[in[0]];
  65. out[1] = table[in[1]+256];
  66. out[2] = table[in[2]+512];
  67. in += 4; out += 4;
  68. }
  69. }
  70. }
  71. static void
  72. im_point_4x8_4x8(Imaging imOut, Imaging imIn, im_point_context* context)
  73. {
  74. int x, y;
  75. /* 4x8-bit source, 4x8-bit destination */
  76. UINT8* table = (UINT8*) context->table;
  77. for (y = 0; y < imIn->ysize; y++) {
  78. UINT8* in = (UINT8*) imIn->image[y];
  79. UINT8* out = (UINT8*) imOut->image[y];
  80. for (x = 0; x < imIn->xsize; x++) {
  81. out[0] = table[in[0]];
  82. out[1] = table[in[1]+256];
  83. out[2] = table[in[2]+512];
  84. out[3] = table[in[3]+768];
  85. in += 4; out += 4;
  86. }
  87. }
  88. }
  89. static void
  90. im_point_8_32(Imaging imOut, Imaging imIn, im_point_context* context)
  91. {
  92. int x, y;
  93. /* 8-bit source, 32-bit destination */
  94. char* table = (char*) context->table;
  95. for (y = 0; y < imIn->ysize; y++) {
  96. UINT8* in = imIn->image8[y];
  97. INT32* out = imOut->image32[y];
  98. for (x = 0; x < imIn->xsize; x++)
  99. memcpy(out + x, table + in[x] * sizeof(INT32), sizeof(INT32));
  100. }
  101. }
  102. static void
  103. im_point_32_8(Imaging imOut, Imaging imIn, im_point_context* context)
  104. {
  105. int x, y;
  106. /* 32-bit source, 8-bit destination */
  107. UINT8* table = (UINT8*) context->table;
  108. for (y = 0; y < imIn->ysize; y++) {
  109. INT32* in = imIn->image32[y];
  110. UINT8* out = imOut->image8[y];
  111. for (x = 0; x < imIn->xsize; x++) {
  112. int v = in[x];
  113. if (v < 0)
  114. v = 0;
  115. else if (v > 65535)
  116. v = 65535;
  117. out[x] = table[v];
  118. }
  119. }
  120. }
  121. Imaging
  122. ImagingPoint(Imaging imIn, const char* mode, const void* table)
  123. {
  124. /* lookup table transform */
  125. ImagingSectionCookie cookie;
  126. Imaging imOut;
  127. im_point_context context;
  128. void (*point)(Imaging imIn, Imaging imOut, im_point_context* context);
  129. if (!imIn)
  130. return (Imaging) ImagingError_ModeError();
  131. if (!mode)
  132. mode = imIn->mode;
  133. if (imIn->type != IMAGING_TYPE_UINT8) {
  134. if (imIn->type != IMAGING_TYPE_INT32 || strcmp(mode, "L") != 0)
  135. goto mode_mismatch;
  136. } else if (!imIn->image8 && strcmp(imIn->mode, mode) != 0)
  137. goto mode_mismatch;
  138. imOut = ImagingNew(mode, imIn->xsize, imIn->ysize);
  139. if (!imOut)
  140. return NULL;
  141. /* find appropriate handler */
  142. if (imIn->type == IMAGING_TYPE_UINT8) {
  143. if (imIn->bands == imOut->bands && imIn->type == imOut->type) {
  144. switch (imIn->bands) {
  145. case 1:
  146. point = im_point_8_8;
  147. break;
  148. case 2:
  149. point = im_point_2x8_2x8;
  150. break;
  151. case 3:
  152. point = im_point_3x8_3x8;
  153. break;
  154. case 4:
  155. point = im_point_4x8_4x8;
  156. break;
  157. default:
  158. /* this cannot really happen */
  159. point = im_point_8_8;
  160. break;
  161. }
  162. } else
  163. point = im_point_8_32;
  164. } else
  165. point = im_point_32_8;
  166. ImagingCopyPalette(imOut, imIn);
  167. ImagingSectionEnter(&cookie);
  168. context.table = table;
  169. point(imOut, imIn, &context);
  170. ImagingSectionLeave(&cookie);
  171. return imOut;
  172. mode_mismatch:
  173. return (Imaging) ImagingError_ValueError(
  174. "point operation not supported for this mode"
  175. );
  176. }
  177. Imaging
  178. ImagingPointTransform(Imaging imIn, double scale, double offset)
  179. {
  180. /* scale/offset transform */
  181. ImagingSectionCookie cookie;
  182. Imaging imOut;
  183. int x, y;
  184. if (!imIn || (strcmp(imIn->mode, "I") != 0 &&
  185. strcmp(imIn->mode, "I;16") != 0 &&
  186. strcmp(imIn->mode, "F") != 0))
  187. return (Imaging) ImagingError_ModeError();
  188. imOut = ImagingNew(imIn->mode, imIn->xsize, imIn->ysize);
  189. if (!imOut)
  190. return NULL;
  191. switch (imIn->type) {
  192. case IMAGING_TYPE_INT32:
  193. ImagingSectionEnter(&cookie);
  194. for (y = 0; y < imIn->ysize; y++) {
  195. INT32* in = imIn->image32[y];
  196. INT32* out = imOut->image32[y];
  197. /* FIXME: add clipping? */
  198. for (x = 0; x < imIn->xsize; x++)
  199. out[x] = in[x] * scale + offset;
  200. }
  201. ImagingSectionLeave(&cookie);
  202. break;
  203. case IMAGING_TYPE_FLOAT32:
  204. ImagingSectionEnter(&cookie);
  205. for (y = 0; y < imIn->ysize; y++) {
  206. FLOAT32* in = (FLOAT32*) imIn->image32[y];
  207. FLOAT32* out = (FLOAT32*) imOut->image32[y];
  208. for (x = 0; x < imIn->xsize; x++)
  209. out[x] = in[x] * scale + offset;
  210. }
  211. ImagingSectionLeave(&cookie);
  212. break;
  213. case IMAGING_TYPE_SPECIAL:
  214. if (strcmp(imIn->mode,"I;16") == 0) {
  215. ImagingSectionEnter(&cookie);
  216. for (y = 0; y < imIn->ysize; y++) {
  217. char* in = (char*)imIn->image[y];
  218. char* out = (char*)imOut->image[y];
  219. /* FIXME: add clipping? */
  220. for (x = 0; x < imIn->xsize; x++) {
  221. UINT16 v;
  222. memcpy(&v, in + x * sizeof(v), sizeof(v));
  223. v = v * scale + offset;
  224. memcpy(out + x * sizeof(UINT16), &v, sizeof(v));
  225. }
  226. }
  227. ImagingSectionLeave(&cookie);
  228. break;
  229. }
  230. /* FALL THROUGH */
  231. default:
  232. ImagingDelete(imOut);
  233. return (Imaging) ImagingError_ValueError("internal error");
  234. }
  235. return imOut;
  236. }