From ee7304700b2f058864d6ef588531399eb9104ea9 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 3 May 2013 14:38:52 +0200 Subject: PNG error handling, extreme --- plugingui/image.cc | 57 +++++++++----- plugingui/image.h | 2 + plugingui/resource_data.cc | 164 ++++++++++++++++++++++++++++++++++++++++ plugingui/resources/createrc.sh | 2 +- plugingui/resources/png_error | Bin 0 -> 2608 bytes tools/img2c/img2c.cc | 69 ++++++++--------- tools/img2c/img2c.pro | 11 +++ 7 files changed, 248 insertions(+), 57 deletions(-) create mode 100644 plugingui/resources/png_error create mode 100644 tools/img2c/img2c.pro diff --git a/plugingui/image.cc b/plugingui/image.cc index 32994b6..d0afe70 100644 --- a/plugingui/image.cc +++ b/plugingui/image.cc @@ -26,24 +26,15 @@ */ #include "image.h" -#include "resource.h" - #include #include #include #include #include -void abort_(const char * s, ...) -{ - va_list args; - va_start(args, s); - vfprintf(stderr, s, args); - fprintf(stderr, "\n"); - va_end(args); - abort(); -} +#include +#include "resource.h" // http://blog.hammerian.net/2009/reading-png-images-from-memory/ typedef struct { @@ -77,29 +68,59 @@ GUI::Image::~Image() { } +void GUI::Image::setError(int err) +{ + GUI::Resource rc(":png_error"); + + const char *p = rc.data(); + + memcpy(&w, p, 4); p += 4; + memcpy(&h, p, 4); p += 4; + + DEBUG(image, "w:%d, h:%d\n", w, h); + + row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * h); + for(unsigned int y = 0; y < h; y++) { + size_t size = w * sizeof(unsigned int); + DEBUG(image, "rc.size:%d >= p:%d (rowsize: %d)\n", + rc.size(), p - rc.data(), size); + row_pointers[y] = (png_byte*)malloc(size); + memcpy(row_pointers[y], p, size); + p += size; + } +} + void GUI::Image::load(const char* data, size_t size) { const char *header = data; // test for it being a png: if(png_sig_cmp((png_byte*)header, 0, 8)) { - abort_("[read_png_file] File is not recognized as a PNG file"); + ERR(image, "[read_png_file] File is not recognized as a PNG file"); + setError(0); + return; } // initialize stuff png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if(!png_ptr) { - abort_("[read_png_file] png_create_read_struct failed"); + ERR(image, "[read_png_file] png_create_read_struct failed"); + setError(1); + return; } info_ptr = png_create_info_struct(png_ptr); if(!info_ptr) { - abort_("[read_png_file] png_create_info_struct failed"); + ERR(image, "[read_png_file] png_create_info_struct failed"); + setError(2); + return; } if(setjmp(png_jmpbuf(png_ptr))) { - abort_("[read_png_file] Error during init_io"); + ERR(image, "[read_png_file] Error during init_io"); + setError(3); + return; } //png_init_io(png_ptr, fp); @@ -122,9 +143,11 @@ void GUI::Image::load(const char* data, size_t size) png_read_update_info(png_ptr, info_ptr); - /* read file */ + // read file if (setjmp(png_jmpbuf(png_ptr))) { - abort_("[read_png_file] Error during read_image"); + ERR(image, "[read_png_file] Error during read_image"); + setError(4); + return; } row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * h); diff --git a/plugingui/image.h b/plugingui/image.h index 1df248f..0181ce8 100644 --- a/plugingui/image.h +++ b/plugingui/image.h @@ -47,6 +47,8 @@ public: Colour getPixel(size_t x, size_t y); private: + void setError(int err); + void load(const char* data, size_t size); size_t w, h; diff --git a/plugingui/resource_data.cc b/plugingui/resource_data.cc index badddec..ce29fd8 100644 --- a/plugingui/resource_data.cc +++ b/plugingui/resource_data.cc @@ -3415,5 +3415,169 @@ const rc_data_t rc_data[] = "\47\111\335\27\346\155\0\60\41\4\122\112\345\103\13\140" "\226\243\41\306\130\242\5\270\1\2\112\32\211\154\353\20" "\253\0\0\0\0\111\105\116\104\256\102\140\202" }, + { ":png_error", 2608, + "\101\0\0\0\12\0\0\0\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\377\377\377\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\0\0\0\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\0\0\0\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\0\0\0\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\0\0\0\377\0\0\0\377" + "\0\0\0\377\377\377\377\377\377\377\377\377\0\0\0\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" + "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377" }, { "", 0, 0 } }; diff --git a/plugingui/resources/createrc.sh b/plugingui/resources/createrc.sh index 44afead..838713a 100755 --- a/plugingui/resources/createrc.sh +++ b/plugingui/resources/createrc.sh @@ -1,2 +1,2 @@ #!/bin/bash -../rcgen *.png > ../resource_data.cc \ No newline at end of file +../rcgen *.png png_error > ../resource_data.cc diff --git a/plugingui/resources/png_error b/plugingui/resources/png_error new file mode 100644 index 0000000..cadb45e Binary files /dev/null and b/plugingui/resources/png_error differ diff --git a/tools/img2c/img2c.cc b/tools/img2c/img2c.cc index e24d2b3..ebff6d7 100644 --- a/tools/img2c/img2c.cc +++ b/tools/img2c/img2c.cc @@ -7,43 +7,34 @@ int main(int argc, char *argv[]) { - // QApplication app(argc, argv); - - - - if(argc < 3) { - printf("Missing parameter [inputfile] [imgname]\n"); - return 1; - } - - QString file = argv[1]; - QString name = argv[2]; - - QImage img; - if(!img.load(file)) { - printf("Could not open file %s\n", file.toStdString().c_str()); - return 1; - } - - printf("struct __img_%s {\n size_t width;\n size_t height;\n unsigned int pixels[%d];\n unsigned int order;\n}", name.toStdString().c_str(), img.width() * img.height()); - printf(" img_%s = {\n", name.toStdString().c_str()); - printf(" %d,\n", img.width()); - printf(" %d,\n", img.height()); - printf(" {\n"); - - for(int y = 0; y < img.height(); y++) { - for(int x = 0; x < img.width(); x++) { - printf(" 0x%08x", htonl(img.pixel(x, y))); - if(x != img.width() - 1 || y != img.height() - 1) { - printf(","); - } - printf("\n"); - } - } - printf(" },\n"); - printf(" 0x%08x", htonl(0x00010203)); - printf("};\n"); - - return 0; - // return app.exec(); + if(argc < 3) { + printf("Missing parameter [inputfile] [outputfile]\n"); + return 1; + } + + QString file = argv[1]; + QString name = argv[2]; + + QImage img; + if(!img.load(file)) { + printf("Could not open file %s\n", file.toStdString().c_str()); + return 1; + } + + FILE *fp = fopen(name.toStdString().c_str(), "w"); + + unsigned int w = img.width(); + unsigned int h = img.height(); + (void)fwrite(&w, sizeof(unsigned int), 1, fp); + (void)fwrite(&h, sizeof(unsigned int), 1, fp); + + for(int y = 0; y < img.height(); y++) { + for(int x = 0; x < img.width(); x++) { + unsigned int px = img.pixel(x, y); + (void)fwrite(&px, 1, sizeof(unsigned int), fp); + } + } + + fclose(fp); + return 0; } diff --git a/tools/img2c/img2c.pro b/tools/img2c/img2c.pro new file mode 100644 index 0000000..08907d5 --- /dev/null +++ b/tools/img2c/img2c.pro @@ -0,0 +1,11 @@ +###################################################################### +# Automatically generated by qmake (2.01a) Fri May 3 14:05:24 2013 +###################################################################### + +TEMPLATE = app +TARGET = +DEPENDPATH += . +INCLUDEPATH += . + +# Input +SOURCES += img2c.cc -- cgit v1.2.3