Skip to content

Commit

Permalink
Fix buffer size calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Gerdgren committed Jun 3, 2019
1 parent c187e25 commit b631153
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 180 deletions.
22 changes: 2 additions & 20 deletions src/formats/bitplane.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@
buffer_t *bitplane_convert(image_t *const image, const unsigned int depth) {
if (image->width % 8 != 0) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int j = 0; j < depth; j++) {
for (unsigned int y = 0; y < image->height; y++) {
for (unsigned int x = 0; x < image->width; x += 8) {
buffer_size++;
}
}
}

unsigned int buffer_size = depth * image->height * ((image->width + (8 - 1)) >> 3);
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand All @@ -38,16 +29,7 @@ buffer_t *bitplane_convert(image_t *const image, const unsigned int depth) {
buffer_t *bitplane_convert_interleaved(image_t *const image, const unsigned int depth) {
if (image->width % 8 != 0) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int y = 0; y < image->height; y++) {
for (unsigned int j = 0; j < depth; j++) {
for (unsigned int x = 0; x < image->width; x += 8) {
buffer_size++;
}
}
}

unsigned int buffer_size = image->height * depth * ((image->width + (8 - 1)) >> 3);
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down
11 changes: 2 additions & 9 deletions src/formats/chunky.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@
#include "../image.h"

buffer_t *chunky_convert(image_t *const image) {
unsigned int size = image->bitmap->size;

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < size; i++) {
buffer_size++;
}

unsigned int buffer_size = image->bitmap->size;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

for (unsigned int i = 0; i < size; i++) {
for (unsigned int i = 0; i < buffer_size; i++) {
unsigned char c = buffer_get_byte(image->bitmap, i);
buffer_set_byte(buffer, i, c);
}
Expand Down
103 changes: 9 additions & 94 deletions src/formats/palette.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@
buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors) {
if (colors < 1 || colors > 256) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < colors * 4; i += 4) {
buffer_size++;
buffer_size++;
}

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 2;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down Expand Up @@ -44,15 +38,7 @@ buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors)
buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int colors) {
if (colors < 1 || colors > 32) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < colors * 4; i += 4) {
buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;
}

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 4;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down Expand Up @@ -85,15 +71,7 @@ buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int c
buffer_t *palette_convert_pal8(image_t *const image, const unsigned int colors) {
if (colors < 1 || colors > 256) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < colors * 4; i += 4) {
buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;
}

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 4;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand All @@ -117,36 +95,12 @@ buffer_t *palette_convert_pal8_copper(image_t *const image, const unsigned int c

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < colors; i += 32) {
buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;

for (unsigned int h = 0; h < 32; h++) { // High
if ((i + h) >= colors) { break; }

buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;
}

buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;

for (unsigned int l = 0; l < 32; l++) { // Low
if ((i + l) >= colors) { break; }

buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;
for (unsigned int i = 0; i < colors; i += 32, buffer_size += 8) {
for (unsigned int j = 0; j < 32; j++) {
if ((i + j) >= colors) { break; }
buffer_size += 8;
}
}

buffer_t *buffer = buffer_create(buffer_size);

unsigned int j = 0;
Expand Down Expand Up @@ -204,22 +158,7 @@ buffer_t *palette_convert_pal8_copper(image_t *const image, const unsigned int c
buffer_t *palette_convert_pal32(image_t *const image, const unsigned int colors) {
if (colors < 1 || colors > 256) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int i = 0; i < colors * 4; i += 4) {
for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}

for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}

for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}
}

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 4 * 3;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down Expand Up @@ -248,31 +187,7 @@ buffer_t *palette_convert_pal32(image_t *const image, const unsigned int colors)
buffer_t *palette_convert_loadrgb32(image_t *const image, const unsigned int colors) {
if (colors < 1 || colors > 256) { return NULL; }

// Calculate needed buffer size
unsigned int buffer_size = 0;
buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;
for (unsigned int i = 0; i < colors * 4; i += 4) {
for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}

for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}

for (unsigned int k = 0; k < 4; k++) {
buffer_size++;
}
}
buffer_size++;
buffer_size++;
buffer_size++;
buffer_size++;


unsigned int buffer_size = ((((colors * 4) + (4 - 1)) >> 2) * 4 * 3) + 8;
buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down
63 changes: 6 additions & 57 deletions src/formats/sprite.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,10 @@ buffer_t *sprite_convert(image_t *const image, const unsigned int width, const b

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int x = 0; x < image->width; x += width) {
if (controlword) {
for (unsigned int i = 0; i < width >> 2; i++) {
buffer_size++;
}
}
for (unsigned int y = 0; y < image->height; y++) {
for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}

for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}
}
if (controlword) {
for (unsigned int i = 0; i < width >> 2; i++) {
buffer_size++;
}
}
for (unsigned int i = 0; i < image->width; i += width) {
if (controlword) { buffer_size += (width >> 2) * 2; }
buffer_size += image->height * (((width + (8 - 1)) >> 3) * 2);
}

buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down Expand Up @@ -75,43 +57,10 @@ buffer_t *sprite_convert_attached(image_t *const image, const unsigned int width

// Calculate needed buffer size
unsigned int buffer_size = 0;
for (unsigned int x = 0; x < image->width; x += width) {
if (controlword) {
for (unsigned int i = 0; i < width >> 2; i++) {
buffer_size++;
}
}
for (unsigned int y = 0; y < image->height; y++) {
for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}

for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}
}
if (controlword) {
for (unsigned int i = 0; i < width >> 2; i++) {
buffer_size++;
buffer_size++;
}
}
for (unsigned int y = 0; y < image->height; y++) {
for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}

for (unsigned int i = 0; i < width; i += 8) {
buffer_size++;
}
}
if (controlword) {
for (unsigned int i = 0; i < width >> 2; i++) {
buffer_size++;
}
}
for (unsigned int i = 0; i < image->width; i += width) {
if (controlword) { buffer_size += (width >> 2) * 4; }
buffer_size += image->height * (((width + (8 - 1)) >> 3) * 4);
}

buffer_t *buffer = buffer_create(buffer_size);
if (!buffer) { return NULL; }

Expand Down

0 comments on commit b631153

Please sign in to comment.