use the correct png type when reading header width and height

This commit is contained in:
Vincent Sanders 2016-07-15 14:37:54 +01:00
parent 78d45e9b82
commit d03f0ee04e

View File

@ -56,176 +56,76 @@ static int raw_hot_x, raw_hot_y;
#define B_AT(x,y) *(PPIX_AT(x,y) + B_OFF) #define B_AT(x,y) *(PPIX_AT(x,y) + B_OFF)
#define A_AT(x,y) *(PPIX_AT(x,y) + A_OFF) #define A_AT(x,y) *(PPIX_AT(x,y) + A_OFF)
static void info_callback(png_structp png, png_infop info);
static void row_callback(png_structp png, png_bytep new_row,
png_uint_32 row_num, int pass);
static void end_callback(png_structp png, png_infop info);
static void static void
usage(void) usage(void)
{ {
fprintf(stderr, "usage: fb_convert_image input.png output.inc varname\n"); fprintf(stderr, "usage: fb_convert_image input.png output.inc varname\n");
} }
static void info_callback(png_structp png, png_infop info);
static void row_callback(png_structp png, png_bytep new_row,
png_uint_32 row_num, int pass);
static void end_callback(png_structp png, png_infop info);
static void static void
detect_hotspot(void) detect_hotspot(void)
{ {
int i; int i;
int greenpixels = 0; int greenpixels = 0;
for (i = 0; i < raw_width; ++i) {
if (A_AT(i, 0) == 255) {
if (G_AT(i, 0) == 255) {
greenpixels++;
raw_hot_x = i;
}
if ((B_AT(i, 0) != 0) || (R_AT(i, 0) != 0)) {
is_cursor = false;
return;
}
} else if (A_AT(i, 0) != 0) {
is_cursor = false;
return;
}
}
if (greenpixels != 1) {
is_cursor = false;
return;
}
for (i = 0; i < raw_height; ++i) { for (i = 0; i < raw_width; ++i) {
if (A_AT(0, i) == 255) { if (A_AT(i, 0) == 255) {
if (G_AT(0, i) == 255) { if (G_AT(i, 0) == 255) {
greenpixels++; greenpixels++;
raw_hot_y = i; raw_hot_x = i;
} }
if ((B_AT(0, i) != 0) || (R_AT(0, i) != 0)) { if ((B_AT(i, 0) != 0) || (R_AT(i, 0) != 0)) {
is_cursor = false; is_cursor = false;
return; return;
} }
} else if (A_AT(0, i) != 0) { } else if (A_AT(i, 0) != 0) {
is_cursor = false; is_cursor = false;
return; return;
} }
} }
if (greenpixels != 2) { if (greenpixels != 1) {
is_cursor = false; is_cursor = false;
return; return;
} }
printf(" Pointer detected. Adjusted hotspot at %d, %d (0-based)\n",
raw_hot_x - 1, raw_hot_y - 1); for (i = 0; i < raw_height; ++i) {
if (A_AT(0, i) == 255) {
if (G_AT(0, i) == 255) {
greenpixels++;
raw_hot_y = i;
}
if ((B_AT(0, i) != 0) || (R_AT(0, i) != 0)) {
is_cursor = false;
return;
}
} else if (A_AT(0, i) != 0) {
is_cursor = false;
return;
}
}
if (greenpixels != 2) {
is_cursor = false;
return;
}
printf(" Pointer detected. Adjusted hotspot at %d, %d (0-based)\n",
raw_hot_x - 1, raw_hot_y - 1);
} }
int
main(int argc, char **argv)
{
FILE *f;
unsigned char buffer[1024];
int br;
int x, y, c;
if (argc != 4) {
usage();
return 1;
}
printf(" CONVERT: %s (%s)\n", argv[1], argv[3]);
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
info = png_create_info_struct(png);
png_set_progressive_read_fn(png, NULL, info_callback, row_callback, end_callback);
f = fopen(argv[1], "rb");
if (f == NULL) {
printf(" Unable to open %s\n", argv[1]);
return 1;
}
do {
br = fread(buffer, 1, 1024, f);
if (br > 0) {
png_process_data(png, info, buffer, br);
}
} while (br > 0);
if (br < 0) {
printf("Error reading input: %s\n", strerror(errno));
fclose(f);
return 1;
}
fclose(f);
detect_hotspot();
f = fopen(argv[2], "w");
if (f == NULL) {
printf(" Unable to open %s\n", argv[2]);
return 2;
}
fprintf(f, "/* This file is auto-generated from %s\n", argv[1]);
fprintf(f, " *\n * Do not edit this file directly.\n */\n\n");
fprintf(f, "#include <sys/types.h>\n\n");
fprintf(f, "#include <stdint.h>\n\n");
fprintf(f, "#include <stdbool.h>\n\n");
fprintf(f, "#include <libnsfb.h>\n\n");
fprintf(f, "#include \"desktop/plot_style.h\"\n");
fprintf(f, "#include \"framebuffer/gui.h\"\n");
fprintf(f, "#include \"framebuffer/fbtk.h\"\n\n");
fprintf(f, "static uint8_t %s_pixdata[] = {\n", argv[3]);
for (y = 0; y < HEIGHT; ++y) {
unsigned char *rowptr = bitmap_data + (rowstride * y);
if (is_cursor) {
/* If it's a cursor, skip one row and one column */
rowptr += rowstride + 4;
}
fprintf(f, "\t");
for (x = 0; x < WIDTH; ++x) {
for (c = 0; c < 4; ++c) {
unsigned char b = *rowptr++;
fprintf(f, "0x%02x, ", b);
}
}
fprintf(f, "\n");
}
fprintf(f, "};\n\n");
fprintf(f, "struct fbtk_bitmap %s = {\n", argv[3]);
fprintf(f, "\t.width\t\t= %d,\n", WIDTH);
fprintf(f, "\t.height\t\t= %d,\n", HEIGHT);
fprintf(f, "\t.hot_x\t\t= %d,\n", HOT_X);
fprintf(f, "\t.hot_y\t\t= %d,\n", HOT_Y);
fprintf(f, "\t.pixdata\t= %s_pixdata,\n", argv[3]);
fprintf(f, "};\n\n");
fclose(f);
return 0;
}
static void static void
info_callback(png_structp png, png_infop info) info_callback(png_structp png, png_infop info)
{ {
int bit_depth, color_type, interlace, intent; int bit_depth, color_type, interlace, intent;
double gamma; double gamma;
unsigned long width, height; png_uint_32 width, height;
/* Read the PNG details */ /* Read the PNG details */
png_get_IHDR(png, info, &width, &height, &bit_depth, png_get_IHDR(png, info, &width, &height, &bit_depth,
&color_type, &interlace, 0, 0); &color_type, &interlace, 0, 0);
/* Set up our transformations */ /* Set up our transformations */
if (color_type == PNG_COLOR_TYPE_PALETTE) if (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png); png_set_palette_to_rgb(png);
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
@ -244,12 +144,12 @@ info_callback(png_structp png, png_infop info)
* see http://www.w3.org/Graphics/PNG/all_seven.html for a test case * see http://www.w3.org/Graphics/PNG/all_seven.html for a test case
*/ */
if (png_get_sRGB(png, info, &intent)) if (png_get_sRGB(png, info, &intent))
png_set_gamma(png, 2.2, 0.45455); png_set_gamma(png, 2.2, 0.45455);
else { else {
if (png_get_gAMA(png, info, &gamma)) if (png_get_gAMA(png, info, &gamma))
png_set_gamma(png, 2.2, gamma); png_set_gamma(png, 2.2, gamma);
else else
png_set_gamma(png, 2.2, 0.45455); png_set_gamma(png, 2.2, 0.45455);
} }
@ -259,9 +159,9 @@ info_callback(png_structp png, png_infop info)
interlace = (interlace == PNG_INTERLACE_ADAM7); interlace = (interlace == PNG_INTERLACE_ADAM7);
raw_width = width; raw_width = width;
raw_height = height; raw_height = height;
rowstride = raw_width * 4; rowstride = raw_width * 4;
bitmap_data = malloc(rowstride * raw_height); bitmap_data = malloc(rowstride * raw_height);
} }
static unsigned int interlace_start[8] = {0, 16, 0, 8, 0, 4, 0}; static unsigned int interlace_start[8] = {0, 16, 0, 8, 0, 4, 0};
@ -271,33 +171,33 @@ static unsigned int interlace_row_step[8] = {8, 8, 8, 4, 4, 2, 2};
static void static void
row_callback(png_structp png, png_bytep new_row, row_callback(png_structp png, png_bytep new_row,
png_uint_32 row_num, int pass) png_uint_32 row_num, int pass)
{ {
unsigned long i, j; unsigned long i, j;
unsigned int start, step; unsigned int start, step;
unsigned char *row = bitmap_data + (rowstride * row_num); unsigned char *row = bitmap_data + (rowstride * row_num);
if (new_row == 0) if (new_row == 0)
return; return;
if (interlace) { if (interlace) {
start = interlace_start[pass]; start = interlace_start[pass];
step = interlace_step[pass]; step = interlace_step[pass];
row_num = interlace_row_start[pass] + row_num = interlace_row_start[pass] +
interlace_row_step[pass] * row_num; interlace_row_step[pass] * row_num;
/* Copy the data to our current row taking interlacing /* Copy the data to our current row taking interlacing
* into consideration */ * into consideration */
row = bitmap_data + (rowstride * row_num); row = bitmap_data + (rowstride * row_num);
for (j = 0, i = start; i < rowbytes; i += step) { for (j = 0, i = start; i < rowbytes; i += step) {
row[i++] = new_row[j++]; row[i++] = new_row[j++];
row[i++] = new_row[j++]; row[i++] = new_row[j++];
row[i++] = new_row[j++]; row[i++] = new_row[j++];
row[i++] = new_row[j++]; row[i++] = new_row[j++];
} }
} else { } else {
memcpy(row, new_row, rowbytes); memcpy(row, new_row, rowbytes);
} }
} }
static void static void
@ -306,10 +206,98 @@ end_callback(png_structp png, png_infop info)
} }
int
main(int argc, char **argv)
{
FILE *f;
unsigned char buffer[1024];
int br;
int x, y, c;
if (argc != 4) {
usage();
return 1;
}
printf(" CONVERT: %s (%s)\n", argv[1], argv[3]);
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
info = png_create_info_struct(png);
png_set_progressive_read_fn(png, NULL, info_callback, row_callback, end_callback);
f = fopen(argv[1], "rb");
if (f == NULL) {
printf(" Unable to open %s\n", argv[1]);
return 1;
}
do {
br = fread(buffer, 1, 1024, f);
if (br > 0) {
png_process_data(png, info, buffer, br);
}
} while (br > 0);
if (br < 0) {
printf("Error reading input: %s\n", strerror(errno));
fclose(f);
return 1;
}
fclose(f);
detect_hotspot();
f = fopen(argv[2], "w");
if (f == NULL) {
printf(" Unable to open %s\n", argv[2]);
return 2;
}
fprintf(f, "/* This file is auto-generated from %s\n", argv[1]);
fprintf(f, " *\n * Do not edit this file directly.\n */\n\n");
fprintf(f, "#include <sys/types.h>\n\n");
fprintf(f, "#include <stdint.h>\n\n");
fprintf(f, "#include <stdbool.h>\n\n");
fprintf(f, "#include <libnsfb.h>\n\n");
fprintf(f, "#include \"desktop/plot_style.h\"\n");
fprintf(f, "#include \"framebuffer/gui.h\"\n");
fprintf(f, "#include \"framebuffer/fbtk.h\"\n\n");
fprintf(f, "static uint8_t %s_pixdata[] = {\n", argv[3]);
for (y = 0; y < HEIGHT; ++y) {
unsigned char *rowptr = bitmap_data + (rowstride * y);
if (is_cursor) {
/* If it's a cursor, skip one row and one column */
rowptr += rowstride + 4;
}
fprintf(f, "\t");
for (x = 0; x < WIDTH; ++x) {
for (c = 0; c < 4; ++c) {
unsigned char b = *rowptr++;
fprintf(f, "0x%02x, ", b);
}
}
fprintf(f, "\n");
}
fprintf(f, "};\n\n");
fprintf(f, "struct fbtk_bitmap %s = {\n", argv[3]);
fprintf(f, "\t.width\t\t= %d,\n", WIDTH);
fprintf(f, "\t.height\t\t= %d,\n", HEIGHT);
fprintf(f, "\t.hot_x\t\t= %d,\n", HOT_X);
fprintf(f, "\t.hot_y\t\t= %d,\n", HOT_Y);
fprintf(f, "\t.pixdata\t= %s_pixdata,\n", argv[3]);
fprintf(f, "};\n\n");
fclose(f);
return 0;
}
/* /*
* Local Variables: * Local Variables:
* c-basic-offset:8 * c-basic-offset:8
* End: * End:
*/ */