Skip to content

Commit

Permalink
png images are included in the executable now
Browse files Browse the repository at this point in the history
  • Loading branch information
Mortis69 committed Mar 22, 2015
1 parent 4d37f06 commit cfbb1e7
Show file tree
Hide file tree
Showing 36 changed files with 4,273 additions and 246 deletions.
32 changes: 2 additions & 30 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ libren_la_SOURCES = \
src/Group.cxx \
src/Gui.H \
src/Gui.cxx \
src/Images.H \
src/Inline.H \
src/InputFloat.H \
src/InputFloat.cxx \
Expand All @@ -68,8 +69,6 @@ libren_la_SOURCES = \
src/Paint.cxx \
src/Palette.H \
src/Palette.cxx \
src/Path.H \
src/Path.cxx \
src/Project.H \
src/Project.cxx \
src/Quantize.H \
Expand Down Expand Up @@ -100,33 +99,9 @@ bin_PROGRAMS = ./bin/rendera
__bin_rendera_SOURCES = src/Main.cxx
__bin_rendera_LDADD = libren.la


nobase_dist_pkgdata_DATA = \
data/icon48x48.png \
data/clone.png \
data/constrain.png \
data/grid.png \
data/logo.png \
data/logo_large.png \
data/mirror.png \
data/origin.png \
data/shape.png \
data/size.png \
data/soft_edge.png \
data/stroke.png \
data/tools.png \
data/transparency.png \
data/wrap.png \
data/zoom_fit.png \
data/zoom_in.png \
data/zoom_one.png \
data/zoom_out.png


check_PROGRAMS = \
test_blend \
test_bresenham \
test_pathexe \
test_fltk_version \
test_endian \
test_numerics
Expand All @@ -143,9 +118,6 @@ test_blend_SOURCES = test/blend.C
test_bresenham_SOURCES = test/bresenham.C
test_bresenham_LDADD = libalgo.la

test_pathexe_SOURCES = test/pathexe.C
test_pathexe_LDADD = libren.la

test_fltk_version_SOURCES = test/fltk_version.C

test_endian_SOURCES = test/endian.C
Expand All @@ -158,5 +130,5 @@ TESTS = $(check_PROGRAMS)

dist_man1_MANS = man/rendera.man

EXTRA_DIST = Rendera.md
EXTRA_DIST = images Rendera.md

7 changes: 7 additions & 0 deletions README.images
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The image header (../src/Images.H) is created with barry:
https://github.com/rubicks/barry

barry -o ../src/Images.H ./*.png

These images should always be included in source distributions.

File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion src/Button.H
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Button : public Fl_Button
{
public:
Button(Fl_Group *, int, int, int, int,
const char *, const char *, Fl_Callback *);
const char *, const unsigned char *, Fl_Callback *);
~Button();

int var;
Expand Down
7 changes: 4 additions & 3 deletions src/Button.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "Project.H"

Button::Button(Fl_Group *g, int x, int y, int w, int h,
const char *label, const char *filename, Fl_Callback *cb)
const char *label, const unsigned char *array, Fl_Callback *cb)
: Fl_Button(x, y, w, h, label)
{
var = 0;
Expand All @@ -40,10 +40,11 @@ Button::Button(Fl_Group *g, int x, int y, int w, int h,

group = g;

if(!(bitmap = File::loadPng(filename, 0)))
if(!(bitmap = File::loadPngFromArray(array, 0)))
{
fl_message_title("Error");
fl_message("Could not load %s, exiting.", filename);
// fl_message("Could not load %s, exiting.", filename);
fl_message("Could not load image.");
exit(1);
}

Expand Down
3 changes: 2 additions & 1 deletion src/Dialog.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "DialogWindow.H"
#include "File.H"
#include "Gui.H"
#include "Images.H"
#include "Inline.H"
#include "InputInt.H"
#include "Map.H"
Expand Down Expand Up @@ -81,7 +82,7 @@ namespace About

Items::dialog = new DialogWindow(384, 0, "About");
Items::logo = new Widget(Items::dialog, 32, y1, 320, 96,
credits, File::themePath("logo_large.png"), -1, -1, 0);
credits, __logo_large_png, -1, -1, 0);
if(Project::theme == Project::THEME_LIGHT)
{
Items::logo->bitmap->invert();
Expand Down
2 changes: 1 addition & 1 deletion src/File.H
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace File
Bitmap *loadBmp(const char *, int);
Bitmap *loadTarga(const char *, int);
Bitmap *loadPng(const char *, int);
Bitmap *loadPngFromArray(const unsigned char *, int);

void save(Fl_Widget *, void *);
int saveBmp(const char *);
Expand All @@ -51,7 +52,6 @@ namespace File

void decodeURI(char *);
void getDirectory(char *, const char *);
char *themePath(const char *);
}

#endif
Expand Down
193 changes: 181 additions & 12 deletions src/File.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,6 @@ namespace
char pal_load_dir[256];
char pal_save_dir[256];

// buffer for theme path + icon filename
char theme_path_string[256];

// show error dialog
void errorMessage()
{
Expand Down Expand Up @@ -195,6 +192,22 @@ namespace
return (memcmp(header, "GIMP Palette", 12) == 0);
}

struct png_state
{
png_uint_32 pos;
png_uint_32 size;
const unsigned char *array;
};

// callback for reading PNG from byte array
void pngReadFromArray(png_structp png_ptr, png_bytep dest, png_uint_32 length)
{
struct png_state *src = (struct png_state *)png_get_io_ptr(png_ptr);

memcpy(dest, src->array + src->pos, length);
src->pos += length;
}

// reset directories
int *init()
{
Expand Down Expand Up @@ -741,6 +754,171 @@ Bitmap *File::loadPng(const char *fn, int overscroll)
return temp;
}

Bitmap *File::loadPngFromArray(const unsigned char *array, int overscroll)
{
png_structp png_ptr;
png_infop info_ptr;

png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if(!png_ptr)
return 0;

info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
png_destroy_read_struct(&png_ptr, 0, 0);
return 0;
}

if(setjmp(png_jmpbuf(png_ptr)))
{
// pnglib does a goto here if there is an error
png_destroy_read_struct(&png_ptr, &info_ptr, 0);
return 0;
}

struct png_state state;
state.array = array;
state.pos = 0;

png_uint_32 temp_w = 0;
png_uint_32 temp_h = 0;
int bits_per_channel = 0;
int color_type = 0;
int interlace_type = 0;
int compression_type = 0;
int filter_method = 0;

png_set_read_fn(png_ptr, &state, (png_rw_ptr)pngReadFromArray);

// png_init_io(png_ptr, in.get());
// png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &temp_w, &temp_h,
&bits_per_channel, &color_type,
&interlace_type, &compression_type, &filter_method);

int w = temp_w;
int h = temp_h;

// check interlace mode
bool interlace = png_set_interlace_handling(png_ptr) > 1 ? 1 : 0;

// expand paletted images to RGB
if(color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);

// expand low-color images to RGB
if(color_type == PNG_COLOR_TYPE_GRAY && bits_per_channel < 8)
png_set_expand(png_ptr);

// check for alpha channel
if(png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
png_set_expand(png_ptr);

// convert 16-bit images to 8
if(bits_per_channel == 16)
png_set_strip_16(png_ptr);

// expand grayscale images to RGB
if(color_type == PNG_COLOR_TYPE_GRAY ||
color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);

// perform gamma correction if the file requires it
// gonna ignore this for now
// double gamma = 0;
// if(png_get_gAMA(png_ptr, info_ptr, &gamma))
// png_set_gamma(png_ptr, 2.2, gamma);

png_read_update_info(png_ptr, info_ptr);

int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
int channels = (int)png_get_channels(png_ptr, info_ptr);

Bitmap *volatile temp = new Bitmap(w, h, overscroll);

if(interlace)
{
// interlaced images require a buffer the size of the entire image
std::vector<png_byte> data(rowbytes * h);
std::vector<png_bytep> row_pointers(h);

for(int y = 0; y < h; y++)
row_pointers[y] = &data[y * rowbytes];

// read image all at once
png_read_image(png_ptr, &row_pointers[0]);

// convert image
for(int y = 0; y < h; y++)
{
int *p = temp->row[y + overscroll] + overscroll;
int xx = 0;

png_bytep row = row_pointers[y];

for(int x = 0; x < w; x++)
{
if(channels == 3)
{
*p++ = makeRgb(row[xx + 0] & 0xFF,
row[xx + 1] & 0xFF,
row[xx + 2] & 0xFF);
}
else if(channels == 4)
{
*p++ = makeRgba(row[xx + 0] & 0xFF,
row[xx + 1] & 0xFF,
row[xx + 2] & 0xFF,
row[xx + 3] & 0xFF);
}

xx += channels;
}
}
}
else
{
// non-interlace images can be read line-by-line
std::vector<png_byte> linebuf(rowbytes);

for(int y = 0; y < h; y++)
{
// read line
png_read_row(png_ptr, &linebuf[0], 0);

int *p = temp->row[y + overscroll] + overscroll;
int xx = 0;

// convert line
for(int x = 0; x < w; x++)
{
if(channels == 3)
{
*p++ = makeRgb(linebuf[xx + 0] & 0xFF,
linebuf[xx + 1] & 0xFF,
linebuf[xx + 2] & 0xFF);
}
else if(channels == 4)
{
*p++ = makeRgba(linebuf[xx + 0] & 0xFF,
linebuf[xx + 1] & 0xFF,
linebuf[xx + 2] & 0xFF,
linebuf[xx + 3] & 0xFF);
}

xx += channels;
}
}
}

png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, 0);

return temp;
}

void File::save(Fl_Widget *, void *)
{
Fl_Native_File_Chooser fc;
Expand Down Expand Up @@ -1343,12 +1521,3 @@ void File::getDirectory(char *dest, const char *src)
}
}

// expand filename to include theme path
char *File::themePath(const char *fn)
{
strcpy(theme_path_string, Project::theme_path);
strcat(theme_path_string, fn);

return theme_path_string;
}

Loading

0 comments on commit cfbb1e7

Please sign in to comment.