diff --git a/src/formats/bitplane.c b/src/formats/bitplane.c index 6d13db6..1760cac 100644 --- a/src/formats/bitplane.c +++ b/src/formats/bitplane.c @@ -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; } @@ -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; } diff --git a/src/formats/chunky.c b/src/formats/chunky.c index 717fcde..c576f2d 100644 --- a/src/formats/chunky.c +++ b/src/formats/chunky.c @@ -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); } diff --git a/src/formats/palette.c b/src/formats/palette.c index c5a6004..e622f2d 100644 --- a/src/formats/palette.c +++ b/src/formats/palette.c @@ -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; } @@ -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; } @@ -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; } @@ -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; @@ -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; } @@ -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; } diff --git a/src/formats/sprite.c b/src/formats/sprite.c index dd73c42..b0eb5ae 100644 --- a/src/formats/sprite.c +++ b/src/formats/sprite.c @@ -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; } @@ -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; }