Skip to content

Commit

Permalink
Add option for using PicCon compatible color conversion in 4 bit pale…
Browse files Browse the repository at this point in the history
…tte mode
  • Loading branch information
tditlu committed Jan 24, 2021
1 parent eb581f7 commit 4cb06aa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Available options are:

-a, --attached Attach sprites, only valid for sprite.

-n, --piccon Use PicCon compatible color conversion for 4 bit palette, only valid with palette output file format.

Examples:

amigeconv -f bitplanes -d 8 font.png font.raw
Expand Down
22 changes: 14 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.1"
#define AMIGECONV_VERSION_DATE "2019-09-01"
#define AMIGECONV_VERSION "1.0.2"
#define AMIGECONV_VERSION_DATE "2021-01-24"

typedef enum {
PALETTE_UNKNOWN = 0,
Expand Down Expand Up @@ -93,14 +93,15 @@ static bool write_palette(
image_t *const image,
palette_t palette,
const unsigned int colors,
bool copper
bool copper,
const bool piccon_compatibility
) {
buffer_t *buffer;
if (palette == PALETTE_PAL4) {
if (copper) {
buffer = palette_convert_pal4_copper(image, colors);
buffer = palette_convert_pal4_copper(image, colors, piccon_compatibility);
} else {
buffer = palette_convert_pal4(image, colors);
buffer = palette_convert_pal4(image, colors, piccon_compatibility);
}
}

Expand All @@ -117,7 +118,7 @@ static bool write_palette(
}

if (palette == PALETTE_LOADRGB4) {
buffer = palette_convert_pal4(image, colors);
buffer = palette_convert_pal4(image, colors, piccon_compatibility);
}

if (palette == PALETTE_LOADRGB32) {
Expand Down Expand Up @@ -146,11 +147,12 @@ static void usage() {
printf(" -w, --width [16,32,64] Width, only valid with sprite output file format.\n");
printf(" -t, --controlword Write control word, only valid with sprite output file format.\n");
printf(" -a, --attached Attach mode sprite, only valid with sprite output file format.\n");
printf(" -n, --piccon Use PicCon compatible color conversion for 4 bit palette, only valid with palette output file format.\n");
printf("\n");
}

int main(int argc, char *argv[]) {
bool interleaved = false, copper = false, controlword = false, attached = false;
bool interleaved = false, copper = false, controlword = false, attached = false, piccon_compatibility = false;
int depth = -1, colors = -1, width = -1;
palette_t palette = PALETTE_UNKNOWN;
format_t format = FORMAT_UNKNOWN;
Expand All @@ -165,6 +167,7 @@ int main(int argc, char *argv[]) {
{"width", required_argument, 0, 'w' },
{"controlword", no_argument, 0, 't' },
{"attached", no_argument, 0, 'a' },
{"piccon", no_argument, 0, 'n' },
{0, 0, 0, 0}
};

Expand Down Expand Up @@ -239,6 +242,9 @@ int main(int argc, char *argv[]) {
case 'a':
attached = true;
break;
case 'n':
piccon_compatibility = true;
break;
default:
usage();
exit(EXIT_FAILURE);
Expand Down Expand Up @@ -387,7 +393,7 @@ int main(int argc, char *argv[]) {
goto error;
}

if (!write_palette(outfile, &image, palette, colors, copper)) {
if (!write_palette(outfile, &image, palette, colors, copper, piccon_compatibility)) {
error = true;
printf("Error: Could not write output file \"%s\".\n\n", outfile);
goto error;
Expand Down
45 changes: 25 additions & 20 deletions src/formats/palette.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include <stdlib.h>
// #include <math.h>
#include <math.h>

#include "../buffer.h"
#include "../image.h"

// PAL4 & LOADRGB4 is the same format
buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors) {
buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors, const bool piccon_compatibility) {
if (colors < 1 || colors > 256) { return NULL; }

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 2;
Expand All @@ -18,15 +18,18 @@ buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors)
unsigned char g = buffer_get_byte(image->palette, i + 1);
unsigned char b = buffer_get_byte(image->palette, i + 2);

// Correct conversion
// r = round((float)r / 17.0);
// g = round((float)g / 17.0);
// b = round((float)b / 17.0);
if (piccon_compatibility) {
// PicCon compatibility
r >>= 4;
g >>= 4;
b >>= 4;
} else {
// Correct conversion
r = round((float)r / 17.0);
g = round((float)g / 17.0);
b = round((float)b / 17.0);
}

// PicCon compatibility
r >>= 4;
g >>= 4;
b >>= 4;

buffer_set_byte(buffer, j++, r);
buffer_set_byte(buffer, j++, (g << 4) + b);
Expand All @@ -35,7 +38,7 @@ buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors)
return buffer;
}

buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int colors) {
buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int colors, const bool piccon_compatibility) {
if (colors < 1 || colors > 32) { return NULL; }

unsigned int buffer_size = (((colors * 4) + (4 - 1)) >> 2) * 4;
Expand All @@ -48,15 +51,17 @@ buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int c
unsigned char g = buffer_get_byte(image->palette, i + 1);
unsigned char b = buffer_get_byte(image->palette, i + 2);

// Correct conversion
// r = round((float)r / 17.0);
// g = round((float)g / 17.0);
// b = round((float)b / 17.0);

// PicCon compatibility
r >>= 4;
g >>= 4;
b >>= 4;
if (piccon_compatibility) {
// PicCon compatibility
r >>= 4;
g >>= 4;
b >>= 4;
} else {
// Correct conversion
r = round((float)r / 17.0);
g = round((float)g / 17.0);
b = round((float)b / 17.0);
}

buffer_set_byte(buffer, j++, (reg >> 8) & 255);
buffer_set_byte(buffer, j++, reg & 255);
Expand Down
4 changes: 2 additions & 2 deletions src/formats/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

#include "../buffer.h"

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);
buffer_t *palette_convert_pal4(image_t *const image, const unsigned int colors, const bool piccon_compatibility);
buffer_t *palette_convert_pal4_copper(image_t *const image, const unsigned int colors, const bool piccon_compatibility);
buffer_t *palette_convert_pal8(image_t *const image, const unsigned int colors);
buffer_t *palette_convert_pal8_copper(image_t *const image, const unsigned int colors);
buffer_t *palette_convert_pal32(image_t *const image, const unsigned int colors);
Expand Down

0 comments on commit 4cb06aa

Please sign in to comment.