GIFDECOD.CPP
上传用户:wep9318
上传日期:2007-01-07
资源大小:893k
文件大小:10k
源码类别:

图片显示

开发平台:

Visual C++

  1. /* DECODE.C - An LZW decoder for GIF
  2.  * Copyright (C) 1987, by Steven A. Bennett
  3.  * Copyright (C) 1994, C++ version by Alejandro Aguilar Sierra
  4. *
  5.  * Permission is given by the author to freely redistribute and include
  6.  * this code in any program as long as this credit is given where due.
  7.  *
  8.  * In accordance with the above, I want to credit Steve Wilhite who wrote
  9.  * the code which this is heavily inspired by...
  10.  *
  11.  * GIF and 'Graphics Interchange Format' are trademarks (tm) of
  12.  * Compuserve, Incorporated, an H&R Block Company.
  13.  *
  14.  * Release Notes: This file contains a decoder routine for GIF images
  15.  * which is similar, structurally, to the original routine by Steve Wilhite.
  16.  * It is, however, somewhat noticably faster in most cases.
  17.  *
  18.  */
  19. #include "stdafx.h"
  20. #include "gifdecod.h"
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26. static LONG code_mask[13]=
  27. {
  28.   0,
  29.   0x0001, 0x0003,
  30.   0x0007, 0x000F,
  31.   0x001F, 0x003F,
  32.   0x007F, 0x00FF,
  33.   0x01FF, 0x03FF,
  34.   0x07FF, 0x0FFF
  35.   };
  36. /* This function initializes the decoder for reading a new image.
  37.  */
  38. SHORT GIFDecoder::init_exp(SHORT size)
  39. {
  40. curr_size = size + 1;
  41. top_slot = 1 << curr_size;
  42. clear = 1 << size;
  43. ending = clear + 1;
  44. slot = newcodes = ending + 1;
  45. navail_bytes = nbits_left = 0;
  46. return(0);
  47. }
  48. /* get_next_code()
  49.  * - gets the next code from the GIF file.  Returns the code, or else
  50.  * a negative number in case of file errors...
  51.  */
  52. SHORT GIFDecoder::get_next_code()
  53. {
  54. SHORT i, x;
  55. ULONG ret;
  56. if (nbits_left == 0)
  57. {
  58. if (navail_bytes <= 0)
  59. {
  60. /* Out of bytes in current block, so read next block
  61.  */
  62. pbytes = byte_buff;
  63. if ((navail_bytes = get_byte()) < 0)
  64. return(navail_bytes);
  65. else if (navail_bytes)
  66.  {
  67. for (i = 0; i < navail_bytes; ++i)
  68. {
  69. if ((x = get_byte()) < 0)
  70. return(x);
  71. byte_buff[i] = x;
  72. }
  73. }
  74. }
  75. b1 = *pbytes++;
  76. nbits_left = 8;
  77. --navail_bytes;
  78. }
  79. ret = b1 >> (8 - nbits_left);
  80. while (curr_size > nbits_left)
  81. {
  82. if (navail_bytes <= 0)
  83. {
  84. /* Out of bytes in current block, so read next block
  85.  */
  86. pbytes = byte_buff;
  87. if ((navail_bytes = get_byte()) < 0)
  88. return(navail_bytes);
  89. else if (navail_bytes)
  90. {
  91. for (i = 0; i < navail_bytes; ++i)
  92. {
  93. if ((x = get_byte()) < 0)
  94. return(x);
  95. byte_buff[i] = x;
  96. }
  97. }
  98. }
  99. b1 = *pbytes++;
  100. ret |= b1 << nbits_left;
  101. nbits_left += 8;
  102. --navail_bytes;
  103. }
  104. nbits_left -= curr_size;
  105. ret &= code_mask[curr_size];
  106. return((SHORT)(ret));
  107. }
  108. /* The reason we have these seperated like this instead of using
  109.  * a structure like the original Wilhite code did, is because this
  110.  * stuff generally produces significantly faster code when compiled...
  111.  * This code is full of similar speedups...  (For a good book on writing
  112.  * C for speed or for space optomisation, see Efficient C by Tom Plum,
  113.  * published by Plum-Hall Associates...)
  114.  */
  115. LOCAL byte stack[MAX_CODES + 1];            /* Stack for storing pixels */
  116. LOCAL byte suffix[MAX_CODES + 1];           /* Suffix table */
  117. LOCAL USHORT prefix[MAX_CODES + 1];           /* Prefix linked list */
  118. /* SHORT decoder(linewidth)
  119.  *    SHORT linewidth;               * Pixels per line of image *
  120.  *
  121.  * - This function decodes an LZW image, according to the method used
  122.  * in the GIF spec.  Every *linewidth* "characters" (ie. pixels) decoded
  123.  * will generate a call to out_line(), which is a user specific function
  124.  * to display a line of pixels.  The function gets it's codes from
  125.  * get_next_code() which is responsible for reading blocks of data and
  126.  * seperating them into the proper size codes.  Finally, get_byte() is
  127.  * the global routine to read the next byte from the GIF file.
  128.  *
  129.  * It is generally a good idea to have linewidth correspond to the actual
  130.  * width of a line (as specified in the Image header) to make your own
  131.  * code a bit simpler, but it isn't absolutely necessary.
  132.  *
  133.  * Returns: 0 if successful, else negative.  (See ERRS.H)
  134.  *
  135.  */
  136. SHORT GIFDecoder::decoder(SHORT linewidth, INT&  bad_code_count)
  137. {
  138. FAST byte *sp, *bufptr;
  139. byte *buf;
  140. FAST SHORT code, fc, oc, bufcnt;
  141. SHORT c, size, ret;
  142. /* Initialize for decoding a new image...
  143.  */
  144. bad_code_count = 0;
  145. if ((size = get_byte()) < 0)
  146. return(size);
  147. if (size < 2 || 9 < size)
  148. return(BAD_CODE_SIZE);
  149. /*   out_line = outline;*/
  150. init_exp(size);
  151. //  printf("L %d %xn",linewidth,size);
  152. /* Initialize in case they forgot to put in a clear code.
  153.  * (This shouldn't happen, but we'll try and decode it anyway...)
  154.  */
  155. oc = fc = 0;
  156.    /* Allocate space for the decode buffer
  157.     */
  158. if ((buf = new byte[linewidth + 1]) == NULL)
  159.       return(OUT_OF_MEMORY);
  160.    /* Set up the stack pointer and decode buffer pointer
  161.     */
  162.    sp = stack;
  163.    bufptr = buf;
  164.    bufcnt = linewidth;
  165.    /* This is the main loop.  For each code we get we pass through the
  166.     * linked list of prefix codes, pushing the corresponding "character" for
  167.  * each code onto the stack.  When the list reaches a single "character"
  168.  * we push that on the stack too, and then start unstacking each
  169.     * character for output in the correct order.  Special handling is
  170.  * included for the clear code, and the whole thing ends when we get
  171.     * an ending code.
  172.     */
  173.    while ((c = get_next_code()) != ending)
  174.       {
  175.       /* If we had a file error, return without completing the decode
  176.        */
  177.       if (c < 0)
  178.          {
  179.  delete[] buf;
  180. return(0);
  181.          }
  182.       /* If the code is a clear code, reinitialize all necessary items.
  183.  */
  184.       if (c == clear)
  185.          {
  186. curr_size = size + 1;
  187.          slot = newcodes;
  188.          top_slot = 1 << curr_size;
  189.          /* Continue reading codes until we get a non-clear code
  190.           * (Another unlikely, but possible case...)
  191.           */
  192.          while ((c = get_next_code()) == clear)
  193.             ;
  194. /* If we get an ending code immediately after a clear code
  195.           * (Yet another unlikely case), then break out of the loop.
  196.           */
  197.          if (c == ending)
  198. break;
  199.          /* Finally, if the code is beyond the range of already set codes,
  200.           * (This one had better NOT happen...  I have no idea what will
  201.  * result from this, but I doubt it will look good...) then set it
  202.           * to color zero.
  203.           */
  204.          if (c >= slot)
  205.             c = 0;
  206.          oc = fc = c;
  207.          /* And let us not forget to put the char into the buffer... And
  208.  * if, on the off chance, we were exactly one pixel from the end
  209.           * of the line, we have to send the buffer to the out_line()
  210.           * routine...
  211.           */
  212. *bufptr++ = c;
  213.          if (--bufcnt == 0)
  214. {
  215.             if ((ret = out_line(buf, linewidth)) < 0)
  216.                {
  217.  delete[] buf;
  218.                return(ret);
  219.                }
  220.             bufptr = buf;
  221.             bufcnt = linewidth;
  222.             }
  223.          }
  224.       else
  225. {
  226.          /* In this case, it's not a clear code or an ending code, so
  227.           * it must be a code code...  So we can now decode the code into
  228.  * a stack of character codes. (Clear as mud, right?)
  229.           */
  230.          code = c;
  231.          /* Here we go again with one of those off chances...  If, on the
  232.           * off chance, the code we got is beyond the range of those already
  233.  * set up (Another thing which had better NOT happen...) we trick
  234.           * the decoder into thinking it actually got the last code read.
  235.           * (Hmmn... I'm not sure why this works...  But it does...)
  236.           */
  237.          if (code >= slot)
  238.             {
  239.             if (code > slot)
  240. ++bad_code_count;
  241.             code = oc;
  242.             *sp++ = fc;
  243.             }
  244. /* Here we scan back along the linked list of prefixes, pushing
  245.           * helpless characters (ie. suffixes) onto the stack as we do so.
  246.           */
  247. while (code >= newcodes)
  248.             {
  249.             *sp++ = suffix[code];
  250. code = prefix[code];
  251.             }
  252.          /* Push the last character on the stack, and set up the new
  253.           * prefix and suffix, and if the required slot number is greater
  254.           * than that allowed by the current bit size, increase the bit
  255.           * size.  (NOTE - If we are all full, we *don't* save the new
  256.           * suffix and prefix...  I'm not certain if this is correct...
  257.           * it might be more proper to overwrite the last code...
  258.           */
  259.          *sp++ = code;
  260.          if (slot < top_slot)
  261.             {
  262.             suffix[slot] = fc = code;
  263.             prefix[slot++] = oc;
  264. oc = c;
  265.             }
  266.          if (slot >= top_slot)
  267. if (curr_size < 12)
  268.                {
  269.                top_slot <<= 1;
  270.                ++curr_size;
  271.                } 
  272.          /* Now that we've pushed the decoded string (in reverse order)
  273.           * onto the stack, lets pop it off and put it into our decode
  274.           * buffer...  And when the decode buffer is full, write another
  275.           * line...
  276.           */
  277.          while (sp > stack)
  278.             {
  279.             *bufptr++ = *(--sp);
  280.             if (--bufcnt == 0)
  281. {
  282.                if ((ret = out_line(buf, linewidth)) < 0)
  283.                   {
  284.   delete[] buf;
  285.                   return(ret);
  286.                   }
  287.                bufptr = buf;
  288.                bufcnt = linewidth;
  289.                }
  290.             }
  291.          }
  292.       }
  293.    ret = 0;
  294.    if (bufcnt != linewidth)
  295.       ret = out_line(buf, (linewidth - bufcnt));
  296.    delete[] buf;
  297.    return(ret);
  298.    }