summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/image-decoders/gif/gif_image_reader.cc
blob: 908a76fc7ba4f669f81d53c6523d40b0f2350bad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is mozilla.org code.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1998
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Chris Saari <saari@netscape.com>
 *   Apple Computer
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

/*
The Graphics Interchange Format(c) is the copyright property of CompuServe
Incorporated. Only CompuServe Incorporated is authorized to define, redefine,
enhance, alter, modify or change in any way the definition of the format.

CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
license for the use of the Graphics Interchange Format(sm) in computer
software; computer software utilizing GIF(sm) must acknowledge ownership of the
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
User and Technical Documentation. Computer software utilizing GIF, which is
distributed or may be distributed without User or Technical Documentation must
display to the screen or printer a message acknowledging ownership of the
Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
this case, the acknowledgement may be displayed in an opening screen or leading
banner, or a closing screen or trailing banner. A message such as the following
may be used:

    "The Graphics Interchange Format(c) is the Copyright property of
    CompuServe Incorporated. GIF(sm) is a Service Mark property of
    CompuServe Incorporated."

For further information, please contact :

    CompuServe Incorporated
    Graphics Technology Department
    5000 Arlington Center Boulevard
    Columbus, Ohio  43220
    U. S. A.

CompuServe Incorporated maintains a mailing list with all those individuals and
organizations who wish to receive copies of this document when it is corrected
or revised. This service is offered free of charge; please provide us with your
mailing address.
*/

#include "third_party/blink/renderer/platform/image-decoders/gif/gif_image_reader.h"

#include <string.h>
#include <algorithm>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/platform/image-decoders/fast_shared_buffer_reader.h"

namespace blink {

namespace {

static constexpr unsigned kMaxColors = 256u;
static constexpr int kBytesPerColormapEntry = 3;

}  // namespace

// GETN(n, s) requests at least 'n' bytes available from 'q', at start of state
// 's'.
//
// Note: the hold will never need to be bigger than 256 bytes, as each GIF block
// (except colormaps) can never be bigger than 256 bytes. Colormaps are directly
// copied in the resp. global_colormap or dynamically allocated local_colormap,
// so a fixed buffer in GIFImageReader is good enough. This buffer is only
// needed to copy left-over data from one GifWrite call to the next.
#define GETN(n, s)           \
  do {                       \
    bytes_to_consume_ = (n); \
    state_ = (s);            \
  } while (0)

// Get a 16-bit value stored in little-endian format.
#define GETINT16(p) ((p)[1] << 8 | (p)[0])

// Send the data to the display front-end.
bool GIFLZWContext::OutputRow(GIFRow::const_iterator row_begin) {
  int drow_start = irow;
  int drow_end = irow;

  // Haeberli-inspired hack for interlaced GIFs: Replicate lines while
  // displaying to diminish the "venetian-blind" effect as the image is
  // loaded. Adjust pixel vertical positions to avoid the appearance of the
  // image crawling up the screen as successive passes are drawn.
  if (frame_context_->ProgressiveDisplay() && frame_context_->Interlaced() &&
      ipass < 4) {
    unsigned row_dup = 0;
    unsigned row_shift = 0;

    switch (ipass) {
      case 1:
        row_dup = 7;
        row_shift = 3;
        break;
      case 2:
        row_dup = 3;
        row_shift = 1;
        break;
      case 3:
        row_dup = 1;
        row_shift = 0;
        break;
      default:
        break;
    }

    drow_start -= row_shift;
    drow_end = drow_start + row_dup;

    // Extend if bottom edge isn't covered because of the shift upward.
    if (((frame_context_->Height() - 1) - drow_end) <= row_shift)
      drow_end = frame_context_->Height() - 1;

    // Clamp first and last rows to upper and lower edge of image.
    if (drow_start < 0)
      drow_start = 0;

    if ((unsigned)drow_end >= frame_context_->Height())
      drow_end = frame_context_->Height() - 1;
  }

  // Protect against too much image data.
  if ((unsigned)drow_start >= frame_context_->Height())
    return true;

  // CALLBACK: Let the client know we have decoded a row.
  if (!client_->HaveDecodedRow(frame_context_->FrameId(), row_begin,
                               frame_context_->Width(), drow_start,
                               drow_end - drow_start + 1,
                               frame_context_->ProgressiveDisplay() &&
                                   frame_context_->Interlaced() && ipass > 1))
    return false;

  if (!frame_context_->Interlaced()) {
    irow++;
  } else {
    do {
      switch (ipass) {
        case 1:
          irow += 8;
          if (irow >= frame_context_->Height()) {
            ipass++;
            irow = 4;
          }
          break;

        case 2:
          irow += 8;
          if (irow >= frame_context_->Height()) {
            ipass++;
            irow = 2;
          }
          break;

        case 3:
          irow += 4;
          if (irow >= frame_context_->Height()) {
            ipass++;
            irow = 1;
          }
          break;

        case 4:
          irow += 2;
          if (irow >= frame_context_->Height()) {
            ipass++;
            irow = 0;
          }
          break;

        default:
          break;
      }
    } while (irow > (frame_context_->Height() - 1));
  }
  return true;
}

// Performs Lempel-Ziv-Welch decoding. Returns whether decoding was successful.
// If successful, the block will have been completely consumed and/or
// rowsRemaining will be 0.
bool GIFLZWContext::DoLZW(const unsigned char* block, size_t bytes_in_block) {
  const size_t width = frame_context_->Width();

  if (row_iter == row_buffer.end())
    return true;

  for (const unsigned char* ch = block; bytes_in_block-- > 0; ch++) {
    // Feed the next byte into the decoder's 32-bit input buffer.
    datum += ((int)*ch) << bits;
    bits += 8;

    // Check for underflow of decoder's 32-bit input buffer.
    while (bits >= codesize) {
      // Get the leading variable-length symbol from the data stream.
      int code = datum & codemask;
      datum >>= codesize;
      bits -= codesize;

      // Reset the dictionary to its original state, if requested.
      if (code == clear_code) {
        codesize = frame_context_->DataSize() + 1;
        codemask = (1 << codesize) - 1;
        avail = clear_code + 2;
        oldcode = -1;
        continue;
      }

      // Check for explicit end-of-stream code.
      if (code == (clear_code + 1)) {
        // end-of-stream should only appear after all image data.
        if (!rows_remaining)
          return true;
        return false;
      }

      const int temp_code = code;
      unsigned short code_length = 0;
      if (code < avail) {
        // This is a pre-existing code, so we already know what it
        // encodes.
        code_length = suffix_length[code];
        row_iter += code_length;
      } else if (code == avail && oldcode != -1) {
        // This is a new code just being added to the dictionary.
        // It must encode the contents of the previous code, plus
        // the first character of the previous code again.
        code_length = suffix_length[oldcode] + 1;
        row_iter += code_length;
        *--row_iter = firstchar;
        code = oldcode;
      } else {
        // This is an invalid code. The dictionary is just initialized
        // and the code is incomplete. We don't know how to handle
        // this case.
        return false;
      }

      while (code >= clear_code) {
        *--row_iter = suffix[code];
        code = prefix[code];
      }

      *--row_iter = firstchar = suffix[code];

      // Define a new codeword in the dictionary as long as we've read
      // more than one value from the stream.
      if (avail < kMaxDictionaryEntries && oldcode != -1) {
        prefix[avail] = oldcode;
        suffix[avail] = firstchar;
        suffix_length[avail] = suffix_length[oldcode] + 1;
        ++avail;

        // If we've used up all the codewords of a given length
        // increase the length of codewords by one bit, but don't
        // exceed the specified maximum codeword size.
        if (!(avail & codemask) && avail < kMaxDictionaryEntries) {
          ++codesize;
          codemask += avail;
        }
      }
      oldcode = temp_code;
      row_iter += code_length;

      // Output as many rows as possible.
      GIFRow::iterator row_begin = row_buffer.begin();
      for (; row_begin + width <= row_iter; row_begin += width) {
        if (!OutputRow(row_begin))
          return false;
        rows_remaining--;
        if (!rows_remaining)
          return true;
      }

      if (row_begin != row_buffer.begin()) {
        // Move the remaining bytes to the beginning of the buffer.
        const size_t bytes_to_copy = row_iter - row_begin;
        memcpy(row_buffer.begin(), row_begin, bytes_to_copy);
        row_iter = row_buffer.begin() + bytes_to_copy;
      }
    }
  }
  return true;
}

void GIFColorMap::BuildTable(FastSharedBufferReader* reader) {
  if (!is_defined_ || !table_.IsEmpty())
    return;

  CHECK_LE(position_ + colors_ * kBytesPerColormapEntry, reader->size());
  DCHECK_LE(colors_, kMaxColors);
  char buffer[kMaxColors * kBytesPerColormapEntry];
  const unsigned char* src_colormap =
      reinterpret_cast<const unsigned char*>(reader->GetConsecutiveData(
          position_, colors_ * kBytesPerColormapEntry, buffer));
  table_.resize(colors_);
  for (Table::iterator iter = table_.begin(); iter != table_.end(); ++iter) {
    *iter = SkPackARGB32NoCheck(255, src_colormap[0], src_colormap[1],
                                src_colormap[2]);
    src_colormap += kBytesPerColormapEntry;
  }
}

// Decodes this frame. |frameDecoded| will be set to true if the entire frame is
// decoded. Returns true if decoding progressed further than before without
// error, or there is insufficient new data to decode further. Otherwise, a
// decoding error occurred; returns false in this case.
bool GIFFrameContext::Decode(FastSharedBufferReader* reader,
                             GIFImageDecoder* client,
                             bool* frame_decoded) {
  local_color_map_.BuildTable(reader);

  *frame_decoded = false;
  if (!lzw_context_) {
    // Wait for more data to properly initialize GIFLZWContext.
    if (!IsDataSizeDefined() || !IsHeaderDefined())
      return true;

    lzw_context_ = std::make_unique<GIFLZWContext>(client, this);
    if (!lzw_context_->PrepareToDecode()) {
      lzw_context_.reset();
      return false;
    }

    current_lzw_block_ = 0;
  }

  // Some bad GIFs have extra blocks beyond the last row, which we don't want to
  // decode.
  while (current_lzw_block_ < lzw_blocks_.size() &&
         lzw_context_->HasRemainingRows()) {
    size_t block_position = lzw_blocks_[current_lzw_block_].block_position;
    size_t block_size = lzw_blocks_[current_lzw_block_].block_size;
    if (block_position + block_size > reader->size())
      return false;

    while (block_size) {
      const char* segment = nullptr;
      size_t segment_length = reader->GetSomeData(segment, block_position);
      size_t decode_size = std::min(segment_length, block_size);
      if (!lzw_context_->DoLZW(reinterpret_cast<const unsigned char*>(segment),
                               decode_size))
        return false;
      block_position += decode_size;
      block_size -= decode_size;
    }
    ++current_lzw_block_;
  }

  // If this frame is data complete then the previous loop must have completely
  // decoded all LZW blocks.
  // There will be no more decoding for this frame so it's time to cleanup.
  if (IsComplete()) {
    *frame_decoded = true;
    lzw_context_.reset();
  }
  return true;
}

// Decodes a frame using GIFFrameContext:decode(). Returns true if decoding has
// progressed, or false if an error has occurred.
bool GIFImageReader::Decode(size_t frame_index) {
  FastSharedBufferReader reader(data_);
  global_color_map_.BuildTable(&reader);

  bool frame_decoded = false;
  GIFFrameContext* current_frame = frames_[frame_index].get();

  return current_frame->Decode(&reader, client_, &frame_decoded) &&
         (!frame_decoded || client_->FrameComplete(frame_index));
}

bool GIFImageReader::Parse(GIFImageDecoder::GIFParseQuery query) {
  if (bytes_read_ >= data_->size()) {
    // This data has already been parsed. For example, in deferred
    // decoding, a DecodingImageGenerator with more data may have already
    // used this same ImageDecoder to decode. This can happen if two
    // SkImages created by a DeferredImageDecoder are drawn/prerolled
    // out of order (with respect to how much data they had at creation
    // time).
    return !client_->Failed();
  }

  return ParseData(bytes_read_, data_->size() - bytes_read_, query);
}

// Parse incoming GIF data stream into internal data structures.
// Return true if parsing has progressed or there is not enough data.
// Return false if a fatal error is encountered.
bool GIFImageReader::ParseData(size_t data_position,
                               size_t len,
                               GIFImageDecoder::GIFParseQuery query) {
  if (!len) {
    // No new data has come in since the last call, just ignore this call.
    return true;
  }

  if (len < bytes_to_consume_)
    return true;

  FastSharedBufferReader reader(data_);

  // A read buffer of 16 bytes is enough to accomodate all possible reads for
  // parsing.
  char read_buffer[16];

  // Read as many components from |m_data| as possible. At the beginning of each
  // iteration, |dataPosition| is advanced by m_bytesToConsume to point to the
  // next component. |len| is decremented accordingly.
  while (len >= bytes_to_consume_) {
    const size_t current_component_position = data_position;

    // Mark the current component as consumed. Note that currentComponent will
    // remain pointed at this component until the next loop iteration.
    data_position += bytes_to_consume_;
    len -= bytes_to_consume_;

    switch (state_) {
      case GIFLZW:
        DCHECK(!frames_.IsEmpty());
        // m_bytesToConsume is the current component size because it hasn't been
        // updated.
        frames_.back()->AddLzwBlock(current_component_position,
                                    bytes_to_consume_);
        GETN(1, kGIFSubBlock);
        break;

      case kGIFLZWStart: {
        DCHECK(!frames_.IsEmpty());
        frames_.back()->SetDataSize(static_cast<unsigned char>(
            reader.GetOneByte(current_component_position)));
        GETN(1, kGIFSubBlock);
        break;
      }

      case kGIFType: {
        const char* current_component = reader.GetConsecutiveData(
            current_component_position, 6, read_buffer);

        // All GIF files begin with "GIF87a" or "GIF89a".
        if (!memcmp(current_component, "GIF89a", 6))
          version_ = 89;
        else if (!memcmp(current_component, "GIF87a", 6))
          version_ = 87;
        else
          return false;
        GETN(7, kGIFGlobalHeader);
        break;
      }

      case kGIFGlobalHeader: {
        const unsigned char* current_component =
            reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                current_component_position, 5, read_buffer));

        // This is the height and width of the "screen" or frame into which
        // images are rendered. The individual images can be smaller than
        // the screen size and located with an origin anywhere within the
        // screen.
        // Note that we don't inform the client of the size yet, as it might
        // change after we read the first frame's image header.
        screen_width_ = GETINT16(current_component);
        screen_height_ = GETINT16(current_component + 2);

        const size_t global_color_map_colors = 2
                                               << (current_component[4] & 0x07);

        if ((current_component[4] & 0x80) &&
            global_color_map_colors > 0) { /* global map */
          global_color_map_.SetTablePositionAndSize(data_position,
                                                    global_color_map_colors);
          GETN(kBytesPerColormapEntry * global_color_map_colors,
               kGIFGlobalColormap);
          break;
        }

        GETN(1, kGIFImageStart);
        break;
      }

      case kGIFGlobalColormap: {
        global_color_map_.SetDefined();
        GETN(1, kGIFImageStart);
        break;
      }

      case kGIFImageStart: {
        const char current_component =
            reader.GetOneByte(current_component_position);

        if (current_component == '!') {  // extension.
          GETN(2, kGIFExtension);
          break;
        }

        if (current_component == ',') {  // image separator.
          GETN(9, kGIFImageHeader);
          break;
        }

        // If we get anything other than ',' (image separator), '!'
        // (extension), or ';' (trailer), there is extraneous data
        // between blocks. The GIF87a spec tells us to keep reading
        // until we find an image separator, but GIF89a says such
        // a file is corrupt. We follow Mozilla's implementation and
        // proceed as if the file were correctly terminated, so the
        // GIF will display.
        GETN(0, kGIFDone);
        break;
      }

      case kGIFExtension: {
        const unsigned char* current_component =
            reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                current_component_position, 2, read_buffer));

        size_t bytes_in_block = current_component[1];
        GIFState exception_state = kGIFSkipBlock;

        switch (*current_component) {
          case 0xf9:
            exception_state = kGIFControlExtension;
            // The GIF spec mandates that the GIFControlExtension header block
            // length is 4 bytes, and the parser for this block reads 4 bytes,
            // so we must enforce that the buffer contains at least this many
            // bytes. If the GIF specifies a different length, we allow that, so
            // long as it's larger; the additional data will simply be ignored.
            bytes_in_block = std::max(bytes_in_block, static_cast<size_t>(4));
            break;

          // The GIF spec also specifies the lengths of the following two
          // extensions' headers (as 12 and 11 bytes, respectively). Because we
          // ignore the plain text extension entirely and sanity-check the
          // actual length of the application extension header before reading
          // it, we allow GIFs to deviate from these values in either direction.
          // This is important for real-world compatibility, as GIFs in the wild
          // exist with application extension headers that are both shorter and
          // longer than 11 bytes.
          case 0x01:
            // ignoring plain text extension
            break;

          case 0xff:
            exception_state = kGIFApplicationExtension;
            break;

          case 0xfe:
            exception_state = kGIFConsumeComment;
            break;
        }

        if (bytes_in_block)
          GETN(bytes_in_block, exception_state);
        else
          GETN(1, kGIFImageStart);
        break;
      }

      case kGIFConsumeBlock: {
        const unsigned char current_component = static_cast<unsigned char>(
            reader.GetOneByte(current_component_position));
        if (!current_component)
          GETN(1, kGIFImageStart);
        else
          GETN(current_component, kGIFSkipBlock);
        break;
      }

      case kGIFSkipBlock: {
        GETN(1, kGIFConsumeBlock);
        break;
      }

      case kGIFControlExtension: {
        const unsigned char* current_component =
            reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                current_component_position, 4, read_buffer));

        AddFrameIfNecessary();
        GIFFrameContext* current_frame = frames_.back().get();
        if (*current_component & 0x1)
          current_frame->SetTransparentPixel(current_component[3]);

        // We ignore the "user input" bit.

        // NOTE: This relies on the values in the FrameDisposalMethod enum
        // matching those in the GIF spec!
        int disposal_method = ((*current_component) >> 2) & 0x7;
        if (disposal_method < 4) {
          current_frame->SetDisposalMethod(
              static_cast<ImageFrame::DisposalMethod>(disposal_method));
        } else if (disposal_method == 4) {
          // Some specs say that disposal method 3 is "overwrite previous",
          // others that setting the third bit of the field (i.e. method 4) is.
          // We map both to the same value.
          current_frame->SetDisposalMethod(
              ImageFrame::kDisposeOverwritePrevious);
        }
        current_frame->SetDelayTime(GETINT16(current_component + 1) * 10);
        GETN(1, kGIFConsumeBlock);
        break;
      }

      case kGIFCommentExtension: {
        const unsigned char current_component = static_cast<unsigned char>(
            reader.GetOneByte(current_component_position));
        if (current_component)
          GETN(current_component, kGIFConsumeComment);
        else
          GETN(1, kGIFImageStart);
        break;
      }

      case kGIFConsumeComment: {
        GETN(1, kGIFCommentExtension);
        break;
      }

      case kGIFApplicationExtension: {
        // Check for netscape application extension.
        if (bytes_to_consume_ == 11) {
          const unsigned char* current_component =
              reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                  current_component_position, 11, read_buffer));

          if (!memcmp(current_component, "NETSCAPE2.0", 11) ||
              !memcmp(current_component, "ANIMEXTS1.0", 11))
            GETN(1, kGIFNetscapeExtensionBlock);
        }

        if (state_ != kGIFNetscapeExtensionBlock)
          GETN(1, kGIFConsumeBlock);
        break;
      }

      // Netscape-specific GIF extension: animation looping.
      case kGIFNetscapeExtensionBlock: {
        const int current_component = static_cast<unsigned char>(
            reader.GetOneByte(current_component_position));
        // GIFConsumeNetscapeExtension always reads 3 bytes from the stream; we
        // should at least wait for this amount.
        if (current_component)
          GETN(std::max(3, current_component), kGIFConsumeNetscapeExtension);
        else
          GETN(1, kGIFImageStart);
        break;
      }

      // Parse netscape-specific application extensions
      case kGIFConsumeNetscapeExtension: {
        const unsigned char* current_component =
            reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                current_component_position, 3, read_buffer));

        int netscape_extension = current_component[0] & 7;

        // Loop entire animation specified # of times. Only read the loop count
        // during the first iteration.
        if (netscape_extension == 1) {
          loop_count_ = GETINT16(current_component + 1);

          // Zero loop count is infinite animation loop request.
          if (!loop_count_)
            loop_count_ = kAnimationLoopInfinite;

          GETN(1, kGIFNetscapeExtensionBlock);
        } else if (netscape_extension == 2) {
          // Wait for specified # of bytes to enter buffer.

          // Don't do this, this extension doesn't exist (isn't used at all)
          // and doesn't do anything, as our streaming/buffering takes care of
          // it all. See http://semmix.pl/color/exgraf/eeg24.htm .
          GETN(1, kGIFNetscapeExtensionBlock);
        } else {
          // 0,3-7 are yet to be defined netscape extension codes
          return false;
        }
        break;
      }

      case kGIFImageHeader: {
        unsigned height, width, x_offset, y_offset;
        const unsigned char* current_component =
            reinterpret_cast<const unsigned char*>(reader.GetConsecutiveData(
                current_component_position, 9, read_buffer));

        /* Get image offsets, with respect to the screen origin */
        x_offset = GETINT16(current_component);
        y_offset = GETINT16(current_component + 2);

        /* Get image width and height. */
        width = GETINT16(current_component + 4);
        height = GETINT16(current_component + 6);

        // Some GIF files have frames that don't fit in the specified
        // overall image size. For the first frame, we can simply enlarge
        // the image size to allow the frame to be visible.  We can't do
        // this on subsequent frames because the rest of the decoding
        // infrastructure assumes the image size won't change as we
        // continue decoding, so any subsequent frames that are even
        // larger will be cropped.
        // Luckily, handling just the first frame is sufficient to deal
        // with most cases, e.g. ones where the image size is erroneously
        // set to zero, since usually the first frame completely fills
        // the image.
        if (CurrentFrameIsFirstFrame()) {
          screen_height_ = std::max(screen_height_, y_offset + height);
          screen_width_ = std::max(screen_width_, x_offset + width);
        }

        // Inform the client of the final size.
        if (!sent_size_to_client_ && client_ &&
            !client_->SetSize(screen_width_, screen_height_))
          return false;
        sent_size_to_client_ = true;

        if (query == GIFImageDecoder::kGIFSizeQuery) {
          // The decoder needs to stop. Hand back the number of bytes we
          // consumed from the buffer minus 9 (the amount we consumed to read
          // the header).
          SetRemainingBytes(len + 9);
          GETN(9, kGIFImageHeader);
          return true;
        }

        AddFrameIfNecessary();
        GIFFrameContext* current_frame = frames_.back().get();

        current_frame->SetHeaderDefined();

        // Work around more broken GIF files that have zero image width or
        // height.
        if (!height || !width) {
          height = screen_height_;
          width = screen_width_;
          if (!height || !width)
            return false;
        }
        current_frame->SetRect(x_offset, y_offset, width, height);
        current_frame->SetInterlaced(current_component[8] & 0x40);

        // Overlaying interlaced, transparent GIFs over
        // existing image data using the Haeberli display hack
        // requires saving the underlying image in order to
        // avoid jaggies at the transparency edges. We are
        // unprepared to deal with that, so don't display such
        // images progressively. Which means only the first
        // frame can be progressively displayed.
        // FIXME: It is possible that a non-transparent frame
        // can be interlaced and progressively displayed.
        current_frame->SetProgressiveDisplay(CurrentFrameIsFirstFrame());

        const bool is_local_colormap_defined = current_component[8] & 0x80;
        if (is_local_colormap_defined) {
          // The three low-order bits of currentComponent[8] specify the bits
          // per pixel.
          const size_t num_colors = 2 << (current_component[8] & 0x7);
          current_frame->LocalColorMap().SetTablePositionAndSize(data_position,
                                                                 num_colors);
          GETN(kBytesPerColormapEntry * num_colors, kGIFImageColormap);
          break;
        }

        GETN(1, kGIFLZWStart);
        break;
      }

      case kGIFImageColormap: {
        DCHECK(!frames_.IsEmpty());
        frames_.back()->LocalColorMap().SetDefined();
        GETN(1, kGIFLZWStart);
        break;
      }

      case kGIFSubBlock: {
        const size_t bytes_in_block = static_cast<unsigned char>(
            reader.GetOneByte(current_component_position));
        if (bytes_in_block) {
          GETN(bytes_in_block, GIFLZW);
        } else {
          // Finished parsing one frame; Process next frame.
          DCHECK(!frames_.IsEmpty());
          // Note that some broken GIF files do not have enough LZW blocks to
          // fully decode all rows; we treat this case as "frame complete".
          frames_.back()->SetComplete();
          GETN(1, kGIFImageStart);
        }
        break;
      }

      case kGIFDone: {
        parse_completed_ = true;
        return true;
      }

      default:
        // We shouldn't ever get here.
        return false;
        break;
    }
  }

  SetRemainingBytes(len);
  return true;
}

void GIFImageReader::SetRemainingBytes(size_t remaining_bytes) {
  DCHECK_LE(remaining_bytes, data_->size());
  bytes_read_ = data_->size() - remaining_bytes;
}

void GIFImageReader::AddFrameIfNecessary() {
  if (frames_.IsEmpty() || frames_.back()->IsComplete())
    frames_.push_back(base::WrapUnique(new GIFFrameContext(frames_.size())));
}

// FIXME: Move this method to close to doLZW().
bool GIFLZWContext::PrepareToDecode() {
  DCHECK(frame_context_->IsDataSizeDefined());
  DCHECK(frame_context_->IsHeaderDefined());

  // Since we use a codesize of 1 more than the datasize, we need to ensure
  // that our datasize is strictly less than the kMaxDictionaryEntryBits.
  if (frame_context_->DataSize() >= kMaxDictionaryEntryBits)
    return false;
  clear_code = 1 << frame_context_->DataSize();
  avail = clear_code + 2;
  oldcode = -1;
  codesize = frame_context_->DataSize() + 1;
  codemask = (1 << codesize) - 1;
  datum = bits = 0;
  ipass = frame_context_->Interlaced() ? 1 : 0;
  irow = 0;

  // We want to know the longest sequence encodable by a dictionary with
  // kMaxDictionaryEntries entries. If we ignore the need to encode the base
  // values themselves at the beginning of the dictionary, as well as the need
  // for a clear code or a termination code, we could use every entry to
  // encode a series of multiple values. If the input value stream looked
  // like "AAAAA..." (a long string of just one value), the first dictionary
  // entry would encode AA, the next AAA, the next AAAA, and so forth. Thus
  // the longest sequence would be kMaxDictionaryEntries + 1 values.
  //
  // However, we have to account for reserved entries. The first |datasize|
  // bits are reserved for the base values, and the next two entries are
  // reserved for the clear code and termination code. In theory a GIF can
  // set the datasize to 0, meaning we have just two reserved entries, making
  // the longest sequence (kMaxDictionaryEntries + 1) - 2 values long. Since
  // each value is a byte, this is also the number of bytes in the longest
  // encodable sequence.
  const size_t kMaxBytes = kMaxDictionaryEntries - 1;

  // Now allocate the output buffer. We decode directly into this buffer
  // until we have at least one row worth of data, then call outputRow().
  // This means worst case we may have (row width - 1) bytes in the buffer
  // and then decode a sequence |maxBytes| long to append.
  row_buffer.resize(frame_context_->Width() - 1 + kMaxBytes);
  row_iter = row_buffer.begin();
  rows_remaining = frame_context_->Height();

  // Clearing the whole suffix table lets us be more tolerant of bad data.
  for (int i = 0; i < clear_code; ++i) {
    suffix[i] = i;
    suffix_length[i] = 1;
  }
  return true;
}

}  // namespace blink