tif_next.c 4.1 KB

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