Skip to content

Commit

Permalink
Add support for depth when converting to chunky (nibbles)
Browse files Browse the repository at this point in the history
  • Loading branch information
tditlu committed Dec 26, 2024
1 parent 7665590 commit f483cf6
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
32 changes: 24 additions & 8 deletions src/amigeconv.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "formats/sprite.h"
#include "formats/palette.h"

#define AMIGECONV_VERSION "1.0.8"
#define AMIGECONV_VERSION_DATE "2024-09-25"
#define AMIGECONV_VERSION "1.0.9"
#define AMIGECONV_VERSION_DATE "2024-12-26"

typedef enum {
PALETTE_UNKNOWN = 0,
Expand All @@ -36,9 +36,10 @@ typedef enum {

static bool write_chunky(
const char *outfile,
image_t *const image
image_t *const image,
unsigned int depth
) {
buffer_t *buffer = chunky_convert(image);
buffer_t *buffer = chunky_convert(image, depth);
if (!buffer) { return false; }

const bool error = buffer_write(buffer, outfile);
Expand Down Expand Up @@ -318,10 +319,25 @@ int main(int argc, char *argv[]) {
goto error;
}

if (format == FORMAT_CHUNKY && !write_chunky(outfile, &image)) {
error = true;
printf("Error: Could not write output file \"%s\".\n\n", outfile);
goto error;
if (format == FORMAT_CHUNKY) {
if (depth == -1) { depth = 8; }
if (depth != 2 && depth != 4 && depth != 8) {
error = true;
printf("Error: Invalid depth specified.\n\n");
goto error;
}

if ((image.bitmap->size * depth % 8) != 0) {
error = true;
printf("Error: Invalid input file image size.\n\n");
goto error;
}

if (!write_chunky(outfile, &image, depth)) {
error = true;
printf("Error: Could not write output file \"%s\".\n\n", outfile);
goto error;
}
}

if (format == FORMAT_BITPLANE) {
Expand Down
33 changes: 28 additions & 5 deletions src/formats/chunky.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,38 @@
#include "../buffer.h"
#include "../image.h"

buffer_t *chunky_convert(image_t *const image) {
unsigned int buffer_size = image->bitmap->size;
buffer_t *chunky_convert(image_t *const image, const unsigned int depth) {
if (depth != 2 && depth != 4 && depth != 8) { return NULL; }
if ((image->bitmap->size * depth % 8) != 0) { return NULL; }

unsigned int buffer_size = (image->bitmap->size / 8) * depth;

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

for (unsigned int i = 0; i < buffer_size; i++) {
unsigned char c = buffer_get_byte(image->bitmap, i);
buffer_set_byte(buffer, i, c);
if (depth == 2) {
unsigned int i = 0;
for (unsigned int j = 0; j < buffer_size; j++) {
unsigned char c = ((buffer_get_byte(image->bitmap, i++) & 0x03) << 6);
c += ((buffer_get_byte(image->bitmap, i++) & 0x03) << 4);
c += ((buffer_get_byte(image->bitmap, i++) & 0x03) << 2);
c += (buffer_get_byte(image->bitmap, i++) & 0x03);
buffer_set_byte(buffer, j, c);
}
} else if (depth == 4) {
unsigned int i = 0;
for (unsigned int j = 0; j < buffer_size; j++) {
unsigned char c = ((buffer_get_byte(image->bitmap, i++) & 0x0f) << 4);
c += (buffer_get_byte(image->bitmap, i++) & 0x0f);
buffer_set_byte(buffer, j, c);
}
} else {
for (unsigned int i = 0; i < buffer_size; i++) {
unsigned char c = buffer_get_byte(image->bitmap, i);
buffer_set_byte(buffer, i, c);
}
}


return buffer;
}
2 changes: 1 addition & 1 deletion src/formats/chunky.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

#include "../buffer.h"

buffer_t *chunky_convert(image_t *const image);
buffer_t *chunky_convert(image_t *const image, const unsigned int depth);

#endif /* _FORMATS_CHUNKY_H */
Binary file added test/data/extra/random2.chk
Binary file not shown.
Binary file added test/data/extra/random4.chk
Binary file not shown.
Binary file added test/data/extra/random8.chk
Binary file not shown.
12 changes: 12 additions & 0 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ printf "\033[0;33mCommand:\033[0m amigeconv -f chunky data/random8.png output/am
../bin/amigeconv -f chunky data/random8.png output/amigeconv/random8.chk
test output/amigeconv/random8.chk data/piccon/random8.chk

printf "\033[0;33mCommand:\033[0m amigeconv -f chunky -d 8 data/random8.png output/amigeconv/random8_d.chk\n"
../bin/amigeconv -f chunky -d 8 data/random8.png output/amigeconv/random8_d.chk
test output/amigeconv/random8_d.chk data/extra/random8.chk

printf "\033[0;33mCommand:\033[0m amigeconv -f chunky -d 4 data/random4.png output/amigeconv/random4.chk\n"
../bin/amigeconv -f chunky -d 4 data/random4.png output/amigeconv/random4.chk
test output/amigeconv/random4.chk data/extra/random4.chk

printf "\033[0;33mCommand:\033[0m amigeconv -f chunky -d 2 data/random2.png output/amigeconv/random2.chk\n"
../bin/amigeconv -f chunky -d 2 data/random2.png output/amigeconv/random2.chk
test output/amigeconv/random2.chk data/extra/random2.chk

printf "\033[0;33mCommand:\033[0m amigeconv -f bitplane -d 8 data/random8.png output/amigeconv/random8.raw\n"
../bin/amigeconv -f bitplane -d 8 data/random8.png output/amigeconv/random8.raw
test output/amigeconv/random8.raw data/piccon/random8.raw
Expand Down

0 comments on commit f483cf6

Please sign in to comment.