File: bmp_image_reader.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (1143 lines) | stat: -rw-r--r-- 42,919 bytes parent folder | download | duplicates (6)
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
// Copyright 2008 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/renderer/platform/image-decoders/bmp/bmp_image_reader.h"

#include "third_party/blink/renderer/platform/image-decoders/jpeg/jpeg_image_decoder.h"
#include "third_party/blink/renderer/platform/image-decoders/png/png_decoder_factory.h"
#include "third_party/skia/include/core/SkColorSpace.h"

namespace {

// See comments on lookup_table_addresses_ in the header.
constexpr uint8_t nBitTo8BitlookupTable[] = {
    // clang-format off
    // 1 bit
    0, 255,
    // 2 bits
    0, 85, 170, 255,
    // 3 bits
    0, 36, 73, 109, 146, 182, 219, 255,
    // 4 bits
    0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255,
    // 5 bits
    0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140,
    148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255,
    // 6 bits
    0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 45, 49, 53, 57, 61, 65, 69, 73, 77,
    81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142,
    146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, 202,
    206, 210, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255,
    // 7 bits
    0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38,
    40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76,
    78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110,
    112, 114, 116, 118, 120, 122, 124, 126, 129, 131, 133, 135, 137, 139, 141,
    143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171,
    173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201,
    203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231,
    233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255,
    // clang-format on
};

}  // namespace

namespace blink {

BMPImageReader::BMPImageReader(ImageDecoder* parent,
                               wtf_size_t decoded_and_header_offset,
                               wtf_size_t img_data_offset,
                               bool is_in_ico)
    : parent_(parent),
      decoded_offset_(decoded_and_header_offset),
      header_offset_(decoded_and_header_offset),
      img_data_offset_(img_data_offset),
      is_in_ico_(is_in_ico) {
  // Clue-in decodeBMP() that we need to detect the correct info header size.
  memset(&info_header_, 0, sizeof(info_header_));
}

BMPImageReader::~BMPImageReader() = default;

void BMPImageReader::SetData(scoped_refptr<SegmentReader> data) {
  data_ = data;
  fast_reader_.SetData(std::move(data));
  if (alternate_decoder_) {
    alternate_decoder_->SetData(data_.get(), parent_->IsAllDataReceived());
  }
}

bool BMPImageReader::DecodeBMP(bool only_size) {
  // Defensively clear the FastSharedBufferReader's cache, as another caller
  // may have called SharedBuffer::MergeSegmentsIntoBuffer().
  fast_reader_.ClearCache();

  // Calculate size of info header.
  if (!info_header_.size && !ReadInfoHeaderSize()) {
    return false;
  }

  const wtf_size_t header_end = header_offset_ + info_header_.size;
  // Read and process info header.
  if ((decoded_offset_ < header_end) && !ProcessInfoHeader()) {
    return false;
  }

  // If there is an applicable color profile, it must be processed now, since
  // once the image size is available, the decoding machinery assumes the color
  // space is as well.  Unfortunately, since the profile appears after
  // everything else, this may delay processing until all data is received.
  // Luckily, few BMPs have an embedded color profile.
  const bool use_alternate_decoder =
      (info_header_.compression == JPEG) || (info_header_.compression == PNG);
  if (!use_alternate_decoder && info_header_.profile_data &&
      !ProcessEmbeddedColorProfile()) {
    return false;
  }

  // Set our size if we haven't already.  In ICO files, IsDecodedSizeAvailable()
  // always returns true (since it reflects the size in the directory, which has
  // already been read); call SetSize() anyway, since it sanity-checks that the
  // size here matches the directory.
  if ((is_in_ico_ || !parent_->IsDecodedSizeAvailable()) &&
      !parent_->SetSize(static_cast<unsigned>(info_header_.width),
                        static_cast<unsigned>(info_header_.height))) {
    return false;
  }

  if (only_size) {
    return true;
  }

  if (use_alternate_decoder) {
    return DecodeAlternateFormat();
  }

  // Read and process the bitmasks, if needed.
  if (need_to_process_bitmasks_ && !ProcessBitmasks()) {
    return false;
  }

  // Read and process the color table, if needed.
  if (need_to_process_color_table_ && !ProcessColorTable()) {
    return false;
  }

  // Initialize the framebuffer if needed.
  DCHECK(buffer_);  // Parent should set this before asking us to decode!
  if ((buffer_->GetStatus() == ImageFrame::kFrameEmpty) && !InitFrame()) {
    return false;
  }

  // Decode the data.
  if (!decoding_and_mask_ && !PastEndOfImage(0) &&
      !DecodePixelData((info_header_.compression != RLE4) &&
                       (info_header_.compression != RLE8) &&
                       (info_header_.compression != RLE24))) {
    return false;
  }

  // If the image has an AND mask and there was no alpha data, process the
  // mask.
  if (is_in_ico_ && !decoding_and_mask_ &&
      ((info_header_.bit_count < 16) || !bit_masks_[3] ||
       !seen_non_zero_alpha_pixel_)) {
    // Reset decoding coordinates to start of image.
    coord_.set_x(0);
    coord_.set_y(is_top_down_ ? 0 : (parent_->Size().height() - 1));

    // The AND mask is stored as 1-bit data.
    info_header_.bit_count = 1;

    decoding_and_mask_ = true;
  }
  if (decoding_and_mask_ && !DecodePixelData(true)) {
    return false;
  }

  // Done!
  buffer_->SetStatus(ImageFrame::kFrameComplete);
  return true;
}

bool BMPImageReader::ReadInfoHeaderSize() {
  // Get size of info header.
  DCHECK_EQ(decoded_offset_, header_offset_);
  if ((decoded_offset_ > data_->size()) ||
      ((data_->size() - decoded_offset_) < 4)) {
    return false;
  }
  info_header_.size = ReadUint32(0);
  // Don't increment decoded_offset here, it just makes the code in
  // ProcessInfoHeader() more confusing.

  // Don't allow the header to overflow (which would be harmless here, but
  // problematic or at least confusing in other places), or to overrun the
  // image data.
  const wtf_size_t header_end = header_offset_ + info_header_.size;
  if ((header_end < header_offset_) ||
      (img_data_offset_ && (img_data_offset_ < header_end))) {
    return parent_->SetFailed();
  }

  // See if this is a header size we understand.  See comments in
  // ReadInfoHeader() for more.
  if (info_header_.size == 12) {
    // OS/2 1.x (and Windows V2): 12
    is_os21x_ = true;
  } else if ((info_header_.size == 40) || HasRGBMasksInHeader()) {
    // Windows V3+: 40, 52, 56, 108, 124
  } else if ((info_header_.size >= 16) && (info_header_.size <= 64) &&
             (!(info_header_.size & 3) || (info_header_.size == 42) ||
              (info_header_.size == 46))) {
    // OS/2 2.x: any multiple of 4 between 16 and 64, inclusive, or 42 or 46
    is_os22x_ = true;
  } else {
    return parent_->SetFailed();
  }

  return true;
}

bool BMPImageReader::ProcessInfoHeader() {
  // Read info header.
  DCHECK_EQ(decoded_offset_, header_offset_);
  if ((decoded_offset_ > data_->size()) ||
      ((data_->size() - decoded_offset_) < info_header_.size) ||
      !ReadInfoHeader()) {
    return false;
  }

  // Sanity-check header values before doing further fixup.
  if (!IsInfoHeaderValid()) {
    return parent_->SetFailed();
  }

  // For paletted images, bitmaps can set clr_used to 0 to mean "all colors", so
  // set it to the maximum number of colors for this bit depth.  Also do this
  // for bitmaps that put too large a value here.
  if (info_header_.bit_count < 16) {
    const uint32_t max_colors = uint32_t{1} << info_header_.bit_count;
    if (!info_header_.clr_used || (info_header_.clr_used > max_colors)) {
      info_header_.clr_used = max_colors;
    }
  }

  // For any bitmaps that set their BitCount to the wrong value, reset the
  // counts now that we've calculated the number of necessary colors, since
  // other code relies on this value being correct.
  if (info_header_.compression == RLE8) {
    info_header_.bit_count = 8;
  } else if (info_header_.compression == RLE4) {
    info_header_.bit_count = 4;
  }

  // Tell caller what still needs to be processed.
  if (info_header_.bit_count >= 16) {
    need_to_process_bitmasks_ = true;
  } else if (info_header_.bit_count) {
    need_to_process_color_table_ = true;
  }

  decoded_offset_ += info_header_.size;
  return true;
}

bool BMPImageReader::ReadInfoHeader() {
  // Supported info header formats:
  // * BITMAPCOREHEADER/OS21XBITMAPHEADER/"Windows V2".  Windows 2.x (?),
  //   OS/2 1.x.  12 bytes.  Incompatible with all below headers.
  // * BITMAPINFOHEADER/"Windows V3".  Windows 3.x.  40 bytes.  Changes width/
  //   height fields to 32 bit and adds features such as compression types.
  //   (Nomenclature: Note that "Windows V3" here and "BITMAPV3..." below are
  //   different things.)
  // * OS22XBITMAPHEADER/BITMAPCOREHEADER2.  OS/2 2.x.  16-64 bytes.  The first
  //   40 bytes are basically identical to BITMAPINFOHEADER, save that most
  //   fields are optional.  Further fields, if present, are incompatible with
  //   all below headers.  Adds features such as halftoning and color spaces
  //   (not implemented here).
  // * BITMAPV2HEADER/BITMAPV2INFOHEADER.  52 bytes.  Extends BITMAPINFOHEADER
  //   with R/G/B masks.  Poorly-documented and obscure.
  // * BITMAPV3HEADER/BITMAPV3INFOHEADER.  56 bytes.  Extends BITMAPV2HEADER
  //   with an alpha mask.  Poorly-documented and obscure.
  // * BITMAPV4HEADER/"Windows V4".  Windows 95.  108 bytes.  Extends
  //   BITMAPV3HEADER with color space support.
  // * BITMAPV5HEADER/"Windows V5".  Windows 98.  124 bytes.  Extends
  //   BITMAPV4HEADER with ICC profile support.

  // Pre-initialize some fields that not all headers set.
  info_header_.compression = RGB;
  info_header_.clr_used = 0;
  info_header_.profile_data = 0;
  info_header_.profile_size = 0;

  if (is_os21x_) {
    info_header_.width = ReadUint16(4);
    info_header_.height = ReadUint16(6);
    info_header_.bit_count = ReadUint16(10);
    return true;
  }

  info_header_.width = ReadUint32(4);
  info_header_.height = ReadUint32(8);
  if (is_in_ico_) {
    info_header_.height /= 2;
  }
  // Detect top-down BMPs.
  if (info_header_.height < 0) {
    // We can't negate INT32_MIN below to get a positive int32_t.
    // IsInfoHeaderValid() will reject heights of 1 << 16 or larger anyway,
    // so just reject this bitmap now.
    if (info_header_.height == INT32_MIN) {
      return parent_->SetFailed();
    }
    is_top_down_ = true;
    info_header_.height = -info_header_.height;
  }

  info_header_.bit_count = ReadUint16(14);

  // Read compression type, if present.
  if (info_header_.size >= 20) {
    const uint32_t compression = ReadUint32(16);

    // Detect OS/2 2.x-specific compression types.
    if ((compression == 3) && (info_header_.bit_count == 1)) {
      info_header_.compression = HUFFMAN1D;
      is_os22x_ = true;
    } else if ((compression == 4) && (info_header_.bit_count == 24)) {
      info_header_.compression = RLE24;
      is_os22x_ = true;
    } else if (compression > ALPHABITFIELDS) {
      return parent_->SetFailed();  // Some type we don't understand.
    } else {
      info_header_.compression = static_cast<CompressionType>(compression);
    }
  }

  // Read colors used, if present.
  if (info_header_.size >= 36) {
    info_header_.clr_used = ReadUint32(32);
  }

  // If we can safely read the four bitmasks from 40-56 bytes in, do that here.
  // If the bit depth is less than 16, these values will be ignored by the image
  // data decoders. If the bit depth is at least 16 but the compression format
  // isn't [ALPHA]BITFIELDS, the RGB bitmasks will be ignored and overwritten in
  // processBitmasks(). (The alpha bitmask will never be overwritten: images
  // that actually want alpha have to specify a valid alpha mask. See comments
  // in ProcessBitmasks().)
  //
  // For other BMPs, bit_masks_[] et. al will be initialized later during
  // ProcessBitmasks().
  if (HasRGBMasksInHeader()) {
    bit_masks_[0] = ReadUint32(40);
    bit_masks_[1] = ReadUint32(44);
    bit_masks_[2] = ReadUint32(48);
  }
  if (HasAlphaMaskInHeader()) {
    bit_masks_[3] = ReadUint32(52);
  }

  // Read color space information, if present and desirable.
  if (HasColorSpaceInfoInHeader() && !parent_->IgnoresColorSpace()) {
    enum {
      kLcsCalibratedRGB = 0x00000000,
      kLcssRGB = 0x73524742,               // "sRGB"
      kLcsWindowsColorSpace = 0x57696E20,  // "Win "
      kProfileLinked = 0x4c494e4b,         // "LINK"
      kProfileEmbedded = 0x4d424544,       // "MBED"
    };

    const uint32_t cs_type = ReadUint32(56);
    switch (cs_type) {
      case kLcsCalibratedRGB: {  // Endpoints and gamma specified directly
        skcms_ICCProfile profile;
        skcms_Init(&profile);

        // Convert chromaticity values from 2.30 fixed point to floating point.
        const auto fxpt2dot30_to_float = [](uint32_t fxpt2dot30) {
          return fxpt2dot30 * 9.31322574615478515625e-10f;
        };
        const float rx = fxpt2dot30_to_float(ReadUint32(60));
        const float ry = fxpt2dot30_to_float(ReadUint32(64));
        const float gx = fxpt2dot30_to_float(ReadUint32(72));
        const float gy = fxpt2dot30_to_float(ReadUint32(76));
        const float bx = fxpt2dot30_to_float(ReadUint32(84));
        const float by = fxpt2dot30_to_float(ReadUint32(88));
        // BMPs do not explicitly encode a white point.  Using the sRGB
        // illuminant (D65) seems reasonable given that Windows' system color
        // space is sRGB.
        constexpr float kD65x = 0.31271;
        constexpr float kD65y = 0.32902;
        skcms_Matrix3x3 to_xyzd50;
        if (!skcms_PrimariesToXYZD50(rx, ry, gx, gy, bx, by, kD65x, kD65y,
                                     &to_xyzd50)) {
          // Some real-world images have bogus values, e.g. all zeros.  Ignore
          // the color space data in such cases, rather than failing.
          break;
        }
        skcms_SetXYZD50(&profile, &to_xyzd50);

        // Convert gamma values from 16.16 fixed point to transfer functions.
        const auto fxpt16dot16_to_fn = [](uint32_t fxpt16dot16) {
          skcms_TransferFunction fn;
          fn.a = 1.0f;
          fn.b = fn.c = fn.d = fn.e = fn.f = 0.0f;
          // Petzold's "Programming Windows" claims the gamma here is a decoding
          // gamma (e.g. 2.2), as opposed to the inverse, an encoding gamma
          // (like PNG encodes in its gAMA chunk).
          fn.g = SkFixedToFloat(fxpt16dot16);
          return fn;
        };
        profile.has_trc = true;
        profile.trc[0].table_entries = 0;
        profile.trc[0].parametric = fxpt16dot16_to_fn(ReadUint32(96));
        profile.trc[1].table_entries = 0;
        profile.trc[1].parametric = fxpt16dot16_to_fn(ReadUint32(100));
        profile.trc[2].table_entries = 0;
        profile.trc[2].parametric = fxpt16dot16_to_fn(ReadUint32(104));

        parent_->SetEmbeddedColorProfile(
            std::make_unique<ColorProfile>(profile));
        break;
      }

      case kLcssRGB:               // sRGB
      case kLcsWindowsColorSpace:  // "The Windows default color space" (sRGB)
        parent_->SetEmbeddedColorProfile(
            std::make_unique<ColorProfile>(*skcms_sRGB_profile()));
        break;

      case kProfileEmbedded:  // Embedded ICC profile
        if (info_header_.size >= 120) {
          info_header_.profile_data = header_offset_ + ReadUint32(112);
          info_header_.profile_size = ReadUint32(116);
        }
        break;

      case kProfileLinked:  // Linked ICC profile.  Unsupported; presents
                            // security concerns.
      default:              // Unknown.
        break;
    }
  }

  return true;
}

bool BMPImageReader::IsInfoHeaderValid() const {
  // Non-positive widths/heights are invalid.  (We've already flipped the
  // sign of the height for top-down bitmaps.)
  if ((info_header_.width <= 0) || !info_header_.height) {
    return false;
  }

  // Only Windows V3+ has ICOs and top-down bitmaps.
  if ((is_in_ico_ || is_top_down_) && (is_os21x_ || is_os22x_)) {
    return false;
  }

  // Only bit depths of 1, 4, 8, or 24 are universally supported.
  if ((info_header_.bit_count != 1) && (info_header_.bit_count != 4) &&
      (info_header_.bit_count != 8) && (info_header_.bit_count != 24)) {
    // Windows V3+ additionally supports bit depths of 0 (for embedded
    // JPEG/PNG images), 2 (on Windows CE), 16, and 32.
    if (is_os21x_ || is_os22x_ ||
        (info_header_.bit_count && (info_header_.bit_count != 2) &&
         (info_header_.bit_count != 16) && (info_header_.bit_count != 32))) {
      return false;
    }
  }

  // Each compression type is only valid with certain bit depths (except RGB,
  // which can be used with any bit depth). Also, some formats do not support
  // some compression types.
  switch (info_header_.compression) {
    case RGB:
      if (!info_header_.bit_count) {
        return false;
      }
      break;

    case RLE8:
      // Supposedly there are undocumented formats like "BitCount = 1,
      // Compression = RLE4" (which means "4 bit, but with a 2-color table"),
      // so also allow the paletted RLE compression types to have too low a
      // bit count; we'll correct this later.
      if (!info_header_.bit_count || (info_header_.bit_count > 8)) {
        return false;
      }
      break;

    case RLE4:
      // See comments in RLE8.
      if (!info_header_.bit_count || (info_header_.bit_count > 4)) {
        return false;
      }
      break;

    case BITFIELDS:
    case ALPHABITFIELDS:
      // Only valid for Windows V3+.
      if (is_os21x_ || is_os22x_ ||
          ((info_header_.bit_count != 16) && (info_header_.bit_count != 32))) {
        return false;
      }
      break;

    case JPEG:
    case PNG:
      // Only valid for Windows V3+.  We don't support embedding these inside
      // ICO files.
      if (is_os21x_ || is_os22x_ || info_header_.bit_count ||
          !img_data_offset_) {
        return false;
      }
      break;

    case HUFFMAN1D:
      // Only valid for OS/2 2.x.
      if (!is_os22x_ || (info_header_.bit_count != 1)) {
        return false;
      }
      break;

    case RLE24:
      // Only valid for OS/2 2.x.
      if (!is_os22x_ || (info_header_.bit_count != 24)) {
        return false;
      }
      break;

    default:
      // Some type we don't understand.  This should have been caught in
      // ReadInfoHeader().
      NOTREACHED();
  }

  // Reject the following valid bitmap types that we don't currently bother
  // decoding.  Few other people decode these either, they're unlikely to be
  // in much use.
  // TODO(pkasting): Consider supporting these someday.
  //   * Bitmaps larger than 2^16 pixels in either dimension.
  if ((info_header_.width >= (1 << 16)) || (info_header_.height >= (1 << 16))) {
    return false;
  }
  //   * OS/2 2.x Huffman-encoded monochrome bitmaps (see
  //      http://www.fileformat.info/mirror/egff/ch09_05.htm , re: "G31D"
  //      algorithm; this seems to be used in TIFF files as well).
  if (info_header_.compression == HUFFMAN1D) {
    return false;
  }

  return true;
}

bool BMPImageReader::DecodeAlternateFormat() {
  // Create decoder if necessary.
  if (!alternate_decoder_) {
    if (info_header_.compression == JPEG) {
      alternate_decoder_ = std::make_unique<JPEGImageDecoder>(
          parent_->GetAlphaOption(), parent_->GetColorBehavior(),
          parent_->GetAuxImage(), parent_->GetMaxDecodedBytes(),
          img_data_offset_);
    } else {
      alternate_decoder_ = CreatePngImageDecoder(
          parent_->GetAlphaOption(), ImageDecoder::kDefaultBitDepth,
          parent_->GetColorBehavior(), parent_->GetMaxDecodedBytes(),
          img_data_offset_);
    }
    alternate_decoder_->SetData(data_.get(), parent_->IsAllDataReceived());
  }

  // Decode the image.
  if (alternate_decoder_->IsSizeAvailable()) {
    if (alternate_decoder_->Size() != parent_->Size()) {
      return parent_->SetFailed();
    }

    alternate_decoder_->SetMemoryAllocator(buffer_->GetAllocator());
    const auto* frame = alternate_decoder_->DecodeFrameBufferAtIndex(0);
    alternate_decoder_->SetMemoryAllocator(nullptr);

    if (frame) {
      *buffer_ = *frame;
    }
  }
  return alternate_decoder_->Failed()
             ? parent_->SetFailed()
             : (buffer_->GetStatus() == ImageFrame::kFrameComplete);
}

bool BMPImageReader::ProcessEmbeddedColorProfile() {
  // Ensure we have received the whole profile.
  if ((info_header_.profile_data > data_->size()) ||
      ((data_->size() - info_header_.profile_data) <
       info_header_.profile_size)) {
    return false;
  }

  // Parse the profile.
  auto owned_buffer = std::make_unique<char[]>(info_header_.profile_size);
  const char* buffer = fast_reader_.GetConsecutiveData(
      info_header_.profile_data, info_header_.profile_size, owned_buffer.get());
  auto profile = ColorProfile::Create(
      base::as_bytes(base::span(buffer, info_header_.profile_size)));
  if (!profile) {
    return parent_->SetFailed();
  }
  parent_->SetEmbeddedColorProfile(std::move(profile));

  // Zero |profile_data| so we don't try to process the profile again in the
  // future.
  info_header_.profile_data = 0;
  return true;
}

bool BMPImageReader::ProcessBitmasks() {
  // Create bit_masks_[] values for R/G/B.
  if ((info_header_.compression != BITFIELDS) &&
      (info_header_.compression != ALPHABITFIELDS)) {
    // The format doesn't actually use bitmasks.  To simplify the decode
    // logic later, create bitmasks for the RGB data.  For Windows V4+,
    // this overwrites the masks we read from the header, which are
    // supposed to be ignored in non-BITFIELDS cases.
    // 16 bits:    MSB <-                     xRRRRRGG GGGBBBBB -> LSB
    // 24/32 bits: MSB <- [AAAAAAAA] RRRRRRRR GGGGGGGG BBBBBBBB -> LSB
    const int num_bits = (info_header_.bit_count == 16) ? 5 : 8;
    for (int i = 0; i <= 2; ++i) {
      bit_masks_[i] = ((uint32_t{1} << (num_bits * (3 - i))) - 1) ^
                      ((uint32_t{1} << (num_bits * (2 - i))) - 1);
    }
  } else if (!HasRGBMasksInHeader()) {
    // For HasRGBMasksInHeader() bitmaps, this was already done when we read the
    // info header.

    // Fail if we don't have enough file space for the bitmasks.
    const wtf_size_t header_end = header_offset_ + info_header_.size;
    const bool read_alpha = info_header_.compression == ALPHABITFIELDS;
    const wtf_size_t kBitmasksSize = read_alpha ? 16 : 12;
    const wtf_size_t bitmasks_end = header_end + kBitmasksSize;
    if ((bitmasks_end < header_end) ||
        (img_data_offset_ && (img_data_offset_ < bitmasks_end))) {
      return parent_->SetFailed();
    }

    // Read bitmasks.
    if ((data_->size() - decoded_offset_) < kBitmasksSize) {
      return false;
    }
    bit_masks_[0] = ReadUint32(0);
    bit_masks_[1] = ReadUint32(4);
    bit_masks_[2] = ReadUint32(8);
    if (read_alpha) {
      bit_masks_[3] = ReadUint32(12);
    }

    decoded_offset_ += kBitmasksSize;
  }

  // Alpha is a poorly-documented and inconsistently-used feature.
  //
  // BITMAPV3HEADER+ have an alpha bitmask in the info header.  Unlike the R/G/B
  // bitmasks, the MSDN docs don't indicate that it is only valid for the
  // BITFIELDS compression format, so we respect it at all times.
  //
  // Windows CE supports the ALPHABITFIELDS compression format, which is rare.
  // We assume any mask specified by this format is valid as well.
  //
  // To complicate things, Windows V3 BMPs, which lack a mask, can specify 32bpp
  // format, which to any sane reader would imply an 8-bit alpha channel -- and
  // for BMPs-in-ICOs, that's precisely what's intended to happen. There also
  // exist standalone BMPs in this format which clearly expect the alpha channel
  // to be respected. However, there are many other BMPs which, for example,
  // fill this channel with all 0s, yet clearly expect to not be displayed as a
  // fully-transparent rectangle.
  //
  // If these were the only two types of Windows V3, 32bpp BMPs in the wild,
  // we could distinguish between them by scanning the alpha channel in the
  // image, looking for nonzero values, and only enabling alpha if we found
  // some. (It turns out we have to do this anyway, because, crazily, there
  // are also Windows V4+ BMPs with an explicit, non-zero alpha mask, which
  // then zero-fill their alpha channels! See comments in
  // processNonRLEData().)
  //
  // Unfortunately there are also V3 BMPs -- indeed, probably more than the
  // number of 32bpp, V3 BMPs which intentionally use alpha -- which specify
  // 32bpp format, use nonzero (and non-255) alpha values, and yet expect to
  // be rendered fully-opaque. And other browsers do so.
  //
  // So it's impossible to display every BMP in the way its creators intended,
  // and we have to choose what to break. Given the paragraph above, we match
  // other browsers and ignore alpha in Windows V3 BMPs except inside ICO
  // files.
  if (!HasAlphaMaskInHeader() && (info_header_.compression != ALPHABITFIELDS)) {
    const bool use_mask = is_in_ico_ &&
                          (info_header_.compression != BITFIELDS) &&
                          (info_header_.bit_count == 32);
    bit_masks_[3] = use_mask ? uint32_t{0xff000000} : 0;
  }

  // Check masks and set shift and LUT address values.
  for (int i = 0; i < 4; ++i) {
    // Trim the mask to the allowed bit depth.  Some Windows V4+ BMPs
    // specify a bogus alpha channel in bits that don't exist in the pixel
    // data (for example, bits 25-31 in a 24-bit RGB format).
    if (info_header_.bit_count < 32) {
      bit_masks_[i] &= ((uint32_t{1} << info_header_.bit_count) - 1);
    }

    // For empty masks (common on the alpha channel, especially after the
    // trimming above), quickly clear the shift and LUT address and
    // continue, to avoid an infinite loop in the counting code below.
    uint32_t temp_mask = bit_masks_[i];
    if (!temp_mask) {
      bit_shifts_right_[i] = 0;
      lookup_table_addresses_[i] = nullptr;
      continue;
    }

    // Make sure bitmask does not overlap any other bitmasks.
    for (int j = 0; j < i; ++j) {
      if (temp_mask & bit_masks_[j]) {
        return parent_->SetFailed();
      }
    }

    // Count offset into pixel data.
    for (bit_shifts_right_[i] = 0; !(temp_mask & 1); temp_mask >>= 1) {
      ++bit_shifts_right_[i];
    }

    // Count size of mask.
    wtf_size_t num_bits = 0;
    for (; temp_mask & 1; temp_mask >>= 1) {
      ++num_bits;
    }

    // Make sure bitmask is contiguous.
    if (temp_mask) {
      return parent_->SetFailed();
    }

    // Since RGBABuffer tops out at 8 bits per channel, adjust the shift
    // amounts to use the most significant 8 bits of the channel.
    if (num_bits >= 8) {
      bit_shifts_right_[i] += (num_bits - 8);
      num_bits = 0;
    }

    // Calculate LUT address.
    lookup_table_addresses_[i] =
        num_bits ? (nBitTo8BitlookupTable + (1 << num_bits) - 2) : nullptr;
  }

  // We've now decoded all the non-image data we care about.  Skip anything
  // else before the actual raster data.
  if (img_data_offset_) {
    decoded_offset_ = img_data_offset_;
  }
  need_to_process_bitmasks_ = false;
  return true;
}

bool BMPImageReader::ProcessColorTable() {
  // On non-OS/2 1.x, an extra padding byte is present, which we need to skip.
  const wtf_size_t bytes_per_color = is_os21x_ ? 3 : 4;

  const wtf_size_t header_end = header_offset_ + info_header_.size;
  wtf_size_t colors_in_palette = info_header_.clr_used;
  CHECK_LE(colors_in_palette, 256u);  // Enforced by ProcessInfoHeader().
  wtf_size_t table_size_in_bytes = colors_in_palette * bytes_per_color;
  const wtf_size_t table_end = header_end + table_size_in_bytes;
  if (table_end < header_end) {
    return parent_->SetFailed();
  }

  // Some BMPs don't contain a complete palette.  Truncate it instead of reading
  // off the end of the palette.
  if (img_data_offset_ && (img_data_offset_ < table_end)) {
    wtf_size_t colors_in_truncated_palette =
        (img_data_offset_ - header_end) / bytes_per_color;
    CHECK_LE(colors_in_truncated_palette, colors_in_palette);
    colors_in_palette = colors_in_truncated_palette;
    table_size_in_bytes = colors_in_palette * bytes_per_color;
  }

  // If we don't have enough data to read in the whole palette yet, stop here.
  if ((decoded_offset_ > data_->size()) ||
      ((data_->size() - decoded_offset_) < table_size_in_bytes)) {
    return false;
  }

  // Read the color table.
  color_table_.resize(colors_in_palette);

  for (wtf_size_t i = 0; i < colors_in_palette; ++i) {
    color_table_[i].rgb_blue = ReadUint8(0);
    color_table_[i].rgb_green = ReadUint8(1);
    color_table_[i].rgb_red = ReadUint8(2);
    decoded_offset_ += bytes_per_color;
  }

  // We've now decoded all the non-image data we care about.  Skip anything
  // else before the actual raster data.
  if (img_data_offset_) {
    decoded_offset_ = img_data_offset_;
  }
  need_to_process_color_table_ = false;
  return true;
}

bool BMPImageReader::InitFrame() {
  if (!buffer_->AllocatePixelData(parent_->Size().width(),
                                  parent_->Size().height(),
                                  parent_->ColorSpaceForSkImages())) {
    return parent_->SetFailed();  // Unable to allocate.
  }

  buffer_->ZeroFillPixelData();
  buffer_->SetStatus(ImageFrame::kFramePartial);
  // SetSize() calls EraseARGB(), which resets the alpha flag, so we force it
  // back to false here.  We'll set it to true later in all cases where these 0s
  // could actually show through.
  buffer_->SetHasAlpha(false);

  // For BMPs, the frame always fills the entire image.
  buffer_->SetOriginalFrameRect(gfx::Rect(parent_->Size()));

  if (!is_top_down_) {
    coord_.set_y(parent_->Size().height() - 1);
  }
  return true;
}

bool BMPImageReader::DecodePixelData(bool non_rle) {
  const gfx::Point coord(coord_);
  const ProcessingResult result =
      non_rle ? ProcessNonRLEData(false, 0) : ProcessRLEData();
  if (coord_ != coord) {
    buffer_->SetPixelsChanged(true);
  }
  return (result == kFailure) ? parent_->SetFailed() : (result == kSuccess);
}

BMPImageReader::ProcessingResult BMPImageReader::ProcessRLEData() {
  if (decoded_offset_ > data_->size()) {
    return kInsufficientData;
  }

  // RLE decoding is poorly specified.  Two main problems:
  // (1) Are EOL markers necessary?  What happens when we have too many
  //     pixels for one row?
  //     http://www.fileformat.info/format/bmp/egff.htm says extra pixels
  //     should wrap to the next line.  Real BMPs I've encountered seem to
  //     instead expect extra pixels to be ignored until the EOL marker is
  //     seen, although this has only happened in a few cases and I suspect
  //     those BMPs may be invalid.  So we only change lines on EOL (or Delta
  //     with dy > 0), and fail in most cases when pixels extend past the end
  //     of the line.
  // (2) When Delta, EOL, or EOF are seen, what happens to the "skipped"
  //     pixels?
  //     http://www.daubnet.com/formats/BMP.html says these should be filled
  //     with color 0.  However, the "do nothing" and "don't care" comments
  //     of other references suggest leaving these alone, i.e. letting them
  //     be transparent to the background behind the image.  This seems to
  //     match how MSPAINT treats BMPs, so we do that.  Note that when we
  //     actually skip pixels for a case like this, we need to note on the
  //     framebuffer that we have alpha.

  // Impossible to decode row-at-a-time, so just do things as a stream of
  // bytes.
  while (true) {
    // Every entry takes at least two bytes; bail if there isn't enough
    // data.
    if ((data_->size() - decoded_offset_) < 2) {
      return kInsufficientData;
    }

    // For every entry except EOF, we'd better not have reached the end of
    // the image.
    const uint8_t count = ReadUint8(0);
    const uint8_t code = ReadUint8(1);
    const bool is_past_end_of_image = PastEndOfImage(0);
    if ((count || (code != 1)) && is_past_end_of_image) {
      return kFailure;
    }

    // Decode.
    if (!count) {
      switch (code) {
        case 0:  // Magic token: EOL
          // Skip any remaining pixels in this row.
          if (coord_.x() < parent_->Size().width()) {
            buffer_->SetHasAlpha(true);
          }
          ColorCorrectCurrentRow();
          MoveBufferToNextRow();

          decoded_offset_ += 2;
          break;

        case 1:  // Magic token: EOF
          // Skip any remaining pixels in the image.
          if ((coord_.x() < parent_->Size().width()) ||
              (is_top_down_ ? (coord_.y() < (parent_->Size().height() - 1))
                            : (coord_.y() > 0))) {
            buffer_->SetHasAlpha(true);
          }
          if (!is_past_end_of_image) {
            ColorCorrectCurrentRow();
          }
          // There's no need to move |coord_| here to trigger the caller
          // to call SetPixelsChanged().  If the only thing that's changed
          // is the alpha state, that will be properly written into the
          // underlying SkBitmap when we mark the frame complete.
          return kSuccess;

        case 2: {  // Magic token: Delta
          // The next two bytes specify dx and dy.  Bail if there isn't
          // enough data.
          if ((data_->size() - decoded_offset_) < 4) {
            return kInsufficientData;
          }

          // Fail if this takes us past the end of the desired row or
          // past the end of the image.
          const uint8_t dx = ReadUint8(2);
          const uint8_t dy = ReadUint8(3);
          if (dx || dy) {
            buffer_->SetHasAlpha(true);
            if (dy) {
              ColorCorrectCurrentRow();
            }
          }
          if (((coord_.x() + dx) > parent_->Size().width()) ||
              PastEndOfImage(dy)) {
            return kFailure;
          }

          // Skip intervening pixels.
          coord_.Offset(dx, is_top_down_ ? dy : -dy);

          decoded_offset_ += 4;
          break;
        }

        default: {  // Absolute mode
          // |code| pixels specified as in BI_RGB, zero-padded at the end
          // to a multiple of 16 bits.
          // Because ProcessNonRLEData() expects decoded_offset_ to
          // point to the beginning of the pixel data, bump it past
          // the escape bytes and then reset if decoding failed.
          decoded_offset_ += 2;
          const ProcessingResult result = ProcessNonRLEData(true, code);
          if (result != kSuccess) {
            decoded_offset_ -= 2;
            return result;
          }
          break;
        }
      }
    } else {  // Encoded mode
      // The following color data is repeated for |count| total pixels.
      // Strangely, some BMPs seem to specify excessively large counts
      // here; ignore pixels past the end of the row.
      const int end_x = std::min(coord_.x() + count, parent_->Size().width());

      if (info_header_.compression == RLE24) {
        // Bail if there isn't enough data.
        if ((data_->size() - decoded_offset_) < 4) {
          return kInsufficientData;
        }

        // One BGR triple that we copy |count| times.
        FillRGBA(end_x, ReadUint8(3), ReadUint8(2), code, 0xff);
        decoded_offset_ += 4;
      } else {
        // RLE8 has one color index that gets repeated; RLE4 has two
        // color indexes in the upper and lower 4 bits of the byte,
        // which are alternated.
        wtf_size_t color_indexes[2] = {code, code};
        if (info_header_.compression == RLE4) {
          color_indexes[0] = (color_indexes[0] >> 4) & 0xf;
          color_indexes[1] &= 0xf;
        }
        for (wtf_size_t which = 0; coord_.x() < end_x;) {
          // Some images specify color values past the end of the
          // color table; set these pixels to black.
          if (color_indexes[which] < color_table_.size()) {
            SetI(color_indexes[which]);
          } else {
            SetRGBA(0, 0, 0, 255);
          }
          which = !which;
        }

        decoded_offset_ += 2;
      }
    }
  }
}

BMPImageReader::ProcessingResult BMPImageReader::ProcessNonRLEData(
    bool in_rle,
    int num_pixels) {
  if (decoded_offset_ > data_->size()) {
    return kInsufficientData;
  }

  if (!in_rle) {
    num_pixels = parent_->Size().width();
  }

  // Fail if we're being asked to decode more pixels than remain in the row.
  const int end_x = coord_.x() + num_pixels;
  if (end_x > parent_->Size().width()) {
    return kFailure;
  }

  // Determine how many bytes of data the requested number of pixels
  // requires.
  const wtf_size_t pixels_per_byte = 8 / info_header_.bit_count;
  const wtf_size_t bytes_per_pixel = info_header_.bit_count / 8;
  const wtf_size_t unpadded_num_bytes =
      (info_header_.bit_count < 16)
          ? ((num_pixels + pixels_per_byte - 1) / pixels_per_byte)
          : (num_pixels * bytes_per_pixel);
  // RLE runs are zero-padded at the end to a multiple of 16 bits.  Non-RLE
  // data is in rows and is zero-padded to a multiple of 32 bits.
  const wtf_size_t align_bits = in_rle ? 1 : 3;
  const wtf_size_t padded_num_bytes =
      (unpadded_num_bytes + align_bits) & ~align_bits;

  // Decode as many rows as we can.  (For RLE, where we only want to decode
  // one row, we've already checked that this condition is true.)
  while (!PastEndOfImage(0)) {
    // Bail if we don't have enough data for the desired number of pixels.
    if ((data_->size() - decoded_offset_) < padded_num_bytes) {
      return kInsufficientData;
    }

    if (info_header_.bit_count < 16) {
      // Paletted data.  Pixels are stored little-endian within bytes.
      // Decode pixels one byte at a time, left to right (so, starting at
      // the most significant bits in the byte).
      const uint8_t mask = (1 << info_header_.bit_count) - 1;
      for (wtf_size_t end_offset = decoded_offset_ + unpadded_num_bytes;
           decoded_offset_ < end_offset; ++decoded_offset_) {
        uint8_t pixel_data = ReadUint8(0);
        for (wtf_size_t pixel = 0;
             (pixel < pixels_per_byte) && (coord_.x() < end_x); ++pixel) {
          const wtf_size_t color_index =
              (pixel_data >> (8 - info_header_.bit_count)) & mask;
          if (decoding_and_mask_) {
            // There's no way to accurately represent an AND + XOR
            // operation as an RGBA image, so where the AND values
            // are 1, we simply set the framebuffer pixels to fully
            // transparent, on the assumption that most ICOs on the
            // web will not be doing a lot of inverting.
            if (color_index) {
              SetRGBA(0, 0, 0, 0);
              buffer_->SetHasAlpha(true);
            } else {
              coord_.Offset(1, 0);
            }
          } else {
            // See comments near the end of ProcessRLEData().
            if (color_index < color_table_.size()) {
              SetI(color_index);
            } else {
              SetRGBA(0, 0, 0, 255);
            }
          }
          pixel_data <<= info_header_.bit_count;
        }
      }
    } else {
      // RGB data.  Decode pixels one at a time, left to right.
      for (; coord_.x() < end_x; decoded_offset_ += bytes_per_pixel) {
        const uint32_t pixel = ReadCurrentPixel(bytes_per_pixel);

        // Some BMPs specify an alpha channel but don't actually use it
        // (it contains all 0s).  To avoid displaying these images as
        // fully-transparent, decode as if images are fully opaque
        // until we actually see a non-zero alpha value; at that point,
        // reset any previously-decoded pixels to fully transparent and
        // continue decoding based on the real alpha channel values.
        // As an optimization, avoid calling SetHasAlpha(true) for
        // images where all alpha values are 255; opaque images are
        // faster to draw.
        int alpha = GetAlpha(pixel);
        if (!seen_non_zero_alpha_pixel_ && !alpha) {
          seen_zero_alpha_pixel_ = true;
          alpha = 255;
        } else {
          seen_non_zero_alpha_pixel_ = true;
          if (seen_zero_alpha_pixel_) {
            buffer_->ZeroFillPixelData();
            seen_zero_alpha_pixel_ = false;
          } else if (alpha != 255) {
            buffer_->SetHasAlpha(true);
          }
        }

        SetRGBA(GetComponent(pixel, 0), GetComponent(pixel, 1),
                GetComponent(pixel, 2), alpha);
      }
    }

    // Success, keep going.
    decoded_offset_ += (padded_num_bytes - unpadded_num_bytes);
    if (in_rle) {
      return kSuccess;
    }
    ColorCorrectCurrentRow();
    MoveBufferToNextRow();
  }

  // Finished decoding whole image.
  return kSuccess;
}

void BMPImageReader::MoveBufferToNextRow() {
  coord_.Offset(-coord_.x(), is_top_down_ ? 1 : -1);
}

void BMPImageReader::ColorCorrectCurrentRow() {
  if (decoding_and_mask_) {
    return;
  }
  // Postprocess the image data according to the profile.
  const ColorProfileTransform* const transform = parent_->ColorTransform();
  if (!transform) {
    return;
  }
  int decoder_width = parent_->Size().width();
  // Enforce 0 ≤ current row < bitmap height.
  CHECK_GE(coord_.y(), 0);
  CHECK_LT(coord_.y(), buffer_->Bitmap().height());
  // Enforce decoder width == bitmap width exactly. (The bitmap rowbytes might
  // add a bit of padding, but we are only converting one row at a time.)
  CHECK_EQ(decoder_width, buffer_->Bitmap().width());
  ImageFrame::PixelData* const row = buffer_->GetAddr(0, coord_.y());
  const skcms_PixelFormat fmt = XformColorFormat();
  const skcms_AlphaFormat alpha =
      (buffer_->HasAlpha() && buffer_->PremultiplyAlpha())
          ? skcms_AlphaFormat_PremulAsEncoded
          : skcms_AlphaFormat_Unpremul;
  const bool success =
      skcms_Transform(row, fmt, alpha, transform->SrcProfile(), row, fmt, alpha,
                      transform->DstProfile(), decoder_width);
  DCHECK(success);
  buffer_->SetPixelsChanged(true);
}

}  // namespace blink