linux executable for LoadFromSD.ino example, issue #1059

This commit is contained in:
kraus 2019-11-24 09:18:00 +01:00
parent a884907a67
commit 1a1b73ea94
12 changed files with 435 additions and 0 deletions

15
tools/png2bin/Makefile Normal file
View File

@ -0,0 +1,15 @@
CFLAGS = -g -Wall
#CFLAGS = -O4 -Wall
SRC = png2bin.c
OBJ = $(SRC:.c=.o)
png2bin: $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o png2bin -lpng
clean:
-rm $(OBJ) png2bin
test:
./png2bin *.png

View File

@ -0,0 +1,2 @@
gcc png2bin.c -lpng -o png2bin.exe

Binary file not shown.

BIN
tools/png2bin/png2bin Executable file

Binary file not shown.

359
tools/png2bin/png2bin.c Normal file
View File

@ -0,0 +1,359 @@
/*
png2bin.c
Convert a png file to the internal file format, suitable for
https://github.com/olikraus/u8g2/tree/master/sys/arduino/u8g2_page_buffer/LoadFromSD
Copyright (c) 2019, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
http://www.libpng.org/pub/png/libpng-manual.txt
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#define PNG_DEBUG 3
#include <png.h>
/*================================================*/
int is_invert = 0;
int is_16bit = 0;
int is_preview = 0;
/*================================================*/
int width, height;
png_byte color_type;
png_byte bit_depth;
uint8_t min, max;
png_structp png_ptr;
png_infop info_ptr;
png_bytep * row_pointers;
void print_info(const char* file_name)
{
printf("%s: png_get_image_width(png_ptr, info_ptr)=%d\n", file_name, png_get_image_width(png_ptr, info_ptr));
printf("%s: png_get_image_height(png_ptr, info_ptr)=%d\n", file_name, png_get_image_height(png_ptr, info_ptr));
/*
#define PNG_COLOR_MASK_PALETTE 1
#define PNG_COLOR_MASK_COLOR 2
#define PNG_COLOR_MASK_ALPHA 4
#define PNG_COLOR_TYPE_GRAY 0
#define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
#define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
#define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
*/
printf("%s: png_get_color_type(png_ptr, info_ptr)=%d\n", file_name, png_get_color_type(png_ptr, info_ptr));
printf("%s: png_get_bit_depth(png_ptr, info_ptr)=%d\n", file_name, png_get_bit_depth(png_ptr, info_ptr));
printf("%s: png_get_rowbytes(png_ptr, info_ptr)=%lu\n", file_name, png_get_rowbytes(png_ptr, info_ptr));
printf("%s: range: %d .. %d\n", file_name, min, max);
}
void print_short_info(const char* file_name)
{
printf("%s rgb=%s alpha=%s w=%d h=%d\n",
file_name,
color_type&2?"no":"yes",
color_type&4?"no":"yes",
width, height);
}
uint8_t get_gray(int x, int y)
{
uint8_t *p;
uint8_t g;
if ( color_type & PNG_COLOR_MASK_ALPHA )
{
p = row_pointers[y] + x*4;
g = ((int)p[0] + (int)p[1] + (int)p[2])/3;
//g = ((int)g+(int)p[3])/2;
return g;
}
p = row_pointers[y] + x*3;
return ((int)p[0] + (int)p[1] + (int)p[2])/3;
}
uint8_t get_pixel(int x, int y)
{
if ( x < 0 )
return 0;
if ( x >= width )
return 0;
if ( y < 0 )
return 0;
if ( y >= height )
return 0;
if ( get_gray(x, y) > 128/2 )
return is_invert == 0 ? 1 : 0;
return is_invert == 0 ? 0 : 1;
}
uint8_t get_pixel_byte(int x, int y)
{
uint8_t b = 0;
int i;
for( i = 0; i < 8; i++ )
{
b |= get_pixel(x + i,y)<<i; // lsb contains the leftmost pixel
}
return b;
}
void write_bdf_bitmap(const char *filename)
{
int x, y;
FILE *bin_fp;
bin_fp = fopen(filename, "wb");
if ( bin_fp == NULL )
{
perror(filename);
return;
}
if ( is_16bit )
{
fputc(width>>8, bin_fp);
fputc(width&255, bin_fp);
fputc(height>>8, bin_fp);
fputc(height&255, bin_fp);
}
else
{
fputc(width, bin_fp);
fputc(height, bin_fp);
}
for( y = 0; y < height; y++ )
{
x = 0;
while( x < width )
{
fputc(get_pixel_byte(x,y), bin_fp);
x+=8;
}
}
fclose(bin_fp);
}
void show_ascii(void)
{
int x, y;
for( y = 0; y < height; y++ )
{
for( x = 0; x < width; x++ )
{
if ( is_invert )
{
if ( get_gray(x, y) <= 128/2 )
printf("#");
else
printf(".");
}
else
{
if ( get_gray(x, y) > 128/2 )
printf("#");
else
printf(".");
}
}
printf("\n");
}
}
void calc_min_max(void)
{
int x, y;
uint8_t val;
min = 255;
max = 0;
for( y = 0; y < height; y++ )
{
for( x = 0; x < width; x++ )
{
val = get_gray(x, y);
if ( max < val )
max = val;
if ( min > val )
min = val;
}
}
}
int read_png_file(const char* file_name, const char* bin_name)
{
unsigned char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (fp == NULL)
{
perror(file_name);
return 0; /* open error */
}
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
return fclose(fp), 0; /* not a png file error */
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL)
return fclose(fp), 0; /* alloc read struct */
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL )
return fclose(fp), 0; /* alloc info struct */
if (setjmp(png_jmpbuf(png_ptr)))
{
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return fclose(fp), 0; /* library error */
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
png_read_png(png_ptr, info_ptr,
PNG_TRANSFORM_SCALE_16|
//PNG_TRANSFORM_STRIP_ALPHA|
PNG_TRANSFORM_PACKING|
//PNG_TRANSFORM_INVERT_ALPHA |
//PNG_TRANSFORM_INVERT_MONO |
PNG_TRANSFORM_GRAY_TO_RGB
, NULL);
width = png_get_image_width(png_ptr, info_ptr);
height = png_get_image_height(png_ptr, info_ptr);
color_type = png_get_color_type(png_ptr, info_ptr);
bit_depth = png_get_bit_depth(png_ptr, info_ptr);
row_pointers = png_get_rows(png_ptr, info_ptr);
//calc_min_max();
//print_info(file_name);
print_short_info(file_name);
if ( is_preview )
show_ascii();
write_bdf_bitmap(bin_name);
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(fp);
return 1;
}
void help(void)
{
printf("png2bin [options] png-file bin-file\n");
printf("Convert a png image to a binary \"XBM\" image, prefixed by hieght and length value.\n");
printf("png-file format: RGB, gray, 1-bit index are supported, alpha channel is ignored, \n");
printf("options:\n");
printf(" -i Invert image\n");
printf(" -p Preview image as ASCII art\n");
printf(" -2 Two byte heigh/length value (16 bit values instead of 8 bit values, high byte first)\n");
}
int main(int argc, char **argv)
{
if ( argc < 3 )
{
help();
return 0;
}
argc--; argv++;
for(;;)
{
if ( argv[0][0] == '-' )
{
if ( strcmp(argv[0], "-i") == 0 )
{
argc--; argv++;
is_invert = 1;
}
if ( strcmp(argv[0], "-2") == 0 )
{
argc--; argv++;
is_16bit = 1;
}
if ( strcmp(argv[0], "-p") == 0 )
{
argc--; argv++;
is_preview = 1;
}
}
else
{
break;
}
}
if ( argv[0] != NULL && argv[1] != NULL )
{
if ( strcmp(argv[0], argv[1]) == 0 )
{
printf("source and destination file must be different\n");
}
else
{
read_png_file(argv[0], argv[1]);
}
}
else
{
help();
}
return 0;
}

View File

@ -0,0 +1,59 @@
#define u8g2_logo_97x51_width 97
#define u8g2_logo_97x51_height 51
static unsigned char u8g2_logo_97x51_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00,
0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe,
0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xcf, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x83, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x00, 0x81, 0x07, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xfc, 0x03,
0x1c, 0x00, 0x3e, 0x1c, 0xc0, 0x03, 0x00, 0x00, 0x3c, 0x80, 0x07, 0xff,
0x07, 0x7f, 0x80, 0xff, 0x3f, 0xe0, 0x01, 0x00, 0x00, 0x3c, 0x80, 0x07,
0xff, 0x8f, 0xff, 0xc1, 0xff, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x3c, 0x80,
0x87, 0xff, 0xdf, 0xff, 0xc1, 0xc3, 0x07, 0x7c, 0x00, 0x00, 0x00, 0x3c,
0x80, 0x87, 0x0f, 0xfe, 0xff, 0xe3, 0x81, 0x03, 0x1e, 0x00, 0x00, 0x00,
0x3c, 0x80, 0xc7, 0x07, 0xfc, 0xe3, 0xe3, 0x81, 0x07, 0x0f, 0x00, 0x00,
0x00, 0x3c, 0x80, 0xc7, 0x07, 0xf8, 0xc1, 0xe7, 0x81, 0x87, 0xff, 0x07,
0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0xf0, 0x80, 0xe7, 0xc3, 0x87, 0xff,
0x07, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0x70, 0x80, 0xc7, 0xe7, 0x83,
0xff, 0x07, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0x78, 0x80, 0xc7, 0xff,
0x03, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x03, 0xf8, 0xc0, 0x87,
0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x80, 0xc7, 0x07, 0xfc, 0xc1,
0xc7, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0xc0, 0x87, 0x0f, 0xfe,
0xff, 0xe3, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0xc0, 0x83, 0xff,
0xdf, 0xff, 0xe3, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xf1, 0x03,
0xff, 0x8f, 0xff, 0xe1, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
0x01, 0xfe, 0x0f, 0xff, 0xc0, 0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xe0,
0xff, 0x00, 0xfc, 0x03, 0x7c, 0xc0, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00,
0x80, 0x3f, 0x00, 0xf8, 0x01, 0x00, 0xe0, 0x01, 0x1e, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1e, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x1e, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xf0, 0xc7, 0x0f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xe0, 0xff,
0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xc0,
0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff,
0x01, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff,
0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x24, 0x20, 0x00,
0x00, 0x08, 0x46, 0x02, 0x00, 0x80, 0xc0, 0x40, 0x00, 0x0c, 0x6e, 0x6a,
0xc0, 0xa4, 0x48, 0x04, 0xaa, 0xac, 0x8c, 0xaa, 0xac, 0x00, 0x6a, 0xa4,
0xaa, 0x20, 0xea, 0xa4, 0x64, 0x66, 0xaa, 0x46, 0x4a, 0x8a, 0x00, 0x4c,
0xa4, 0xaa, 0x20, 0xaa, 0xa2, 0x44, 0x2a, 0xaa, 0x28, 0xaa, 0x4c, 0x00,
0xe8, 0xa8, 0x6c, 0xc4, 0xa4, 0x42, 0xee, 0x2a, 0xcc, 0x26, 0x6c, 0xe8,
0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00 };

Binary file not shown.

After

Width:  |  Height:  |  Size: 497 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 576 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 651 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 694 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 379 B

BIN
tools/png2bin/zlib1.dll Normal file

Binary file not shown.