mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 02:32:06 +00:00
@@ -22,4 +22,5 @@ void output_tilemap_file(const struct Options *opts,
|
|||||||
const struct Tilemap *tilemap);
|
const struct Tilemap *tilemap);
|
||||||
void output_palette_file(const struct Options *opts,
|
void output_palette_file(const struct Options *opts,
|
||||||
const struct RawIndexedImage *raw_image);
|
const struct RawIndexedImage *raw_image);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
15
src/gfx/gb.c
15
src/gfx/gb.c
@@ -42,13 +42,8 @@ void raw_to_gb(const struct RawIndexedImage *raw_image, struct GBImage *gb)
|
|||||||
index = raw_image->data[y][x];
|
index = raw_image->data[y][x];
|
||||||
index &= (1 << depth) - 1;
|
index &= (1 << depth) - 1;
|
||||||
|
|
||||||
if (!gb->horizontal) {
|
|
||||||
byte = y * depth
|
byte = y * depth
|
||||||
+ x / 8 * raw_image->height / 8 * 8 * depth;
|
+ x / 8 * raw_image->height / 8 * 8 * depth;
|
||||||
} else {
|
|
||||||
byte = y * depth
|
|
||||||
+ x / 8 * raw_image->height / 8 * 8 * depth;
|
|
||||||
}
|
|
||||||
gb->data[byte] |= (index & 1) << (7 - x % 8);
|
gb->data[byte] |= (index & 1) << (7 - x % 8);
|
||||||
if (depth == 2) {
|
if (depth == 2) {
|
||||||
gb->data[byte + 1] |=
|
gb->data[byte + 1] |=
|
||||||
@@ -177,12 +172,12 @@ void output_palette_file(const struct Options *opts,
|
|||||||
uint8_t cur_bytes[2];
|
uint8_t cur_bytes[2];
|
||||||
|
|
||||||
f = fopen(opts->palfile, "wb");
|
f = fopen(opts->palfile, "wb");
|
||||||
if (!f) {
|
if (!f)
|
||||||
err(1, "Opening palette file '%s' failed",
|
err(1, "Opening palette file '%s' failed", opts->palfile);
|
||||||
opts->palfile);
|
|
||||||
}
|
|
||||||
for (i = 0; i < raw_image->num_colors; i++) {
|
for (i = 0; i < raw_image->num_colors; i++) {
|
||||||
color = raw_image->palette[i].blue >> 3 << 10 |
|
color =
|
||||||
|
raw_image->palette[i].blue >> 3 << 10 |
|
||||||
raw_image->palette[i].green >> 3 << 5 |
|
raw_image->palette[i].green >> 3 << 5 |
|
||||||
raw_image->palette[i].red >> 3;
|
raw_image->palette[i].red >> 3;
|
||||||
cur_bytes[0] = color & 0xFF;
|
cur_bytes[0] = color & 0xFF;
|
||||||
|
|||||||
@@ -131,9 +131,9 @@ int main(int argc, char *argv[])
|
|||||||
if (png_options.trim)
|
if (png_options.trim)
|
||||||
opts.trim = png_options.trim;
|
opts.trim = png_options.trim;
|
||||||
|
|
||||||
|
|
||||||
if (raw_image->width % 8 || raw_image->height % 8) {
|
if (raw_image->width % 8 || raw_image->height % 8) {
|
||||||
errx(1, "Input PNG file %s not sized correctly. "
|
errx(1, "Input PNG file %s not sized correctly. The image's width and height must be multiples of 8.",
|
||||||
"The image's width and height must be multiples of 8.",
|
|
||||||
opts.infile);
|
opts.infile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,9 +236,8 @@ int main(int argc, char *argv[])
|
|||||||
if (*opts.palfile)
|
if (*opts.palfile)
|
||||||
output_palette_file(&opts, raw_image);
|
output_palette_file(&opts, raw_image);
|
||||||
|
|
||||||
if (opts.fix || opts.debug) {
|
if (opts.fix || opts.debug)
|
||||||
output_png_file(&opts, &png_options, raw_image);
|
output_png_file(&opts, &png_options, raw_image);
|
||||||
}
|
|
||||||
|
|
||||||
destroy_raw_image(&raw_image);
|
destroy_raw_image(&raw_image);
|
||||||
free(gb.data);
|
free(gb.data);
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ struct RawIndexedImage *input_png_file(const struct Options *opts,
|
|||||||
|
|
||||||
if (img.depth != depth) {
|
if (img.depth != depth) {
|
||||||
if (opts->verbose) {
|
if (opts->verbose) {
|
||||||
warnx("Image bit depth is not %i (is %i).", depth, img.depth);
|
warnx("Image bit depth is not %i (is %i).",
|
||||||
|
depth, img.depth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ struct RawIndexedImage *input_png_file(const struct Options *opts,
|
|||||||
case PNG_COLOR_TYPE_RGB_ALPHA:
|
case PNG_COLOR_TYPE_RGB_ALPHA:
|
||||||
raw_image = truecolor_png_to_raw(&img); break;
|
raw_image = truecolor_png_to_raw(&img); break;
|
||||||
default:
|
default:
|
||||||
/* Shouldn't happen, but might as well handle it just in case. */
|
/* Shouldn't happen, but might as well handle just in case. */
|
||||||
errx(1, "Input PNG file is of invalid color type.");
|
errx(1, "Input PNG file is of invalid color type.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +92,8 @@ void output_png_file(const struct Options *opts,
|
|||||||
if (!f)
|
if (!f)
|
||||||
err(1, "Opening output png file '%s' failed", outfile);
|
err(1, "Opening output png file '%s' failed", outfile);
|
||||||
|
|
||||||
img.png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
img.png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
|
||||||
|
NULL, NULL, NULL);
|
||||||
if (!img.png)
|
if (!img.png)
|
||||||
errx(1, "Creating png structure failed");
|
errx(1, "Creating png structure failed");
|
||||||
|
|
||||||
@@ -99,7 +101,6 @@ void output_png_file(const struct Options *opts,
|
|||||||
if (!img.info)
|
if (!img.info)
|
||||||
errx(1, "Creating png info structure failed");
|
errx(1, "Creating png info structure failed");
|
||||||
|
|
||||||
/* TODO: Better error handling here? */
|
|
||||||
if (setjmp(png_jmpbuf(img.png)))
|
if (setjmp(png_jmpbuf(img.png)))
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
@@ -118,9 +119,8 @@ void output_png_file(const struct Options *opts,
|
|||||||
png_set_PLTE(img.png, img.info, png_palette, raw_image->num_colors);
|
png_set_PLTE(img.png, img.info, png_palette, raw_image->num_colors);
|
||||||
free(png_palette);
|
free(png_palette);
|
||||||
|
|
||||||
if (opts->fix) {
|
if (opts->fix)
|
||||||
set_text(&img, png_options);
|
set_text(&img, png_options);
|
||||||
}
|
|
||||||
|
|
||||||
png_write_info(img.png, img.info);
|
png_write_info(img.png, img.info);
|
||||||
|
|
||||||
@@ -139,9 +139,9 @@ void destroy_raw_image(struct RawIndexedImage **raw_image_ptr_ptr)
|
|||||||
int y;
|
int y;
|
||||||
struct RawIndexedImage *raw_image = *raw_image_ptr_ptr;
|
struct RawIndexedImage *raw_image = *raw_image_ptr_ptr;
|
||||||
|
|
||||||
for (y = 0; y < raw_image->height; y++) {
|
for (y = 0; y < raw_image->height; y++)
|
||||||
free(raw_image->data[y]);
|
free(raw_image->data[y]);
|
||||||
}
|
|
||||||
free(raw_image->data);
|
free(raw_image->data);
|
||||||
free(raw_image->palette);
|
free(raw_image->palette);
|
||||||
free(raw_image);
|
free(raw_image);
|
||||||
@@ -150,7 +150,8 @@ void destroy_raw_image(struct RawIndexedImage **raw_image_ptr_ptr)
|
|||||||
|
|
||||||
static void initialize_png(struct PNGImage *img, FILE *f)
|
static void initialize_png(struct PNGImage *img, FILE *f)
|
||||||
{
|
{
|
||||||
img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
img->png = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
||||||
|
NULL, NULL, NULL);
|
||||||
if (!img->png)
|
if (!img->png)
|
||||||
errx(1, "Creating png structure failed");
|
errx(1, "Creating png structure failed");
|
||||||
|
|
||||||
@@ -158,10 +159,8 @@ static void initialize_png(struct PNGImage *img, FILE *f)
|
|||||||
if (!img->info)
|
if (!img->info)
|
||||||
errx(1, "Creating png info structure failed");
|
errx(1, "Creating png info structure failed");
|
||||||
|
|
||||||
/* TODO: Better error handling here? */
|
if (setjmp(png_jmpbuf(img->png)))
|
||||||
if (setjmp(png_jmpbuf(img->png))) {
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
png_init_io(img->png, f);
|
png_init_io(img->png, f);
|
||||||
|
|
||||||
@@ -173,7 +172,6 @@ static void initialize_png(struct PNGImage *img, FILE *f)
|
|||||||
img->type = png_get_color_type(img->png, img->info);
|
img->type = png_get_color_type(img->png, img->info);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void read_png(struct PNGImage *img);
|
static void read_png(struct PNGImage *img);
|
||||||
static struct RawIndexedImage *create_raw_image(int width, int height,
|
static struct RawIndexedImage *create_raw_image(int width, int height,
|
||||||
int num_colors);
|
int num_colors);
|
||||||
@@ -193,9 +191,8 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
|
|||||||
uint8_t *old_to_new_palette;
|
uint8_t *old_to_new_palette;
|
||||||
int i, x, y;
|
int i, x, y;
|
||||||
|
|
||||||
if (img->depth < 8) {
|
if (img->depth < 8)
|
||||||
png_set_packing(img->png);
|
png_set_packing(img->png);
|
||||||
}
|
|
||||||
|
|
||||||
png_get_PLTE(img->png, img->info, &palette, &colors_in_PLTE);
|
png_get_PLTE(img->png, img->info, &palette, &colors_in_PLTE);
|
||||||
|
|
||||||
@@ -219,7 +216,8 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
|
|||||||
old_to_new_palette[i] = 0;
|
old_to_new_palette[i] = 0;
|
||||||
} else {
|
} else {
|
||||||
old_to_new_palette[i] = colors_in_new_palette;
|
old_to_new_palette[i] = colors_in_new_palette;
|
||||||
palette[colors_in_new_palette++] = original_palette[i];
|
palette[colors_in_new_palette++] =
|
||||||
|
original_palette[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (i = num_trans; i < colors_in_PLTE; i++) {
|
for (i = num_trans; i < colors_in_PLTE; i++) {
|
||||||
@@ -229,19 +227,23 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
|
|||||||
|
|
||||||
if (colors_in_new_palette != colors_in_PLTE) {
|
if (colors_in_new_palette != colors_in_PLTE) {
|
||||||
palette = realloc(palette,
|
palette = realloc(palette,
|
||||||
sizeof(png_color) * colors_in_new_palette);
|
sizeof(png_color) *
|
||||||
|
colors_in_new_palette);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Setting and validating palette before reading allows us to error out
|
* Setting and validating palette before reading
|
||||||
* *before* doing the data transformation if the palette is too long.
|
* allows us to error out *before* doing the data
|
||||||
|
* transformation if the palette is too long.
|
||||||
*/
|
*/
|
||||||
set_raw_image_palette(raw_image, palette, colors_in_new_palette);
|
set_raw_image_palette(raw_image, palette,
|
||||||
|
colors_in_new_palette);
|
||||||
read_png(img);
|
read_png(img);
|
||||||
|
|
||||||
for (y = 0; y < img->height; y++) {
|
for (y = 0; y < img->height; y++) {
|
||||||
for (x = 0; x < img->width; x++) {
|
for (x = 0; x < img->width; x++) {
|
||||||
raw_image->data[y][x] = old_to_new_palette[img->data[y][x]];
|
raw_image->data[y][x] =
|
||||||
|
old_to_new_palette[img->data[y][x]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,28 +253,29 @@ static struct RawIndexedImage *indexed_png_to_raw(struct PNGImage *img)
|
|||||||
read_png(img);
|
read_png(img);
|
||||||
|
|
||||||
for (y = 0; y < img->height; y++) {
|
for (y = 0; y < img->height; y++) {
|
||||||
for (x = 0; x < img->width; x++) {
|
for (x = 0; x < img->width; x++)
|
||||||
raw_image->data[y][x] = img->data[y][x];
|
raw_image->data[y][x] = img->data[y][x];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return raw_image;
|
return raw_image;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct RawIndexedImage *grayscale_png_to_raw(struct PNGImage *img)
|
static struct RawIndexedImage *grayscale_png_to_raw(struct PNGImage *img)
|
||||||
{
|
{
|
||||||
if (img->depth < 8) {
|
if (img->depth < 8)
|
||||||
png_set_expand_gray_1_2_4_to_8(img->png);
|
png_set_expand_gray_1_2_4_to_8(img->png);
|
||||||
}
|
|
||||||
png_set_gray_to_rgb(img->png);
|
png_set_gray_to_rgb(img->png);
|
||||||
return truecolor_png_to_raw(img);
|
return truecolor_png_to_raw(img);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rgba_png_palette(struct PNGImage *img,
|
static void rgba_png_palette(struct PNGImage *img,
|
||||||
png_color **palette_ptr_ptr, int *num_colors);
|
png_color **palette_ptr_ptr, int *num_colors);
|
||||||
static struct RawIndexedImage *processed_rgba_png_to_raw(
|
static struct RawIndexedImage
|
||||||
struct PNGImage *img, const png_color *palette, int colors_in_palette);
|
*processed_rgba_png_to_raw(const struct PNGImage *img,
|
||||||
|
const png_color *palette,
|
||||||
|
int colors_in_palette);
|
||||||
|
|
||||||
static struct RawIndexedImage *truecolor_png_to_raw(struct PNGImage *img)
|
static struct RawIndexedImage *truecolor_png_to_raw(struct PNGImage *img)
|
||||||
{
|
{
|
||||||
@@ -289,12 +292,11 @@ static struct RawIndexedImage *truecolor_png_to_raw(struct PNGImage *img)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!(img->type & PNG_COLOR_MASK_ALPHA)) {
|
if (!(img->type & PNG_COLOR_MASK_ALPHA)) {
|
||||||
if (png_get_valid(img->png, img->info, PNG_INFO_tRNS)) {
|
if (png_get_valid(img->png, img->info, PNG_INFO_tRNS))
|
||||||
png_set_tRNS_to_alpha(img->png);
|
png_set_tRNS_to_alpha(img->png);
|
||||||
} else {
|
else
|
||||||
png_set_add_alpha(img->png, 0xFF, PNG_FILLER_AFTER);
|
png_set_add_alpha(img->png, 0xFF, PNG_FILLER_AFTER);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
read_png(img);
|
read_png(img);
|
||||||
|
|
||||||
@@ -314,12 +316,11 @@ static void rgba_build_palette(struct PNGImage *img,
|
|||||||
static void rgba_png_palette(struct PNGImage *img,
|
static void rgba_png_palette(struct PNGImage *img,
|
||||||
png_color **palette_ptr_ptr, int *num_colors)
|
png_color **palette_ptr_ptr, int *num_colors)
|
||||||
{
|
{
|
||||||
if (png_get_valid(img->png, img->info, PNG_INFO_PLTE)) {
|
if (png_get_valid(img->png, img->info, PNG_INFO_PLTE))
|
||||||
return rgba_PLTE_palette(img, palette_ptr_ptr, num_colors);
|
return rgba_PLTE_palette(img, palette_ptr_ptr, num_colors);
|
||||||
} else {
|
else
|
||||||
return rgba_build_palette(img, palette_ptr_ptr, num_colors);
|
return rgba_build_palette(img, palette_ptr_ptr, num_colors);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void rgba_PLTE_palette(struct PNGImage *img,
|
static void rgba_PLTE_palette(struct PNGImage *img,
|
||||||
png_color **palette_ptr_ptr, int *num_colors)
|
png_color **palette_ptr_ptr, int *num_colors)
|
||||||
@@ -327,12 +328,15 @@ static void rgba_PLTE_palette(struct PNGImage *img,
|
|||||||
png_get_PLTE(img->png, img->info, palette_ptr_ptr, num_colors);
|
png_get_PLTE(img->png, img->info, palette_ptr_ptr, num_colors);
|
||||||
/*
|
/*
|
||||||
* Lets us free the palette manually instead of leaving it to libpng,
|
* Lets us free the palette manually instead of leaving it to libpng,
|
||||||
* which lets us handle a PLTE palette and a built palette the same way.
|
* which lets us handle a PLTE and a built palette the same way.
|
||||||
*/
|
*/
|
||||||
png_data_freer(img->png, img->info,
|
png_data_freer(img->png, img->info,
|
||||||
PNG_USER_WILL_FREE_DATA, PNG_FREE_PLTE);
|
PNG_USER_WILL_FREE_DATA, PNG_FREE_PLTE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void update_built_palette(png_color *palette,
|
||||||
|
const png_color *pixel_color, png_byte alpha,
|
||||||
|
int *num_colors, bool *only_grayscale);
|
||||||
static int fit_grayscale_palette(png_color *palette, int *num_colors);
|
static int fit_grayscale_palette(png_color *palette, int *num_colors);
|
||||||
static void order_color_palette(png_color *palette, int num_colors);
|
static void order_color_palette(png_color *palette, int num_colors);
|
||||||
|
|
||||||
@@ -340,12 +344,10 @@ static void rgba_build_palette(struct PNGImage *img,
|
|||||||
png_color **palette_ptr_ptr, int *num_colors)
|
png_color **palette_ptr_ptr, int *num_colors)
|
||||||
{
|
{
|
||||||
png_color *palette;
|
png_color *palette;
|
||||||
int y, value_index, i;
|
int y, value_index;
|
||||||
png_color cur_pixel_color;
|
png_color cur_pixel_color;
|
||||||
png_byte cur_alpha;
|
png_byte cur_alpha;
|
||||||
bool only_grayscale = true;
|
bool only_grayscale = true;
|
||||||
bool color_exists;
|
|
||||||
png_color cur_palette_color;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* By filling the palette up with black by default, if the image
|
* By filling the palette up with black by default, if the image
|
||||||
@@ -363,55 +365,65 @@ static void rgba_build_palette(struct PNGImage *img,
|
|||||||
cur_pixel_color.blue = img->data[y][value_index++];
|
cur_pixel_color.blue = img->data[y][value_index++];
|
||||||
cur_alpha = img->data[y][value_index++];
|
cur_alpha = img->data[y][value_index++];
|
||||||
|
|
||||||
/*
|
update_built_palette(palette, &cur_pixel_color,
|
||||||
* Transparent pixels don't count toward the palette,
|
cur_alpha,
|
||||||
* as they'll be replaced with color #0 later.
|
num_colors, &only_grayscale);
|
||||||
*/
|
|
||||||
if (cur_alpha == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (only_grayscale &&
|
|
||||||
!(cur_pixel_color.red == cur_pixel_color.green &&
|
|
||||||
cur_pixel_color.red == cur_pixel_color.blue)) {
|
|
||||||
only_grayscale = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
color_exists = false;
|
|
||||||
for (i = 0; i < *num_colors; i++) {
|
|
||||||
cur_palette_color = palette[i];
|
|
||||||
if (cur_pixel_color.red == cur_palette_color.red &&
|
|
||||||
cur_pixel_color.green == cur_palette_color.green &&
|
|
||||||
cur_pixel_color.blue == cur_palette_color.blue) {
|
|
||||||
color_exists = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!color_exists) {
|
|
||||||
if (*num_colors == colors) {
|
|
||||||
err(1, "Too many colors in input PNG file to fit into a "
|
|
||||||
"%d-bit palette (max %d).", depth, colors);
|
|
||||||
}
|
|
||||||
palette[*num_colors] = cur_pixel_color;
|
|
||||||
(*num_colors)++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* In order not to count 100% transparent images as grayscale. */
|
/* In order not to count 100% transparent images as grayscale. */
|
||||||
only_grayscale = *num_colors ? only_grayscale : false;
|
only_grayscale = *num_colors ? only_grayscale : false;
|
||||||
|
|
||||||
if (!only_grayscale || !fit_grayscale_palette(palette, num_colors)) {
|
if (!only_grayscale || !fit_grayscale_palette(palette, num_colors))
|
||||||
order_color_palette(palette, *num_colors);
|
order_color_palette(palette, *num_colors);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void update_built_palette(png_color *palette,
|
||||||
|
const png_color *pixel_color, png_byte alpha,
|
||||||
|
int *num_colors, bool *only_grayscale)
|
||||||
|
{
|
||||||
|
bool color_exists;
|
||||||
|
png_color cur_palette_color;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Transparent pixels don't count toward the palette,
|
||||||
|
* as they'll be replaced with color #0 later.
|
||||||
|
*/
|
||||||
|
if (alpha == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (*only_grayscale && !(pixel_color->red == pixel_color->green &&
|
||||||
|
pixel_color->red == pixel_color->blue)) {
|
||||||
|
*only_grayscale = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
color_exists = false;
|
||||||
|
for (i = 0; i < *num_colors; i++) {
|
||||||
|
cur_palette_color = palette[i];
|
||||||
|
if (pixel_color->red == cur_palette_color.red &&
|
||||||
|
pixel_color->green == cur_palette_color.green &&
|
||||||
|
pixel_color->blue == cur_palette_color.blue) {
|
||||||
|
color_exists = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!color_exists) {
|
||||||
|
if (*num_colors == colors) {
|
||||||
|
err(1, "Too many colors in input PNG file to fit into a %d-bit palette (max %d).",
|
||||||
|
depth, colors);
|
||||||
|
}
|
||||||
|
palette[*num_colors] = *pixel_color;
|
||||||
|
(*num_colors)++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int fit_grayscale_palette(png_color *palette, int *num_colors)
|
static int fit_grayscale_palette(png_color *palette, int *num_colors)
|
||||||
{
|
{
|
||||||
int i, shade_index;
|
|
||||||
int interval = 256 / colors;
|
int interval = 256 / colors;
|
||||||
png_color *fitted_palette = malloc(sizeof(png_color) * colors);
|
png_color *fitted_palette = malloc(sizeof(png_color) * colors);
|
||||||
bool *set_indices = calloc(colors, sizeof(bool));
|
bool *set_indices = calloc(colors, sizeof(bool));
|
||||||
|
int i, shade_index;
|
||||||
|
|
||||||
fitted_palette[0].red = 0xFF;
|
fitted_palette[0].red = 0xFF;
|
||||||
fitted_palette[0].green = 0xFF;
|
fitted_palette[0].green = 0xFF;
|
||||||
@@ -439,9 +451,8 @@ static int fit_grayscale_palette(png_color *palette, int *num_colors)
|
|||||||
set_indices[shade_index] = true;
|
set_indices[shade_index] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < colors; i++) {
|
for (i = 0; i < colors; i++)
|
||||||
palette[i] = fitted_palette[i];
|
palette[i] = fitted_palette[i];
|
||||||
}
|
|
||||||
|
|
||||||
*num_colors = colors;
|
*num_colors = colors;
|
||||||
|
|
||||||
@@ -460,16 +471,16 @@ static int compare_luminance(const void *a, const void *b)
|
|||||||
{
|
{
|
||||||
struct ColorWithLuminance *x = (struct ColorWithLuminance *)a;
|
struct ColorWithLuminance *x = (struct ColorWithLuminance *)a;
|
||||||
struct ColorWithLuminance *y = (struct ColorWithLuminance *)b;
|
struct ColorWithLuminance *y = (struct ColorWithLuminance *)b;
|
||||||
|
|
||||||
return y->luminance - x->luminance;
|
return y->luminance - x->luminance;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void order_color_palette(png_color *palette, int num_colors)
|
static void order_color_palette(png_color *palette, int num_colors)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
struct ColorWithLuminance *palette_with_luminance;
|
struct ColorWithLuminance *palette_with_luminance =
|
||||||
|
|
||||||
palette_with_luminance =
|
|
||||||
malloc(sizeof(struct ColorWithLuminance) * num_colors);
|
malloc(sizeof(struct ColorWithLuminance) * num_colors);
|
||||||
|
|
||||||
for (i = 0; i < num_colors; i++) {
|
for (i = 0; i < num_colors; i++) {
|
||||||
/*
|
/*
|
||||||
* Normally this would be done with floats, but since it's only
|
* Normally this would be done with floats, but since it's only
|
||||||
@@ -482,22 +493,25 @@ static void order_color_palette(png_color *palette, int num_colors)
|
|||||||
}
|
}
|
||||||
qsort(palette_with_luminance, num_colors,
|
qsort(palette_with_luminance, num_colors,
|
||||||
sizeof(struct ColorWithLuminance), compare_luminance);
|
sizeof(struct ColorWithLuminance), compare_luminance);
|
||||||
for (i = 0; i < num_colors; i++) {
|
for (i = 0; i < num_colors; i++)
|
||||||
palette[i] = palette_with_luminance[i].color;
|
palette[i] = palette_with_luminance[i].color;
|
||||||
}
|
|
||||||
free(palette_with_luminance);
|
free(palette_with_luminance);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t palette_index_of(const png_color *palette, int num_colors,
|
static void put_raw_image_pixel(struct RawIndexedImage *raw_image,
|
||||||
const png_color *color);
|
const struct PNGImage *img,
|
||||||
|
int *value_index, int x, int y,
|
||||||
|
const png_color *palette,
|
||||||
|
int colors_in_palette);
|
||||||
|
|
||||||
static struct RawIndexedImage *processed_rgba_png_to_raw(
|
static struct RawIndexedImage
|
||||||
struct PNGImage *img, const png_color *palette, int colors_in_palette)
|
*processed_rgba_png_to_raw(const struct PNGImage *img,
|
||||||
|
const png_color *palette,
|
||||||
|
int colors_in_palette)
|
||||||
{
|
{
|
||||||
struct RawIndexedImage *raw_image;
|
struct RawIndexedImage *raw_image;
|
||||||
int x, y, value_index;
|
int x, y, value_index;
|
||||||
png_color cur_color;
|
|
||||||
png_byte cur_alpha;
|
|
||||||
|
|
||||||
raw_image = create_raw_image(img->width, img->height, colors);
|
raw_image = create_raw_image(img->width, img->height, colors);
|
||||||
|
|
||||||
@@ -508,18 +522,9 @@ static struct RawIndexedImage *processed_rgba_png_to_raw(
|
|||||||
value_index = img->width * 4 - 1;
|
value_index = img->width * 4 - 1;
|
||||||
|
|
||||||
while (x >= 0) {
|
while (x >= 0) {
|
||||||
cur_alpha = img->data[y][value_index];
|
put_raw_image_pixel(raw_image, img,
|
||||||
if (cur_alpha == 0) {
|
&value_index, x, y,
|
||||||
raw_image->data[y][x] = 0;
|
palette, colors_in_palette);
|
||||||
value_index -= 4;
|
|
||||||
} else {
|
|
||||||
value_index--;
|
|
||||||
cur_color.blue = img->data[y][value_index--];
|
|
||||||
cur_color.green = img->data[y][value_index--];
|
|
||||||
cur_color.red = img->data[y][value_index--];
|
|
||||||
raw_image->data[y][x] =
|
|
||||||
palette_index_of(palette, colors_in_palette, &cur_color);
|
|
||||||
}
|
|
||||||
x--;
|
x--;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -527,8 +532,35 @@ static struct RawIndexedImage *processed_rgba_png_to_raw(
|
|||||||
return raw_image;
|
return raw_image;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t palette_index_of(const png_color *palette, int num_colors,
|
static uint8_t palette_index_of(const png_color *palette,
|
||||||
const png_color *color)
|
int num_colors, const png_color *color);
|
||||||
|
|
||||||
|
static void put_raw_image_pixel(struct RawIndexedImage *raw_image,
|
||||||
|
const struct PNGImage *img,
|
||||||
|
int *value_index, int x, int y,
|
||||||
|
const png_color *palette,
|
||||||
|
int colors_in_palette)
|
||||||
|
{
|
||||||
|
png_color pixel_color;
|
||||||
|
png_byte alpha;
|
||||||
|
|
||||||
|
alpha = img->data[y][*value_index];
|
||||||
|
if (alpha == 0) {
|
||||||
|
raw_image->data[y][x] = 0;
|
||||||
|
*value_index -= 4;
|
||||||
|
} else {
|
||||||
|
(*value_index)--;
|
||||||
|
pixel_color.blue = img->data[y][(*value_index)--];
|
||||||
|
pixel_color.green = img->data[y][(*value_index)--];
|
||||||
|
pixel_color.red = img->data[y][(*value_index)--];
|
||||||
|
raw_image->data[y][x] = palette_index_of(palette,
|
||||||
|
colors_in_palette,
|
||||||
|
&pixel_color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t palette_index_of(const png_color *palette,
|
||||||
|
int num_colors, const png_color *color)
|
||||||
{
|
{
|
||||||
uint8_t i;
|
uint8_t i;
|
||||||
|
|
||||||
@@ -539,8 +571,7 @@ static uint8_t palette_index_of(const png_color *palette, int num_colors,
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
errx(1, "The input PNG file contains colors that don't appear "
|
errx(1, "The input PNG file contains colors that don't appear in its embedded palette.");
|
||||||
"in its embedded palette.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_png(struct PNGImage *img)
|
static void read_png(struct PNGImage *img)
|
||||||
@@ -550,9 +581,8 @@ static void read_png(struct PNGImage *img)
|
|||||||
png_read_update_info(img->png, img->info);
|
png_read_update_info(img->png, img->info);
|
||||||
|
|
||||||
img->data = malloc(sizeof(png_byte *) * img->height);
|
img->data = malloc(sizeof(png_byte *) * img->height);
|
||||||
for (y = 0; y < img->height; y++) {
|
for (y = 0; y < img->height; y++)
|
||||||
img->data[y] = malloc(png_get_rowbytes(img->png, img->info));
|
img->data[y] = malloc(png_get_rowbytes(img->png, img->info));
|
||||||
}
|
|
||||||
|
|
||||||
png_read_image(img->png, img->data);
|
png_read_image(img->png, img->data);
|
||||||
png_read_end(img->png, img->info);
|
png_read_end(img->png, img->info);
|
||||||
@@ -573,9 +603,8 @@ static struct RawIndexedImage *create_raw_image(int width, int height,
|
|||||||
raw_image->palette = malloc(sizeof(struct RGBColor) * num_colors);
|
raw_image->palette = malloc(sizeof(struct RGBColor) * num_colors);
|
||||||
|
|
||||||
raw_image->data = malloc(sizeof(uint8_t *) * height);
|
raw_image->data = malloc(sizeof(uint8_t *) * height);
|
||||||
for (y = 0; y < height; y++) {
|
for (y = 0; y < height; y++)
|
||||||
raw_image->data[y] = malloc(sizeof(uint8_t) * width);
|
raw_image->data[y] = malloc(sizeof(uint8_t) * width);
|
||||||
}
|
|
||||||
|
|
||||||
return raw_image;
|
return raw_image;
|
||||||
}
|
}
|
||||||
@@ -586,9 +615,9 @@ static void set_raw_image_palette(struct RawIndexedImage *raw_image,
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (num_colors > raw_image->num_colors) {
|
if (num_colors > raw_image->num_colors) {
|
||||||
errx(1, "Too many colors in input PNG file's palette to fit into "
|
errx(1, "Too many colors in input PNG file's palette to fit into a %d-bit palette (%d in input palette, max %d).",
|
||||||
"a %d-bit palette (%d in input palette, max %d).",
|
raw_image->num_colors >> 1,
|
||||||
raw_image->num_colors >> 1, num_colors, raw_image->num_colors);
|
num_colors, raw_image->num_colors);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < num_colors; i++) {
|
for (i = 0; i < num_colors; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user