OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fxcodec/lbmp/fx_bmp.h" | 7 #include "core/fxcodec/lbmp/fx_bmp.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 bmp_ptr->skip_size = skip_size_org; | 98 bmp_ptr->skip_size = skip_size_org; |
99 return 2; | 99 return 2; |
100 } | 100 } |
101 bmp_ptr->width = | 101 bmp_ptr->width = |
102 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcWidth); | 102 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcWidth); |
103 bmp_ptr->height = | 103 bmp_ptr->height = |
104 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcHeight); | 104 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcHeight); |
105 bmp_ptr->bitCounts = | 105 bmp_ptr->bitCounts = |
106 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcBitCount); | 106 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcBitCount); |
107 bmp_ptr->compress_flag = BMP_RGB; | 107 bmp_ptr->compress_flag = BMP_RGB; |
108 bmp_ptr->imgTB_flag = FALSE; | 108 bmp_ptr->imgTB_flag = false; |
109 } break; | 109 } break; |
110 case kBmpInfoHeaderSize: { | 110 case kBmpInfoHeaderSize: { |
111 BmpInfoHeaderPtr bmp_info_header_ptr = nullptr; | 111 BmpInfoHeaderPtr bmp_info_header_ptr = nullptr; |
112 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, | 112 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, |
113 bmp_ptr->img_ifh_size)) { | 113 bmp_ptr->img_ifh_size)) { |
114 bmp_ptr->skip_size = skip_size_org; | 114 bmp_ptr->skip_size = skip_size_org; |
115 return 2; | 115 return 2; |
116 } | 116 } |
117 bmp_ptr->width = | 117 bmp_ptr->width = |
118 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); | 118 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); |
119 bmp_ptr->height = | 119 bmp_ptr->height = |
120 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); | 120 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); |
121 bmp_ptr->bitCounts = | 121 bmp_ptr->bitCounts = |
122 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); | 122 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); |
123 bmp_ptr->compress_flag = | 123 bmp_ptr->compress_flag = |
124 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); | 124 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); |
125 bmp_ptr->color_used = | 125 bmp_ptr->color_used = |
126 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); | 126 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); |
127 bmp_ptr->dpi_x = (int32_t)GetDWord_LSBFirst( | 127 bmp_ptr->dpi_x = (int32_t)GetDWord_LSBFirst( |
128 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); | 128 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); |
129 bmp_ptr->dpi_y = (int32_t)GetDWord_LSBFirst( | 129 bmp_ptr->dpi_y = (int32_t)GetDWord_LSBFirst( |
130 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); | 130 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); |
131 if (bmp_ptr->height < 0) { | 131 if (bmp_ptr->height < 0) { |
132 bmp_ptr->height = -bmp_ptr->height; | 132 bmp_ptr->height = -bmp_ptr->height; |
133 bmp_ptr->imgTB_flag = TRUE; | 133 bmp_ptr->imgTB_flag = true; |
134 } | 134 } |
135 } break; | 135 } break; |
136 default: { | 136 default: { |
137 if (bmp_ptr->img_ifh_size > | 137 if (bmp_ptr->img_ifh_size > |
138 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { | 138 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { |
139 BmpInfoHeaderPtr bmp_info_header_ptr = nullptr; | 139 BmpInfoHeaderPtr bmp_info_header_ptr = nullptr; |
140 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, | 140 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, |
141 bmp_ptr->img_ifh_size)) { | 141 bmp_ptr->img_ifh_size)) { |
142 bmp_ptr->skip_size = skip_size_org; | 142 bmp_ptr->skip_size = skip_size_org; |
143 return 2; | 143 return 2; |
144 } | 144 } |
145 uint16_t biPlanes; | 145 uint16_t biPlanes; |
146 bmp_ptr->width = | 146 bmp_ptr->width = |
147 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); | 147 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); |
148 bmp_ptr->height = | 148 bmp_ptr->height = |
149 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); | 149 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); |
150 bmp_ptr->bitCounts = | 150 bmp_ptr->bitCounts = |
151 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); | 151 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); |
152 bmp_ptr->compress_flag = | 152 bmp_ptr->compress_flag = |
153 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); | 153 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); |
154 bmp_ptr->color_used = | 154 bmp_ptr->color_used = |
155 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); | 155 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); |
156 biPlanes = GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biPlanes); | 156 biPlanes = GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biPlanes); |
157 bmp_ptr->dpi_x = GetDWord_LSBFirst( | 157 bmp_ptr->dpi_x = GetDWord_LSBFirst( |
158 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); | 158 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); |
159 bmp_ptr->dpi_y = GetDWord_LSBFirst( | 159 bmp_ptr->dpi_y = GetDWord_LSBFirst( |
160 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); | 160 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); |
161 if (bmp_ptr->height < 0) { | 161 if (bmp_ptr->height < 0) { |
162 bmp_ptr->height = -bmp_ptr->height; | 162 bmp_ptr->height = -bmp_ptr->height; |
163 bmp_ptr->imgTB_flag = TRUE; | 163 bmp_ptr->imgTB_flag = true; |
164 } | 164 } |
165 if (bmp_ptr->compress_flag == BMP_RGB && biPlanes == 1 && | 165 if (bmp_ptr->compress_flag == BMP_RGB && biPlanes == 1 && |
166 bmp_ptr->color_used == 0) { | 166 bmp_ptr->color_used == 0) { |
167 break; | 167 break; |
168 } | 168 } |
169 } | 169 } |
170 bmp_error(bmp_ptr, "Unsupported Bmp File"); | 170 bmp_error(bmp_ptr, "Unsupported Bmp File"); |
171 return 0; | 171 return 0; |
172 } | 172 } |
173 } | 173 } |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 383 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
384 bmp_ptr->out_row_buffer); | 384 bmp_ptr->out_row_buffer); |
385 } | 385 } |
386 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 386 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
387 return 1; | 387 return 1; |
388 } | 388 } |
389 int32_t bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr) { | 389 int32_t bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr) { |
390 uint8_t* first_byte_ptr = nullptr; | 390 uint8_t* first_byte_ptr = nullptr; |
391 uint8_t* second_byte_ptr = nullptr; | 391 uint8_t* second_byte_ptr = nullptr; |
392 bmp_ptr->col_num = 0; | 392 bmp_ptr->col_num = 0; |
393 while (TRUE) { | 393 while (true) { |
394 uint32_t skip_size_org = bmp_ptr->skip_size; | 394 uint32_t skip_size_org = bmp_ptr->skip_size; |
395 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) | 395 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) |
396 return 2; | 396 return 2; |
397 | 397 |
398 switch (*first_byte_ptr) { | 398 switch (*first_byte_ptr) { |
399 case RLE_MARKER: { | 399 case RLE_MARKER: { |
400 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { | 400 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { |
401 bmp_ptr->skip_size = skip_size_org; | 401 bmp_ptr->skip_size = skip_size_org; |
402 return 2; | 402 return 2; |
403 } | 403 } |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 } | 485 } |
486 } | 486 } |
487 } | 487 } |
488 bmp_error(bmp_ptr, "Any Uncontrol Error"); | 488 bmp_error(bmp_ptr, "Any Uncontrol Error"); |
489 return 0; | 489 return 0; |
490 } | 490 } |
491 int32_t bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr) { | 491 int32_t bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr) { |
492 uint8_t* first_byte_ptr = nullptr; | 492 uint8_t* first_byte_ptr = nullptr; |
493 uint8_t* second_byte_ptr = nullptr; | 493 uint8_t* second_byte_ptr = nullptr; |
494 bmp_ptr->col_num = 0; | 494 bmp_ptr->col_num = 0; |
495 while (TRUE) { | 495 while (true) { |
496 uint32_t skip_size_org = bmp_ptr->skip_size; | 496 uint32_t skip_size_org = bmp_ptr->skip_size; |
497 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) | 497 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) |
498 return 2; | 498 return 2; |
499 | 499 |
500 switch (*first_byte_ptr) { | 500 switch (*first_byte_ptr) { |
501 case RLE_MARKER: { | 501 case RLE_MARKER: { |
502 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { | 502 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { |
503 bmp_ptr->skip_size = skip_size_org; | 503 bmp_ptr->skip_size = skip_size_org; |
504 return 2; | 504 return 2; |
505 } | 505 } |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
889 } else { | 889 } else { |
890 i += rle[0]; | 890 i += rle[0]; |
891 dst_buf[dst_pos++] = rle[0]; | 891 dst_buf[dst_pos++] = rle[0]; |
892 dst_buf[dst_pos++] = rle[1]; | 892 dst_buf[dst_pos++] = rle[1]; |
893 } | 893 } |
894 } | 894 } |
895 dst_buf[dst_pos++] = RLE_MARKER; | 895 dst_buf[dst_pos++] = RLE_MARKER; |
896 dst_buf[dst_pos++] = RLE_EOI; | 896 dst_buf[dst_pos++] = RLE_EOI; |
897 dst_size = dst_pos; | 897 dst_size = dst_pos; |
898 } | 898 } |
899 FX_BOOL bmp_encode_image(bmp_compress_struct_p bmp_ptr, | 899 bool bmp_encode_image(bmp_compress_struct_p bmp_ptr, |
900 uint8_t*& dst_buf, | 900 uint8_t*& dst_buf, |
901 uint32_t& dst_size) { | 901 uint32_t& dst_size) { |
902 uint32_t head_size = sizeof(BmpFileHeader) + sizeof(BmpInfoHeader); | 902 uint32_t head_size = sizeof(BmpFileHeader) + sizeof(BmpInfoHeader); |
903 uint32_t pal_size = sizeof(uint32_t) * bmp_ptr->pal_num; | 903 uint32_t pal_size = sizeof(uint32_t) * bmp_ptr->pal_num; |
904 if (bmp_ptr->info_header.biClrUsed > 0 && | 904 if (bmp_ptr->info_header.biClrUsed > 0 && |
905 bmp_ptr->info_header.biClrUsed < bmp_ptr->pal_num) { | 905 bmp_ptr->info_header.biClrUsed < bmp_ptr->pal_num) { |
906 pal_size = sizeof(uint32_t) * bmp_ptr->info_header.biClrUsed; | 906 pal_size = sizeof(uint32_t) * bmp_ptr->info_header.biClrUsed; |
907 } | 907 } |
908 dst_size = head_size + sizeof(uint32_t) * bmp_ptr->pal_num; | 908 dst_size = head_size + sizeof(uint32_t) * bmp_ptr->pal_num; |
909 dst_buf = FX_TryAlloc(uint8_t, dst_size); | 909 dst_buf = FX_TryAlloc(uint8_t, dst_size); |
910 if (!dst_buf) | 910 if (!dst_buf) |
911 return FALSE; | 911 return false; |
912 | 912 |
913 FXSYS_memset(dst_buf, 0, dst_size); | 913 FXSYS_memset(dst_buf, 0, dst_size); |
914 bmp_ptr->file_header.bfOffBits = head_size; | 914 bmp_ptr->file_header.bfOffBits = head_size; |
915 if (bmp_ptr->pal_ptr && pal_size) { | 915 if (bmp_ptr->pal_ptr && pal_size) { |
916 FXSYS_memcpy(&dst_buf[head_size], bmp_ptr->pal_ptr, pal_size); | 916 FXSYS_memcpy(&dst_buf[head_size], bmp_ptr->pal_ptr, pal_size); |
917 bmp_ptr->file_header.bfOffBits += pal_size; | 917 bmp_ptr->file_header.bfOffBits += pal_size; |
918 } | 918 } |
919 WriteInfoHeader(&bmp_ptr->info_header, dst_buf); | 919 WriteInfoHeader(&bmp_ptr->info_header, dst_buf); |
920 switch (bmp_ptr->info_header.biCompression) { | 920 switch (bmp_ptr->info_header.biCompression) { |
921 case BMP_RGB: | 921 case BMP_RGB: |
922 bmp_encode_rgb(bmp_ptr, dst_buf, dst_size); | 922 bmp_encode_rgb(bmp_ptr, dst_buf, dst_size); |
923 break; | 923 break; |
924 case BMP_BITFIELDS: | 924 case BMP_BITFIELDS: |
925 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); | 925 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); |
926 break; | 926 break; |
927 case BMP_RLE8: | 927 case BMP_RLE8: |
928 bmp_encode_rle8(bmp_ptr, dst_buf, dst_size); | 928 bmp_encode_rle8(bmp_ptr, dst_buf, dst_size); |
929 break; | 929 break; |
930 case BMP_RLE4: | 930 case BMP_RLE4: |
931 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); | 931 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); |
932 break; | 932 break; |
933 default: | 933 default: |
934 break; | 934 break; |
935 } | 935 } |
936 bmp_ptr->file_header.bfSize = dst_size; | 936 bmp_ptr->file_header.bfSize = dst_size; |
937 WriteFileHeader(&bmp_ptr->file_header, dst_buf); | 937 WriteFileHeader(&bmp_ptr->file_header, dst_buf); |
938 return TRUE; | 938 return true; |
939 } | 939 } |
OLD | NEW |