| 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 22 matching lines...) Expand all Loading... |
| 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); |
| 43 if (bmp_ptr == NULL) { | |
| 44 return NULL; | |
| 45 } | |
| 46 FXSYS_memset(bmp_ptr, 0, sizeof(bmp_decompress_struct)); | 43 FXSYS_memset(bmp_ptr, 0, sizeof(bmp_decompress_struct)); |
| 47 bmp_ptr->decode_status = BMP_D_STATUS_HEADER; | 44 bmp_ptr->decode_status = BMP_D_STATUS_HEADER; |
| 48 bmp_ptr->bmp_header_ptr = FX_Alloc(BmpFileHeader, 1); | 45 bmp_ptr->bmp_header_ptr = FX_Alloc(BmpFileHeader, 1); |
| 49 return bmp_ptr; | 46 return bmp_ptr; |
| 50 } | 47 } |
| 51 void bmp_destroy_decompress(bmp_decompress_struct_pp bmp_ptr_ptr) { | 48 void bmp_destroy_decompress(bmp_decompress_struct_pp bmp_ptr_ptr) { |
| 52 if (bmp_ptr_ptr == NULL || *bmp_ptr_ptr == NULL) { | 49 if (!bmp_ptr_ptr || !*bmp_ptr_ptr) |
| 53 return; | 50 return; |
| 54 } | 51 |
| 55 bmp_decompress_struct_p bmp_ptr = *bmp_ptr_ptr; | 52 bmp_decompress_struct_p bmp_ptr = *bmp_ptr_ptr; |
| 56 *bmp_ptr_ptr = NULL; | 53 *bmp_ptr_ptr = NULL; |
| 57 if (bmp_ptr->out_row_buffer) { | 54 if (bmp_ptr->out_row_buffer) { |
| 58 FX_Free(bmp_ptr->out_row_buffer); | 55 FX_Free(bmp_ptr->out_row_buffer); |
| 59 } | 56 } |
| 60 FX_Free(bmp_ptr->pal_ptr); | 57 FX_Free(bmp_ptr->pal_ptr); |
| 61 FX_Free(bmp_ptr->bmp_header_ptr); | 58 FX_Free(bmp_ptr->bmp_header_ptr); |
| 62 FX_Free(bmp_ptr); | 59 FX_Free(bmp_ptr); |
| 63 } | 60 } |
| 64 int32_t bmp_read_header(bmp_decompress_struct_p bmp_ptr) { | 61 int32_t bmp_read_header(bmp_decompress_struct_p bmp_ptr) { |
| 65 if (bmp_ptr == NULL) { | 62 if (!bmp_ptr) |
| 66 return 0; | 63 return 0; |
| 67 } | 64 |
| 68 uint32_t skip_size_org = bmp_ptr->skip_size; | 65 uint32_t skip_size_org = bmp_ptr->skip_size; |
| 69 if (bmp_ptr->decode_status == BMP_D_STATUS_HEADER) { | 66 if (bmp_ptr->decode_status == BMP_D_STATUS_HEADER) { |
| 70 ASSERT(sizeof(BmpFileHeader) == 14); | 67 ASSERT(sizeof(BmpFileHeader) == 14); |
| 71 BmpFileHeader* bmp_header_ptr = NULL; | 68 BmpFileHeader* bmp_header_ptr = NULL; |
| 72 if (bmp_read_data(bmp_ptr, (uint8_t**)&bmp_header_ptr, 14) == NULL) { | 69 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_header_ptr, 14)) |
| 73 return 2; | 70 return 2; |
| 74 } | 71 |
| 75 bmp_ptr->bmp_header_ptr->bfType = | 72 bmp_ptr->bmp_header_ptr->bfType = |
| 76 GetWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfType); | 73 GetWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfType); |
| 77 bmp_ptr->bmp_header_ptr->bfOffBits = | 74 bmp_ptr->bmp_header_ptr->bfOffBits = |
| 78 GetDWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfOffBits); | 75 GetDWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfOffBits); |
| 79 bmp_ptr->data_size = GetDWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfSize); | 76 bmp_ptr->data_size = GetDWord_LSBFirst((uint8_t*)&bmp_header_ptr->bfSize); |
| 80 if (bmp_ptr->bmp_header_ptr->bfType != BMP_SIGNATURE) { | 77 if (bmp_ptr->bmp_header_ptr->bfType != BMP_SIGNATURE) { |
| 81 bmp_error(bmp_ptr, "Not A Bmp Image"); | 78 bmp_error(bmp_ptr, "Not A Bmp Image"); |
| 82 return 0; | 79 return 0; |
| 83 } | 80 } |
| 84 if (bmp_ptr->avail_in < sizeof(uint32_t)) { | 81 if (bmp_ptr->avail_in < sizeof(uint32_t)) { |
| 85 bmp_ptr->skip_size = skip_size_org; | 82 bmp_ptr->skip_size = skip_size_org; |
| 86 return 2; | 83 return 2; |
| 87 } | 84 } |
| 88 bmp_ptr->img_ifh_size = | 85 bmp_ptr->img_ifh_size = |
| 89 GetDWord_LSBFirst(bmp_ptr->next_in + bmp_ptr->skip_size); | 86 GetDWord_LSBFirst(bmp_ptr->next_in + bmp_ptr->skip_size); |
| 90 bmp_ptr->pal_type = 0; | 87 bmp_ptr->pal_type = 0; |
| 91 static_assert(sizeof(BmpCoreHeader) == kBmpCoreHeaderSize, | 88 static_assert(sizeof(BmpCoreHeader) == kBmpCoreHeaderSize, |
| 92 "BmpCoreHeader has wrong size"); | 89 "BmpCoreHeader has wrong size"); |
| 93 static_assert(sizeof(BmpInfoHeader) == kBmpInfoHeaderSize, | 90 static_assert(sizeof(BmpInfoHeader) == kBmpInfoHeaderSize, |
| 94 "BmpInfoHeader has wrong size"); | 91 "BmpInfoHeader has wrong size"); |
| 95 switch (bmp_ptr->img_ifh_size) { | 92 switch (bmp_ptr->img_ifh_size) { |
| 96 case kBmpCoreHeaderSize: { | 93 case kBmpCoreHeaderSize: { |
| 97 bmp_ptr->pal_type = 1; | 94 bmp_ptr->pal_type = 1; |
| 98 BmpCoreHeaderPtr bmp_core_header_ptr = NULL; | 95 BmpCoreHeaderPtr bmp_core_header_ptr = NULL; |
| 99 if (bmp_read_data(bmp_ptr, (uint8_t**)&bmp_core_header_ptr, | 96 if (!bmp_read_data(bmp_ptr, (uint8_t**)&bmp_core_header_ptr, |
| 100 bmp_ptr->img_ifh_size) == NULL) { | 97 bmp_ptr->img_ifh_size)) { |
| 101 bmp_ptr->skip_size = skip_size_org; | 98 bmp_ptr->skip_size = skip_size_org; |
| 102 return 2; | 99 return 2; |
| 103 } | 100 } |
| 104 bmp_ptr->width = | 101 bmp_ptr->width = |
| 105 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcWidth); | 102 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcWidth); |
| 106 bmp_ptr->height = | 103 bmp_ptr->height = |
| 107 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcHeight); | 104 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcHeight); |
| 108 bmp_ptr->bitCounts = | 105 bmp_ptr->bitCounts = |
| 109 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcBitCount); | 106 GetWord_LSBFirst((uint8_t*)&bmp_core_header_ptr->bcBitCount); |
| 110 bmp_ptr->compress_flag = BMP_RGB; | 107 bmp_ptr->compress_flag = BMP_RGB; |
| 111 bmp_ptr->imgTB_flag = FALSE; | 108 bmp_ptr->imgTB_flag = FALSE; |
| 112 } break; | 109 } break; |
| 113 case kBmpInfoHeaderSize: { | 110 case kBmpInfoHeaderSize: { |
| 114 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; | 111 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; |
| 115 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, |
| 116 bmp_ptr->img_ifh_size) == NULL) { | 113 bmp_ptr->img_ifh_size)) { |
| 117 bmp_ptr->skip_size = skip_size_org; | 114 bmp_ptr->skip_size = skip_size_org; |
| 118 return 2; | 115 return 2; |
| 119 } | 116 } |
| 120 bmp_ptr->width = | 117 bmp_ptr->width = |
| 121 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); | 118 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); |
| 122 bmp_ptr->height = | 119 bmp_ptr->height = |
| 123 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); | 120 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); |
| 124 bmp_ptr->bitCounts = | 121 bmp_ptr->bitCounts = |
| 125 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); | 122 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); |
| 126 bmp_ptr->compress_flag = | 123 bmp_ptr->compress_flag = |
| 127 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); | 124 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biCompression); |
| 128 bmp_ptr->color_used = | 125 bmp_ptr->color_used = |
| 129 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); | 126 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biClrUsed); |
| 130 bmp_ptr->dpi_x = (int32_t)GetDWord_LSBFirst( | 127 bmp_ptr->dpi_x = (int32_t)GetDWord_LSBFirst( |
| 131 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); | 128 (uint8_t*)&bmp_info_header_ptr->biXPelsPerMeter); |
| 132 bmp_ptr->dpi_y = (int32_t)GetDWord_LSBFirst( | 129 bmp_ptr->dpi_y = (int32_t)GetDWord_LSBFirst( |
| 133 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); | 130 (uint8_t*)&bmp_info_header_ptr->biYPelsPerMeter); |
| 134 if (bmp_ptr->height < 0) { | 131 if (bmp_ptr->height < 0) { |
| 135 bmp_ptr->height = -bmp_ptr->height; | 132 bmp_ptr->height = -bmp_ptr->height; |
| 136 bmp_ptr->imgTB_flag = TRUE; | 133 bmp_ptr->imgTB_flag = TRUE; |
| 137 } | 134 } |
| 138 } break; | 135 } break; |
| 139 default: { | 136 default: { |
| 140 if (bmp_ptr->img_ifh_size > | 137 if (bmp_ptr->img_ifh_size > |
| 141 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { | 138 std::min(kBmpInfoHeaderSize, sizeof(BmpInfoHeader))) { |
| 142 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; | 139 BmpInfoHeaderPtr bmp_info_header_ptr = NULL; |
| 143 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, |
| 144 bmp_ptr->img_ifh_size) == NULL) { | 141 bmp_ptr->img_ifh_size)) { |
| 145 bmp_ptr->skip_size = skip_size_org; | 142 bmp_ptr->skip_size = skip_size_org; |
| 146 return 2; | 143 return 2; |
| 147 } | 144 } |
| 148 uint16_t biPlanes; | 145 uint16_t biPlanes; |
| 149 bmp_ptr->width = | 146 bmp_ptr->width = |
| 150 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); | 147 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biWidth); |
| 151 bmp_ptr->height = | 148 bmp_ptr->height = |
| 152 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); | 149 GetDWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biHeight); |
| 153 bmp_ptr->bitCounts = | 150 bmp_ptr->bitCounts = |
| 154 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); | 151 GetWord_LSBFirst((uint8_t*)&bmp_info_header_ptr->biBitCount); |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 return bmp_decode_rle4(bmp_ptr); | 313 return bmp_decode_rle4(bmp_ptr); |
| 317 } | 314 } |
| 318 } | 315 } |
| 319 bmp_error(bmp_ptr, "Any Uncontrol Error"); | 316 bmp_error(bmp_ptr, "Any Uncontrol Error"); |
| 320 return 0; | 317 return 0; |
| 321 } | 318 } |
| 322 int32_t bmp_decode_rgb(bmp_decompress_struct_p bmp_ptr) { | 319 int32_t bmp_decode_rgb(bmp_decompress_struct_p bmp_ptr) { |
| 323 uint8_t* row_buf = bmp_ptr->out_row_buffer; | 320 uint8_t* row_buf = bmp_ptr->out_row_buffer; |
| 324 uint8_t* des_buf = NULL; | 321 uint8_t* des_buf = NULL; |
| 325 while (bmp_ptr->row_num < bmp_ptr->height) { | 322 while (bmp_ptr->row_num < bmp_ptr->height) { |
| 326 if (bmp_read_data(bmp_ptr, &des_buf, bmp_ptr->src_row_bytes) == NULL) { | 323 if (!bmp_read_data(bmp_ptr, &des_buf, bmp_ptr->src_row_bytes)) |
| 327 return 2; | 324 return 2; |
| 328 } | 325 |
| 329 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_DATA); | 326 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_DATA); |
| 330 switch (bmp_ptr->bitCounts) { | 327 switch (bmp_ptr->bitCounts) { |
| 331 case 1: { | 328 case 1: { |
| 332 for (int32_t col = 0; col < bmp_ptr->width; col++) { | 329 for (int32_t col = 0; col < bmp_ptr->width; col++) { |
| 333 *row_buf++ = des_buf[col >> 3] & (0x80 >> (col % 8)) ? 0x01 : 0x00; | 330 *row_buf++ = des_buf[col >> 3] & (0x80 >> (col % 8)) ? 0x01 : 0x00; |
| 334 } | 331 } |
| 335 } break; | 332 } break; |
| 336 case 4: { | 333 case 4: { |
| 337 for (int32_t col = 0; col < bmp_ptr->width; col++) { | 334 for (int32_t col = 0; col < bmp_ptr->width; col++) { |
| 338 *row_buf++ = (col & 0x01) ? (des_buf[col >> 1] & 0x0F) | 335 *row_buf++ = (col & 0x01) ? (des_buf[col >> 1] & 0x0F) |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 } | 379 } |
| 383 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 380 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
| 384 return 1; | 381 return 1; |
| 385 } | 382 } |
| 386 int32_t bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr) { | 383 int32_t bmp_decode_rle8(bmp_decompress_struct_p bmp_ptr) { |
| 387 uint8_t* first_byte_ptr = NULL; | 384 uint8_t* first_byte_ptr = NULL; |
| 388 uint8_t* second_byte_ptr = NULL; | 385 uint8_t* second_byte_ptr = NULL; |
| 389 bmp_ptr->col_num = 0; | 386 bmp_ptr->col_num = 0; |
| 390 while (TRUE) { | 387 while (TRUE) { |
| 391 uint32_t skip_size_org = bmp_ptr->skip_size; | 388 uint32_t skip_size_org = bmp_ptr->skip_size; |
| 392 if (bmp_read_data(bmp_ptr, &first_byte_ptr, 1) == NULL) { | 389 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) |
| 393 return 2; | 390 return 2; |
| 394 } | 391 |
| 395 switch (*first_byte_ptr) { | 392 switch (*first_byte_ptr) { |
| 396 case RLE_MARKER: { | 393 case RLE_MARKER: { |
| 397 if (bmp_read_data(bmp_ptr, &first_byte_ptr, 1) == NULL) { | 394 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { |
| 398 bmp_ptr->skip_size = skip_size_org; | 395 bmp_ptr->skip_size = skip_size_org; |
| 399 return 2; | 396 return 2; |
| 400 } | 397 } |
| 401 switch (*first_byte_ptr) { | 398 switch (*first_byte_ptr) { |
| 402 case RLE_EOL: { | 399 case RLE_EOL: { |
| 403 if (bmp_ptr->row_num >= bmp_ptr->height) { | 400 if (bmp_ptr->row_num >= bmp_ptr->height) { |
| 404 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 401 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
| 405 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 402 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 406 return 0; | 403 return 0; |
| 407 } | 404 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 421 bmp_ptr, bmp_ptr->imgTB_flag | 418 bmp_ptr, bmp_ptr->imgTB_flag |
| 422 ? bmp_ptr->row_num++ | 419 ? bmp_ptr->row_num++ |
| 423 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 420 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
| 424 bmp_ptr->out_row_buffer); | 421 bmp_ptr->out_row_buffer); |
| 425 } | 422 } |
| 426 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 423 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
| 427 return 1; | 424 return 1; |
| 428 } | 425 } |
| 429 case RLE_DELTA: { | 426 case RLE_DELTA: { |
| 430 uint8_t* delta_ptr; | 427 uint8_t* delta_ptr; |
| 431 if (bmp_read_data(bmp_ptr, &delta_ptr, 2) == NULL) { | 428 if (!bmp_read_data(bmp_ptr, &delta_ptr, 2)) { |
| 432 bmp_ptr->skip_size = skip_size_org; | 429 bmp_ptr->skip_size = skip_size_org; |
| 433 return 2; | 430 return 2; |
| 434 } | 431 } |
| 435 bmp_ptr->col_num += (int32_t)delta_ptr[0]; | 432 bmp_ptr->col_num += (int32_t)delta_ptr[0]; |
| 436 int32_t bmp_row_num_next = bmp_ptr->row_num + (int32_t)delta_ptr[1]; | 433 int32_t bmp_row_num_next = bmp_ptr->row_num + (int32_t)delta_ptr[1]; |
| 437 if (bmp_ptr->col_num >= bmp_ptr->out_row_bytes || | 434 if (bmp_ptr->col_num >= bmp_ptr->out_row_bytes || |
| 438 bmp_row_num_next >= bmp_ptr->height) { | 435 bmp_row_num_next >= bmp_ptr->height) { |
| 439 bmp_error(bmp_ptr, "The Bmp File Is Corrupt Or Not Supported"); | 436 bmp_error(bmp_ptr, "The Bmp File Is Corrupt Or Not Supported"); |
| 440 return 0; | 437 return 0; |
| 441 } | 438 } |
| 442 while (bmp_ptr->row_num < bmp_row_num_next) { | 439 while (bmp_ptr->row_num < bmp_row_num_next) { |
| 443 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); | 440 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); |
| 444 bmp_ptr->bmp_get_row_fn( | 441 bmp_ptr->bmp_get_row_fn( |
| 445 bmp_ptr, bmp_ptr->imgTB_flag | 442 bmp_ptr, bmp_ptr->imgTB_flag |
| 446 ? bmp_ptr->row_num++ | 443 ? bmp_ptr->row_num++ |
| 447 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 444 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
| 448 bmp_ptr->out_row_buffer); | 445 bmp_ptr->out_row_buffer); |
| 449 } | 446 } |
| 450 } break; | 447 } break; |
| 451 default: { | 448 default: { |
| 452 if ((int32_t)(*first_byte_ptr) > | 449 if ((int32_t)(*first_byte_ptr) > |
| 453 bmp_ptr->src_row_bytes - bmp_ptr->col_num) { | 450 bmp_ptr->src_row_bytes - bmp_ptr->col_num) { |
| 454 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 451 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 455 return 0; | 452 return 0; |
| 456 } | 453 } |
| 457 if (bmp_read_data(bmp_ptr, &second_byte_ptr, | 454 if (!bmp_read_data(bmp_ptr, &second_byte_ptr, |
| 458 *first_byte_ptr & 1 ? *first_byte_ptr + 1 | 455 *first_byte_ptr & 1 ? *first_byte_ptr + 1 |
| 459 : *first_byte_ptr) == NULL) { | 456 : *first_byte_ptr)) { |
| 460 bmp_ptr->skip_size = skip_size_org; | 457 bmp_ptr->skip_size = skip_size_org; |
| 461 return 2; | 458 return 2; |
| 462 } | 459 } |
| 463 FXSYS_memcpy(bmp_ptr->out_row_buffer + bmp_ptr->col_num, | 460 FXSYS_memcpy(bmp_ptr->out_row_buffer + bmp_ptr->col_num, |
| 464 second_byte_ptr, *first_byte_ptr); | 461 second_byte_ptr, *first_byte_ptr); |
| 465 bmp_ptr->col_num += (int32_t)(*first_byte_ptr); | 462 bmp_ptr->col_num += (int32_t)(*first_byte_ptr); |
| 466 } | 463 } |
| 467 } | 464 } |
| 468 } break; | 465 } break; |
| 469 default: { | 466 default: { |
| 470 if (bmp_read_data(bmp_ptr, &second_byte_ptr, 1) == NULL) { | 467 if (!bmp_read_data(bmp_ptr, &second_byte_ptr, 1)) { |
| 471 bmp_ptr->skip_size = skip_size_org; | 468 bmp_ptr->skip_size = skip_size_org; |
| 472 return 2; | 469 return 2; |
| 473 } | 470 } |
| 474 if ((int32_t)(*first_byte_ptr) > | 471 if ((int32_t)(*first_byte_ptr) > |
| 475 bmp_ptr->src_row_bytes - bmp_ptr->col_num) { | 472 bmp_ptr->src_row_bytes - bmp_ptr->col_num) { |
| 476 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 473 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 477 return 0; | 474 return 0; |
| 478 } | 475 } |
| 479 FXSYS_memset(bmp_ptr->out_row_buffer + bmp_ptr->col_num, | 476 FXSYS_memset(bmp_ptr->out_row_buffer + bmp_ptr->col_num, |
| 480 *second_byte_ptr, *first_byte_ptr); | 477 *second_byte_ptr, *first_byte_ptr); |
| 481 bmp_ptr->col_num += (int32_t)(*first_byte_ptr); | 478 bmp_ptr->col_num += (int32_t)(*first_byte_ptr); |
| 482 } | 479 } |
| 483 } | 480 } |
| 484 } | 481 } |
| 485 bmp_error(bmp_ptr, "Any Uncontrol Error"); | 482 bmp_error(bmp_ptr, "Any Uncontrol Error"); |
| 486 return 0; | 483 return 0; |
| 487 } | 484 } |
| 488 int32_t bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr) { | 485 int32_t bmp_decode_rle4(bmp_decompress_struct_p bmp_ptr) { |
| 489 uint8_t* first_byte_ptr = NULL; | 486 uint8_t* first_byte_ptr = NULL; |
| 490 uint8_t* second_byte_ptr = NULL; | 487 uint8_t* second_byte_ptr = NULL; |
| 491 bmp_ptr->col_num = 0; | 488 bmp_ptr->col_num = 0; |
| 492 while (TRUE) { | 489 while (TRUE) { |
| 493 uint32_t skip_size_org = bmp_ptr->skip_size; | 490 uint32_t skip_size_org = bmp_ptr->skip_size; |
| 494 if (bmp_read_data(bmp_ptr, &first_byte_ptr, 1) == NULL) { | 491 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) |
| 495 return 2; | 492 return 2; |
| 496 } | 493 |
| 497 switch (*first_byte_ptr) { | 494 switch (*first_byte_ptr) { |
| 498 case RLE_MARKER: { | 495 case RLE_MARKER: { |
| 499 if (bmp_read_data(bmp_ptr, &first_byte_ptr, 1) == NULL) { | 496 if (!bmp_read_data(bmp_ptr, &first_byte_ptr, 1)) { |
| 500 bmp_ptr->skip_size = skip_size_org; | 497 bmp_ptr->skip_size = skip_size_org; |
| 501 return 2; | 498 return 2; |
| 502 } | 499 } |
| 503 switch (*first_byte_ptr) { | 500 switch (*first_byte_ptr) { |
| 504 case RLE_EOL: { | 501 case RLE_EOL: { |
| 505 if (bmp_ptr->row_num >= bmp_ptr->height) { | 502 if (bmp_ptr->row_num >= bmp_ptr->height) { |
| 506 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 503 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
| 507 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 504 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 508 return 0; | 505 return 0; |
| 509 } | 506 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 523 bmp_ptr, bmp_ptr->imgTB_flag | 520 bmp_ptr, bmp_ptr->imgTB_flag |
| 524 ? bmp_ptr->row_num++ | 521 ? bmp_ptr->row_num++ |
| 525 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 522 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
| 526 bmp_ptr->out_row_buffer); | 523 bmp_ptr->out_row_buffer); |
| 527 } | 524 } |
| 528 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); | 525 bmp_save_decoding_status(bmp_ptr, BMP_D_STATUS_TAIL); |
| 529 return 1; | 526 return 1; |
| 530 } | 527 } |
| 531 case RLE_DELTA: { | 528 case RLE_DELTA: { |
| 532 uint8_t* delta_ptr; | 529 uint8_t* delta_ptr; |
| 533 if (bmp_read_data(bmp_ptr, &delta_ptr, 2) == NULL) { | 530 if (!bmp_read_data(bmp_ptr, &delta_ptr, 2)) { |
| 534 bmp_ptr->skip_size = skip_size_org; | 531 bmp_ptr->skip_size = skip_size_org; |
| 535 return 2; | 532 return 2; |
| 536 } | 533 } |
| 537 bmp_ptr->col_num += (int32_t)delta_ptr[0]; | 534 bmp_ptr->col_num += (int32_t)delta_ptr[0]; |
| 538 int32_t bmp_row_num_next = bmp_ptr->row_num + (int32_t)delta_ptr[1]; | 535 int32_t bmp_row_num_next = bmp_ptr->row_num + (int32_t)delta_ptr[1]; |
| 539 if (bmp_ptr->col_num >= bmp_ptr->out_row_bytes || | 536 if (bmp_ptr->col_num >= bmp_ptr->out_row_bytes || |
| 540 bmp_row_num_next >= bmp_ptr->height) { | 537 bmp_row_num_next >= bmp_ptr->height) { |
| 541 bmp_error(bmp_ptr, "The Bmp File Is Corrupt Or Not Supported"); | 538 bmp_error(bmp_ptr, "The Bmp File Is Corrupt Or Not Supported"); |
| 542 return 0; | 539 return 0; |
| 543 } | 540 } |
| 544 while (bmp_ptr->row_num < bmp_row_num_next) { | 541 while (bmp_ptr->row_num < bmp_row_num_next) { |
| 545 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); | 542 FXSYS_memset(bmp_ptr->out_row_buffer, 0, bmp_ptr->out_row_bytes); |
| 546 bmp_ptr->bmp_get_row_fn( | 543 bmp_ptr->bmp_get_row_fn( |
| 547 bmp_ptr, bmp_ptr->imgTB_flag | 544 bmp_ptr, bmp_ptr->imgTB_flag |
| 548 ? bmp_ptr->row_num++ | 545 ? bmp_ptr->row_num++ |
| 549 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), | 546 : (bmp_ptr->height - 1 - bmp_ptr->row_num++), |
| 550 bmp_ptr->out_row_buffer); | 547 bmp_ptr->out_row_buffer); |
| 551 } | 548 } |
| 552 } break; | 549 } break; |
| 553 default: { | 550 default: { |
| 554 uint8_t size = (uint8_t)(((uint16_t)(*first_byte_ptr) + 1) >> 1); | 551 uint8_t size = (uint8_t)(((uint16_t)(*first_byte_ptr) + 1) >> 1); |
| 555 if ((int32_t)*first_byte_ptr >= | 552 if ((int32_t)*first_byte_ptr >= |
| 556 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { | 553 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { |
| 557 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { | 554 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { |
| 558 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 555 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 559 return 0; | 556 return 0; |
| 560 } | 557 } |
| 561 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; | 558 *first_byte_ptr = bmp_ptr->out_row_bytes - bmp_ptr->col_num - 1; |
| 562 } | 559 } |
| 563 if (bmp_read_data(bmp_ptr, &second_byte_ptr, | 560 if (!bmp_read_data(bmp_ptr, &second_byte_ptr, |
| 564 size & 1 ? size + 1 : size) == NULL) { | 561 size & 1 ? size + 1 : size)) { |
| 565 bmp_ptr->skip_size = skip_size_org; | 562 bmp_ptr->skip_size = skip_size_org; |
| 566 return 2; | 563 return 2; |
| 567 } | 564 } |
| 568 for (uint8_t i = 0; i < *first_byte_ptr; i++) { | 565 for (uint8_t i = 0; i < *first_byte_ptr; i++) { |
| 569 if (i & 0x01) { | 566 if (i & 0x01) { |
| 570 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = | 567 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = |
| 571 (*second_byte_ptr++ & 0x0F); | 568 (*second_byte_ptr++ & 0x0F); |
| 572 } else { | 569 } else { |
| 573 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = | 570 *(bmp_ptr->out_row_buffer + bmp_ptr->col_num++) = |
| 574 ((*second_byte_ptr & 0xF0) >> 4); | 571 ((*second_byte_ptr & 0xF0) >> 4); |
| 575 } | 572 } |
| 576 } | 573 } |
| 577 } | 574 } |
| 578 } | 575 } |
| 579 } break; | 576 } break; |
| 580 default: { | 577 default: { |
| 581 if (bmp_read_data(bmp_ptr, &second_byte_ptr, 1) == NULL) { | 578 if (!bmp_read_data(bmp_ptr, &second_byte_ptr, 1)) { |
| 582 bmp_ptr->skip_size = skip_size_org; | 579 bmp_ptr->skip_size = skip_size_org; |
| 583 return 2; | 580 return 2; |
| 584 } | 581 } |
| 585 if ((int32_t)*first_byte_ptr > | 582 if ((int32_t)*first_byte_ptr > |
| 586 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { | 583 bmp_ptr->out_row_bytes - bmp_ptr->col_num) { |
| 587 uint8_t size = (uint8_t)(((uint16_t)(*first_byte_ptr) + 1) >> 1); | 584 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) { | 585 if (size + (bmp_ptr->col_num >> 1) > bmp_ptr->src_row_bytes) { |
| 589 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); | 586 bmp_error(bmp_ptr, "The Bmp File Is Corrupt"); |
| 590 return 0; | 587 return 0; |
| 591 } | 588 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 602 } | 599 } |
| 603 } | 600 } |
| 604 } | 601 } |
| 605 } | 602 } |
| 606 bmp_error(bmp_ptr, "Any Uncontrol Error"); | 603 bmp_error(bmp_ptr, "Any Uncontrol Error"); |
| 607 return 0; | 604 return 0; |
| 608 } | 605 } |
| 609 uint8_t* bmp_read_data(bmp_decompress_struct_p bmp_ptr, | 606 uint8_t* bmp_read_data(bmp_decompress_struct_p bmp_ptr, |
| 610 uint8_t** des_buf_pp, | 607 uint8_t** des_buf_pp, |
| 611 uint32_t data_size) { | 608 uint32_t data_size) { |
| 612 if (bmp_ptr == NULL || bmp_ptr->avail_in < bmp_ptr->skip_size + data_size) { | 609 if (!bmp_ptr || bmp_ptr->avail_in < bmp_ptr->skip_size + data_size) |
| 613 return NULL; | 610 return NULL; |
| 614 } | 611 |
| 615 *des_buf_pp = bmp_ptr->next_in + bmp_ptr->skip_size; | 612 *des_buf_pp = bmp_ptr->next_in + bmp_ptr->skip_size; |
| 616 bmp_ptr->skip_size += data_size; | 613 bmp_ptr->skip_size += data_size; |
| 617 return *des_buf_pp; | 614 return *des_buf_pp; |
| 618 } | 615 } |
| 619 void bmp_save_decoding_status(bmp_decompress_struct_p bmp_ptr, int32_t status) { | 616 void bmp_save_decoding_status(bmp_decompress_struct_p bmp_ptr, int32_t status) { |
| 620 bmp_ptr->decode_status = status; | 617 bmp_ptr->decode_status = status; |
| 621 bmp_ptr->next_in += bmp_ptr->skip_size; | 618 bmp_ptr->next_in += bmp_ptr->skip_size; |
| 622 bmp_ptr->avail_in -= bmp_ptr->skip_size; | 619 bmp_ptr->avail_in -= bmp_ptr->skip_size; |
| 623 bmp_ptr->skip_size = 0; | 620 bmp_ptr->skip_size = 0; |
| 624 } | 621 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 if (bmp_ptr->info_header.biBitCount != 16 && | 698 if (bmp_ptr->info_header.biBitCount != 16 && |
| 702 bmp_ptr->info_header.biBitCount != 32) { | 699 bmp_ptr->info_header.biBitCount != 32) { |
| 703 return; | 700 return; |
| 704 } | 701 } |
| 705 uint32_t size, dst_pos, i; | 702 uint32_t size, dst_pos, i; |
| 706 size = bmp_ptr->src_pitch * bmp_ptr->src_row * | 703 size = bmp_ptr->src_pitch * bmp_ptr->src_row * |
| 707 bmp_ptr->info_header.biBitCount / 16; | 704 bmp_ptr->info_header.biBitCount / 16; |
| 708 dst_pos = bmp_ptr->file_header.bfOffBits; | 705 dst_pos = bmp_ptr->file_header.bfOffBits; |
| 709 dst_size += size; | 706 dst_size += size; |
| 710 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); | 707 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); |
| 711 if (dst_buf == NULL) { | |
| 712 return; | |
| 713 } | |
| 714 FXSYS_memset(&dst_buf[dst_pos], 0, size); | 708 FXSYS_memset(&dst_buf[dst_pos], 0, size); |
| 715 uint32_t mask_red; | 709 uint32_t mask_red; |
| 716 uint32_t mask_green; | 710 uint32_t mask_green; |
| 717 uint32_t mask_blue; | 711 uint32_t mask_blue; |
| 718 mask_red = 0x7C00; | 712 mask_red = 0x7C00; |
| 719 mask_green = 0x03E0; | 713 mask_green = 0x03E0; |
| 720 mask_blue = 0x001F; | 714 mask_blue = 0x001F; |
| 721 if (bmp_ptr->info_header.biCompression == BMP_BITFIELDS) { | 715 if (bmp_ptr->info_header.biCompression == BMP_BITFIELDS) { |
| 722 if (bmp_ptr->bit_type == BMP_BIT_565) { | 716 if (bmp_ptr->bit_type == BMP_BIT_565) { |
| 723 mask_red = 0xF800; | 717 mask_red = 0xF800; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 788 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); | 782 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); |
| 789 return; | 783 return; |
| 790 } | 784 } |
| 791 uint32_t size, dst_pos; | 785 uint32_t size, dst_pos; |
| 792 uint32_t dst_pitch = | 786 uint32_t dst_pitch = |
| 793 (bmp_ptr->src_width * bmp_ptr->info_header.biBitCount + 31) / 32 * 4; | 787 (bmp_ptr->src_width * bmp_ptr->info_header.biBitCount + 31) / 32 * 4; |
| 794 size = dst_pitch * bmp_ptr->src_row; | 788 size = dst_pitch * bmp_ptr->src_row; |
| 795 dst_pos = bmp_ptr->file_header.bfOffBits; | 789 dst_pos = bmp_ptr->file_header.bfOffBits; |
| 796 dst_size += size; | 790 dst_size += size; |
| 797 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); | 791 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); |
| 798 if (dst_buf == NULL) { | |
| 799 return; | |
| 800 } | |
| 801 FXSYS_memset(&dst_buf[dst_pos], 0, size); | 792 FXSYS_memset(&dst_buf[dst_pos], 0, size); |
| 802 for (int32_t row_num = bmp_ptr->src_row - 1; row_num > -1; row_num--) { | 793 for (int32_t row_num = bmp_ptr->src_row - 1; row_num > -1; row_num--) { |
| 803 FXSYS_memcpy(&dst_buf[dst_pos], | 794 FXSYS_memcpy(&dst_buf[dst_pos], |
| 804 &bmp_ptr->src_buf[row_num * bmp_ptr->src_pitch], | 795 &bmp_ptr->src_buf[row_num * bmp_ptr->src_pitch], |
| 805 bmp_ptr->src_pitch); | 796 bmp_ptr->src_pitch); |
| 806 dst_pos += dst_pitch; | 797 dst_pos += dst_pitch; |
| 807 } | 798 } |
| 808 dst_size = dst_pos; | 799 dst_size = dst_pos; |
| 809 } | 800 } |
| 810 static uint8_t bmp_rle8_search(const uint8_t* buf, int32_t len) { | 801 static uint8_t bmp_rle8_search(const uint8_t* buf, int32_t len) { |
| 811 uint8_t num; | 802 uint8_t num; |
| 812 num = 1; | 803 num = 1; |
| 813 while (num < len) { | 804 while (num < len) { |
| 814 if (buf[num - 1] != buf[num] || num == 0xFF) { | 805 if (buf[num - 1] != buf[num] || num == 0xFF) { |
| 815 break; | 806 break; |
| 816 } | 807 } |
| 817 num++; | 808 num++; |
| 818 } | 809 } |
| 819 return num; | 810 return num; |
| 820 } | 811 } |
| 821 static void bmp_encode_rle8(bmp_compress_struct_p bmp_ptr, | 812 static void bmp_encode_rle8(bmp_compress_struct_p bmp_ptr, |
| 822 uint8_t*& dst_buf, | 813 uint8_t*& dst_buf, |
| 823 uint32_t& dst_size) { | 814 uint32_t& dst_size) { |
| 824 uint32_t size, dst_pos, index; | 815 uint32_t size, dst_pos, index; |
| 825 uint8_t rle[2] = {0}; | 816 uint8_t rle[2] = {0}; |
| 826 size = bmp_ptr->src_pitch * bmp_ptr->src_row * 2; | 817 size = bmp_ptr->src_pitch * bmp_ptr->src_row * 2; |
| 827 dst_pos = bmp_ptr->file_header.bfOffBits; | 818 dst_pos = bmp_ptr->file_header.bfOffBits; |
| 828 dst_size += size; | 819 dst_size += size; |
| 829 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); | 820 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); |
| 830 if (dst_buf == NULL) { | |
| 831 return; | |
| 832 } | |
| 833 FXSYS_memset(&dst_buf[dst_pos], 0, size); | 821 FXSYS_memset(&dst_buf[dst_pos], 0, size); |
| 834 for (int32_t row_num = bmp_ptr->src_row - 1, i = 0; row_num > -1;) { | 822 for (int32_t row_num = bmp_ptr->src_row - 1, i = 0; row_num > -1;) { |
| 835 index = row_num * bmp_ptr->src_pitch; | 823 index = row_num * bmp_ptr->src_pitch; |
| 836 rle[0] = bmp_rle8_search(&bmp_ptr->src_buf[index + i], size - index - i); | 824 rle[0] = bmp_rle8_search(&bmp_ptr->src_buf[index + i], size - index - i); |
| 837 rle[1] = bmp_ptr->src_buf[index + i]; | 825 rle[1] = bmp_ptr->src_buf[index + i]; |
| 838 if (i + rle[0] >= (int32_t)bmp_ptr->src_pitch) { | 826 if (i + rle[0] >= (int32_t)bmp_ptr->src_pitch) { |
| 839 rle[0] = uint8_t(bmp_ptr->src_pitch - i); | 827 rle[0] = uint8_t(bmp_ptr->src_pitch - i); |
| 840 if (rle[0]) { | 828 if (rle[0]) { |
| 841 dst_buf[dst_pos++] = rle[0]; | 829 dst_buf[dst_pos++] = rle[0]; |
| 842 dst_buf[dst_pos++] = rle[1]; | 830 dst_buf[dst_pos++] = rle[1]; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 868 } | 856 } |
| 869 static void bmp_encode_rle4(bmp_compress_struct_p bmp_ptr, | 857 static void bmp_encode_rle4(bmp_compress_struct_p bmp_ptr, |
| 870 uint8_t*& dst_buf, | 858 uint8_t*& dst_buf, |
| 871 uint32_t& dst_size) { | 859 uint32_t& dst_size) { |
| 872 uint32_t size, dst_pos, index; | 860 uint32_t size, dst_pos, index; |
| 873 uint8_t rle[2] = {0}; | 861 uint8_t rle[2] = {0}; |
| 874 size = bmp_ptr->src_pitch * bmp_ptr->src_row; | 862 size = bmp_ptr->src_pitch * bmp_ptr->src_row; |
| 875 dst_pos = bmp_ptr->file_header.bfOffBits; | 863 dst_pos = bmp_ptr->file_header.bfOffBits; |
| 876 dst_size += size; | 864 dst_size += size; |
| 877 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); | 865 dst_buf = FX_Realloc(uint8_t, dst_buf, dst_size); |
| 878 if (dst_buf == NULL) { | |
| 879 return; | |
| 880 } | |
| 881 FXSYS_memset(&dst_buf[dst_pos], 0, size); | 866 FXSYS_memset(&dst_buf[dst_pos], 0, size); |
| 882 for (int32_t row_num = bmp_ptr->src_row - 1, i = 0; row_num > -1; | 867 for (int32_t row_num = bmp_ptr->src_row - 1, i = 0; row_num > -1; |
| 883 rle[1] = 0) { | 868 rle[1] = 0) { |
| 884 index = row_num * bmp_ptr->src_pitch; | 869 index = row_num * bmp_ptr->src_pitch; |
| 885 rle[0] = bmp_rle4_search(&bmp_ptr->src_buf[index + i], size - index - i); | 870 rle[0] = bmp_rle4_search(&bmp_ptr->src_buf[index + i], size - index - i); |
| 886 rle[1] |= (bmp_ptr->src_buf[index + i] & 0x0f) << 4; | 871 rle[1] |= (bmp_ptr->src_buf[index + i] & 0x0f) << 4; |
| 887 rle[1] |= bmp_ptr->src_buf[index + i + 1] & 0x0f; | 872 rle[1] |= bmp_ptr->src_buf[index + i + 1] & 0x0f; |
| 888 if (i + rle[0] >= (int32_t)bmp_ptr->src_pitch) { | 873 if (i + rle[0] >= (int32_t)bmp_ptr->src_pitch) { |
| 889 rle[0] = uint8_t(bmp_ptr->src_pitch - i); | 874 rle[0] = uint8_t(bmp_ptr->src_pitch - i); |
| 890 if (rle[0]) { | 875 if (rle[0]) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 909 uint8_t*& dst_buf, | 894 uint8_t*& dst_buf, |
| 910 uint32_t& dst_size) { | 895 uint32_t& dst_size) { |
| 911 uint32_t head_size = sizeof(BmpFileHeader) + sizeof(BmpInfoHeader); | 896 uint32_t head_size = sizeof(BmpFileHeader) + sizeof(BmpInfoHeader); |
| 912 uint32_t pal_size = sizeof(uint32_t) * bmp_ptr->pal_num; | 897 uint32_t pal_size = sizeof(uint32_t) * bmp_ptr->pal_num; |
| 913 if (bmp_ptr->info_header.biClrUsed > 0 && | 898 if (bmp_ptr->info_header.biClrUsed > 0 && |
| 914 bmp_ptr->info_header.biClrUsed < bmp_ptr->pal_num) { | 899 bmp_ptr->info_header.biClrUsed < bmp_ptr->pal_num) { |
| 915 pal_size = sizeof(uint32_t) * bmp_ptr->info_header.biClrUsed; | 900 pal_size = sizeof(uint32_t) * bmp_ptr->info_header.biClrUsed; |
| 916 } | 901 } |
| 917 dst_size = head_size + sizeof(uint32_t) * bmp_ptr->pal_num; | 902 dst_size = head_size + sizeof(uint32_t) * bmp_ptr->pal_num; |
| 918 dst_buf = FX_TryAlloc(uint8_t, dst_size); | 903 dst_buf = FX_TryAlloc(uint8_t, dst_size); |
| 919 if (dst_buf == NULL) { | 904 if (!dst_buf) |
| 920 return FALSE; | 905 return FALSE; |
| 921 } | 906 |
| 922 FXSYS_memset(dst_buf, 0, dst_size); | 907 FXSYS_memset(dst_buf, 0, dst_size); |
| 923 bmp_ptr->file_header.bfOffBits = head_size; | 908 bmp_ptr->file_header.bfOffBits = head_size; |
| 924 if (bmp_ptr->pal_ptr && pal_size) { | 909 if (bmp_ptr->pal_ptr && pal_size) { |
| 925 FXSYS_memcpy(&dst_buf[head_size], bmp_ptr->pal_ptr, pal_size); | 910 FXSYS_memcpy(&dst_buf[head_size], bmp_ptr->pal_ptr, pal_size); |
| 926 bmp_ptr->file_header.bfOffBits += pal_size; | 911 bmp_ptr->file_header.bfOffBits += pal_size; |
| 927 } | 912 } |
| 928 WriteInfoHeader(&bmp_ptr->info_header, dst_buf); | 913 WriteInfoHeader(&bmp_ptr->info_header, dst_buf); |
| 929 switch (bmp_ptr->info_header.biCompression) { | 914 switch (bmp_ptr->info_header.biCompression) { |
| 930 case BMP_RGB: | 915 case BMP_RGB: |
| 931 bmp_encode_rgb(bmp_ptr, dst_buf, dst_size); | 916 bmp_encode_rgb(bmp_ptr, dst_buf, dst_size); |
| 932 break; | 917 break; |
| 933 case BMP_BITFIELDS: | 918 case BMP_BITFIELDS: |
| 934 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); | 919 bmp_encode_bitfields(bmp_ptr, dst_buf, dst_size); |
| 935 break; | 920 break; |
| 936 case BMP_RLE8: | 921 case BMP_RLE8: |
| 937 bmp_encode_rle8(bmp_ptr, dst_buf, dst_size); | 922 bmp_encode_rle8(bmp_ptr, dst_buf, dst_size); |
| 938 break; | 923 break; |
| 939 case BMP_RLE4: | 924 case BMP_RLE4: |
| 940 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); | 925 bmp_encode_rle4(bmp_ptr, dst_buf, dst_size); |
| 941 break; | 926 break; |
| 942 default: | 927 default: |
| 943 break; | 928 break; |
| 944 } | 929 } |
| 945 bmp_ptr->file_header.bfSize = dst_size; | 930 bmp_ptr->file_header.bfSize = dst_size; |
| 946 WriteFileHeader(&bmp_ptr->file_header, dst_buf); | 931 WriteFileHeader(&bmp_ptr->file_header, dst_buf); |
| 947 return TRUE; | 932 return TRUE; |
| 948 } | 933 } |
| OLD | NEW |