| 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 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 const size_t kBmpCoreHeaderSize = 12; | 13 const size_t kBmpCoreHeaderSize = 12; |
| 14 const size_t kBmpInfoHeaderSize = 40; | 14 const size_t kBmpInfoHeaderSize = 40; |
| 15 | 15 |
| 16 // TODO(thestig): Replace with FXDWORD_GET_LSBFIRST? | 16 // TODO(thestig): Replace with FXDWORD_GET_LSBFIRST? |
| 17 FX_DWORD GetDWord_LSBFirst(uint8_t* p) { | 17 FX_DWORD GetDWord_LSBFirst(uint8_t* p) { |
| 18 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); | 18 return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24); |
| 19 } | 19 } |
| 20 | 20 |
| 21 void SetDWord_LSBFirst(uint8_t* p, FX_DWORD v) { | 21 void SetDWord_LSBFirst(uint8_t* p, FX_DWORD v) { |
| 22 p[0] = (uint8_t)v; | 22 p[0] = (uint8_t)v; |
| 23 p[1] = (uint8_t)(v >> 8); | 23 p[1] = (uint8_t)(v >> 8); |
| 24 p[2] = (uint8_t)(v >> 16); | 24 p[2] = (uint8_t)(v >> 16); |
| 25 p[3] = (uint8_t)(v >> 24); | 25 p[3] = (uint8_t)(v >> 24); |
| 26 } | 26 } |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 FX_WORD GetWord_LSBFirst(uint8_t* p) { | 29 uint16_t GetWord_LSBFirst(uint8_t* p) { |
| 30 return p[0] | (p[1] << 8); | 30 return p[0] | (p[1] << 8); |
| 31 } | 31 } |
| 32 void SetWord_LSBFirst(uint8_t* p, FX_WORD v) { | 32 void SetWord_LSBFirst(uint8_t* p, uint16_t v) { |
| 33 p[0] = (uint8_t)v; | 33 p[0] = (uint8_t)v; |
| 34 p[1] = (uint8_t)(v >> 8); | 34 p[1] = (uint8_t)(v >> 8); |
| 35 } | 35 } |
| 36 void bmp_error(bmp_decompress_struct_p bmp_ptr, const FX_CHAR* err_msg) { | 36 void bmp_error(bmp_decompress_struct_p bmp_ptr, const FX_CHAR* err_msg) { |
| 37 if (bmp_ptr && bmp_ptr->bmp_error_fn) { | 37 if (bmp_ptr && bmp_ptr->bmp_error_fn) { |
| 38 bmp_ptr->bmp_error_fn(bmp_ptr, err_msg); | 38 bmp_ptr->bmp_error_fn(bmp_ptr, err_msg); |
| 39 } | 39 } |
| 40 } | 40 } |
| 41 bmp_decompress_struct_p bmp_create_decompress() { | 41 bmp_decompress_struct_p bmp_create_decompress() { |
| 42 bmp_decompress_struct_p bmp_ptr = FX_Alloc(bmp_decompress_struct, 1); | 42 bmp_decompress_struct_p bmp_ptr = FX_Alloc(bmp_decompress_struct, 1); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 } break; | 138 } break; |
| 139 default: { | 139 default: { |
| 140 if (bmp_ptr->img_ifh_size > | 140 if (bmp_ptr->img_ifh_size > |
| 141 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { | 141 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { |
| 142 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; | 142 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; |
| 143 if (bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, | 143 if (bmp_read_data(bmp_ptr, (uint8_t**)&bmp_info_header_ptr, |
| 144 bmp_ptr->img_ifh_size) == NULL) { | 144 bmp_ptr->img_ifh_size) == NULL) { |
| 145 bmp_ptr->skip_size = skip_size_org; | 145 bmp_ptr->skip_size = skip_size_org; |
| 146 return 2; | 146 return 2; |
| 147 } | 147 } |
| 148 FX_WORD biPlanes; | 148 uint16_t biPlanes; |
| 149 bmp_ptr->width = | 149 bmp_ptr->width = |
| 150 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); | 150 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); |
| 151 bmp_ptr->height = | 151 bmp_ptr->height = |
| 152 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); | 152 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); |
| 153 bmp_ptr->bitCounts = | 153 bmp_ptr->bitCounts = |
| 154 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); | 154 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); |
| 155 bmp_ptr->compress_flag = | 155 bmp_ptr->compress_flag = |
| 156 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); | 156 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); |
| 157 bmp_ptr->color_used = | 157 bmp_ptr->color_used = |
| 158 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); | 158 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 *row_buf++ = des_buf[col >> 3] & (0x80 >> (col % 8)) ? 0x01 : 0x00; | 333 *row_buf++ = des_buf[col >> 3] & (0x80 >> (col % 8)) ? 0x01 : 0x00; |
| 334 } | 334 } |
| 335 } break; | 335 } break; |
| 336 case 4: { | 336 case 4: { |
| 337 for (int32_t col = 0; col < bmp_ptr->width; col++) { | 337 for (int32_t col = 0; col < bmp_ptr->width; col++) { |
| 338 *row_buf++ = (col & 0x01) ? (des_buf[col >> 1] & 0x0F) | 338 *row_buf++ = (col & 0x01) ? (des_buf[col >> 1] & 0x0F) |
| 339 : ((des_buf[col >> 1] & 0xF0) >> 4); | 339 : ((des_buf[col >> 1] & 0xF0) >> 4); |
| 340 } | 340 } |
| 341 } break; | 341 } break; |
| 342 case 16: { | 342 case 16: { |
| 343 FX_WORD* buf = (FX_WORD*)des_buf; | 343 uint16_t* buf = (uint16_t*)des_buf; |
| 344 uint8_t blue_bits = 0; | 344 uint8_t blue_bits = 0; |
| 345 uint8_t green_bits = 0; | 345 uint8_t green_bits = 0; |
| 346 uint8_t red_bits = 0; | 346 uint8_t red_bits = 0; |
| 347 for (int32_t i = 0; i < 16; i++) { | 347 for (int32_t i = 0; i < 16; i++) { |
| 348 if ((bmp_ptr->mask_blue >> i) & 0x01) { | 348 if ((bmp_ptr->mask_blue >> i) & 0x01) { |
| 349 blue_bits++; | 349 blue_bits++; |
| 350 } | 350 } |
| 351 if ((bmp_ptr->mask_green >> i) & 0x01) { | 351 if ((bmp_ptr->mask_green >> i) & 0x01) { |
| 352 green_bits++; | 352 green_bits++; |
| 353 } | 353 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 while (bmp_ptr->row_num < bmp_row_num_next) { | 544 while (bmp_ptr->row_num < bmp_row_num_next) { |
| 545 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); | 545 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); |
| 546 bmp_ptr->bmp_get_row_fn( | 546 bmp_ptr->bmp_get_row_fn( |
| 547 bmp_ptr, bmp_ptr->imgTB_flag | 547 bmp_ptr, bmp_ptr->imgTB_flag |
| 548 ? bmp_ptr->row_num++ | 548 ? bmp_ptr->row_num++ |
| 549 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 549 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
| 550 bmp_ptr->out_row_buffer); | 550 bmp_ptr->out_row_buffer); |
| 551 } | 551 } |
| 552 } break; | 552 } break; |
| 553 default: { | 553 default: { |
| 554 uint8_t size = (uint8_t)(((FX_WORD)(*first_byte_ptr) + 1) >> 1); | 554 uint8_t size = (uint8_t)(((uint16_t)(*first_byte_ptr) + 1) >> 1); |
| 555 if ((int32_t)*first_byte_ptr >= | 555 if ((int32_t)*first_byte_ptr >= |
| 556 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { | 556 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { |
| 557 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { | 557 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { |
| 558 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 558 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 559 return 0; | 559 return 0; |
| 560 } | 560 } |
| 561 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; | 561 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; |
| 562 } | 562 } |
| 563 if (bmp_read_data(bmp_ptr, &second_byte_ptr, | 563 if (bmp_read_data(bmp_ptr, &second_byte_ptr, |
| 564 size & 1 ? size + 1 : size) == NULL) { | 564 size & 1 ? size + 1 : size) == NULL) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 577 } | 577 } |
| 578 } | 578 } |
| 579 } break; | 579 } break; |
| 580 default: { | 580 default: { |
| 581 if (bmp_read_data(bmp_ptr, &second_byte_ptr, 1) == NULL) { | 581 if (bmp_read_data(bmp_ptr, &second_byte_ptr, 1) == NULL) { |
| 582 bmp_ptr->skip_size = skip_size_org; | 582 bmp_ptr->skip_size = skip_size_org; |
| 583 return 2; | 583 return 2; |
| 584 } | 584 } |
| 585 if ((int32_t)*first_byte_ptr > | 585 if ((int32_t)*first_byte_ptr > |
| 586 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { | 586 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { |
| 587 uint8_t size = (uint8_t)(((FX_WORD)(*first_byte_ptr) + 1) >> 1); | 587 uint8_t size = (uint8_t)(((uint16_t)(*first_byte_ptr) + 1) >> 1); |
| 588 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { | 588 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { |
| 589 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 589 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 590 return 0; | 590 return 0; |
| 591 } | 591 } |
| 592 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; | 592 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; |
| 593 } | 593 } |
| 594 for (uint8_t i = 0; i < *first_byte_ptr; i++) { | 594 for (uint8_t i = 0; i < *first_byte_ptr; i++) { |
| 595 if (i & 0x01) { | 595 if (i & 0x01) { |
| 596 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = | 596 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = |
| 597 (*second_byte_ptr & 0x0F); | 597 (*second_byte_ptr & 0x0F); |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 939 case BMP_RLE4: | 939 case BMP_RLE4: |
| 940 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); | 940 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); |
| 941 break; | 941 break; |
| 942 default: | 942 default: |
| 943 break; | 943 break; |
| 944 } | 944 } |
| 945 bmp_ptr->file_header.bfSize = dst_size; | 945 bmp_ptr->file_header.bfSize = dst_size; |
| 946 WriteFileHeader(&bmp_ptr->file_header, dst_buf); | 946 WriteFileHeader(&bmp_ptr->file_header, dst_buf); |
| 947 return TRUE; | 947 return TRUE; |
| 948 } | 948 } |
| OLD | NEW |