Skip to content

Commit

Permalink
Fix incorrect output size estimate for very tall images
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
veluca93 committed May 22, 2023
1 parent b5c9ab1 commit 2c94cea
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions fpnge.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,34 @@ enum FPNGEOptionsPredictor {
FPNGE_PREDICTOR_BEST
};
struct FPNGEOptions {
char predictor; // FPNGEOptionsPredictor
char huffman_sample; // 0-127: how much of the image to sample
char cicp_colorspace; // FPNGECicpColorspace
char predictor; // FPNGEOptionsPredictor
char huffman_sample; // 0-127: how much of the image to sample
char cicp_colorspace; // FPNGECicpColorspace
};

#define FPNGE_COMPRESS_LEVEL_DEFAULT 4
#define FPNGE_COMPRESS_LEVEL_BEST 5
inline void FPNGEFillOptions(struct FPNGEOptions *options, int level,
int cicp_colorspace) {
if (level == 0)
level = FPNGE_COMPRESS_LEVEL_DEFAULT;
if (level == 0) level = FPNGE_COMPRESS_LEVEL_DEFAULT;
options->cicp_colorspace = cicp_colorspace;
options->huffman_sample = 1;
switch (level) {
case 1:
options->predictor = 2;
break;
case 2:
options->predictor = 4;
break;
case 3:
options->predictor = 5;
break;
case 5:
options->huffman_sample = 23;
// fall through
default:
options->predictor = 6;
break;
case 1:
options->predictor = 2;
break;
case 2:
options->predictor = 4;
break;
case 3:
options->predictor = 5;
break;
case 5:
options->huffman_sample = 23;
// fall through
default:
options->predictor = 6;
break;
}
}

Expand All @@ -74,7 +73,7 @@ inline size_t FPNGEOutputAllocSize(size_t bytes_per_channel,
size_t num_channels, size_t width,
size_t height) {
// likely an overestimate
return 1024 + 2 * bytes_per_channel * num_channels * width * height;
return 1024 + (2 * bytes_per_channel * width * num_channels + 1) * height;
}

#ifdef __cplusplus
Expand Down

0 comments on commit 2c94cea

Please sign in to comment.