tif_next.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. #include "tiffiop.h"
  25. #ifdef NEXT_SUPPORT
  26. /*
  27. * TIFF Library.
  28. *
  29. * NeXT 2-bit Grey Scale Compression Algorithm Support
  30. */
  31. #define SETPIXEL(op, v) { \
  32. switch (npixels++ & 3) { \
  33. case 0: op[0] = (unsigned char) ((v) << 6); break; \
  34. case 1: op[0] |= (v) << 4; break; \
  35. case 2: op[0] |= (v) << 2; break; \
  36. case 3: *op++ |= (v); op_offset++; break; \
  37. } \
  38. }
  39. #define LITERALROW 0x00
  40. #define LITERALSPAN 0x40
  41. #define WHITE ((1<<2)-1)
  42. static int
  43. NeXTDecode(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
  44. {
  45. static const char module[] = "NeXTDecode";
  46. unsigned char *bp, *op;
  47. tmsize_t cc;
  48. uint8* row;
  49. tmsize_t scanline, n;
  50. (void) s;
  51. /*
  52. * Each scanline is assumed to start off as all
  53. * white (we assume a PhotometricInterpretation
  54. * of ``min-is-black'').
  55. */
  56. for (op = (unsigned char*) buf, cc = occ; cc-- > 0;)
  57. *op++ = 0xff;
  58. bp = (unsigned char *)tif->tif_rawcp;
  59. cc = tif->tif_rawcc;
  60. scanline = tif->tif_scanlinesize;
  61. if (occ % scanline)
  62. {
  63. TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
  64. return (0);
  65. }
  66. for (row = buf; cc > 0 && occ > 0; occ -= scanline, row += scanline) {
  67. n = *bp++;
  68. cc--;
  69. switch (n) {
  70. case LITERALROW:
  71. /*
  72. * The entire scanline is given as literal values.
  73. */
  74. if (cc < scanline)
  75. goto bad;
  76. _TIFFmemcpy(row, bp, scanline);
  77. bp += scanline;
  78. cc -= scanline;
  79. break;
  80. case LITERALSPAN: {
  81. tmsize_t off;
  82. /*
  83. * The scanline has a literal span that begins at some
  84. * offset.
  85. */
  86. if( cc < 4 )
  87. goto bad;
  88. off = (bp[0] * 256) + bp[1];
  89. n = (bp[2] * 256) + bp[3];
  90. if (cc < 4+n || off+n > scanline)
  91. goto bad;
  92. _TIFFmemcpy(row+off, bp+4, n);
  93. bp += 4+n;
  94. cc -= 4+n;
  95. break;
  96. }
  97. default: {
  98. uint32 npixels = 0, grey;
  99. tmsize_t op_offset = 0;
  100. uint32 imagewidth = tif->tif_dir.td_imagewidth;
  101. if( isTiled(tif) )
  102. imagewidth = tif->tif_dir.td_tilewidth;
  103. /*
  104. * The scanline is composed of a sequence of constant
  105. * color ``runs''. We shift into ``run mode'' and
  106. * interpret bytes as codes of the form
  107. * <color><npixels> until we've filled the scanline.
  108. */
  109. op = row;
  110. for (;;) {
  111. grey = (uint32)((n>>6) & 0x3);
  112. n &= 0x3f;
  113. /*
  114. * Ensure the run does not exceed the scanline
  115. * bounds, potentially resulting in a security
  116. * issue.
  117. */
  118. while (n-- > 0 && npixels < imagewidth && op_offset < scanline)
  119. SETPIXEL(op, grey);
  120. if (npixels >= imagewidth)
  121. break;
  122. if (op_offset >= scanline ) {
  123. TIFFErrorExt(tif->tif_clientdata, module, "Invalid data for scanline %ld",
  124. (long) tif->tif_row);
  125. return (0);
  126. }
  127. if (cc == 0)
  128. goto bad;
  129. n = *bp++;
  130. cc--;
  131. }
  132. break;
  133. }
  134. }
  135. }
  136. tif->tif_rawcp = (uint8*) bp;
  137. tif->tif_rawcc = cc;
  138. return (1);
  139. bad:
  140. TIFFErrorExt(tif->tif_clientdata, module, "Not enough data for scanline %ld",
  141. (long) tif->tif_row);
  142. return (0);
  143. }
  144. static int
  145. NeXTPreDecode(TIFF* tif, uint16 s)
  146. {
  147. static const char module[] = "NeXTPreDecode";
  148. TIFFDirectory *td = &tif->tif_dir;
  149. (void)s;
  150. if( td->td_bitspersample != 2 )
  151. {
  152. TIFFErrorExt(tif->tif_clientdata, module, "Unsupported BitsPerSample = %d",
  153. td->td_bitspersample);
  154. return (0);
  155. }
  156. return (1);
  157. }
  158. int
  159. TIFFInitNeXT(TIFF* tif, int scheme)
  160. {
  161. (void) scheme;
  162. tif->tif_predecode = NeXTPreDecode;
  163. tif->tif_decoderow = NeXTDecode;
  164. tif->tif_decodestrip = NeXTDecode;
  165. tif->tif_decodetile = NeXTDecode;
  166. return (1);
  167. }
  168. #endif /* NEXT_SUPPORT */
  169. /* vim: set ts=8 sts=8 sw=8 noet: */
  170. /*
  171. * Local Variables:
  172. * mode: c
  173. * c-basic-offset: 8
  174. * fill-column: 78
  175. * End:
  176. */