added support to save plots to a file in bitmap format - gsl-shell.git - gsl-shell

index : gsl-shell.git
gsl-shell
summary refs log tree commit diff
diff options
context:
space:
mode:
authorfrancesco <francesco.bbt@gmail.com>2010年11月26日 23:16:08 +0100
committerfrancesco <francesco.bbt@gmail.com>2010年11月26日 23:16:08 +0100
commit988961d3651dba3d13c208c852c424f2d97db658 (patch)
tree9ca45dd5669a2027e112c21e17ed13d2e515b459
parent8a0a8f276f0ef0e1395f4dcb563dc80dd7c80321 (diff)
downloadgsl-shell-988961d3651dba3d13c208c852c424f2d97db658.tar.gz
added support to save plots to a file in bitmap format
Diffstat
-rw-r--r--agg-plot/Makefile 2
-rw-r--r--agg-plot/agg-pixfmt-config.h 16
-rw-r--r--agg-plot/agg_platform_support_win32.cpp 18
-rw-r--r--agg-plot/agg_platform_support_x11.cpp 138
-rw-r--r--agg-plot/bitmap-plot.cpp 49
-rw-r--r--agg-plot/bitmap-plot.h 12
-rw-r--r--agg-plot/lua-plot.cpp 2
-rw-r--r--agg-plot/platform_support_ext.h 2
8 files changed, 169 insertions, 70 deletions
diff --git a/agg-plot/Makefile b/agg-plot/Makefile
index 0f937d12..d9a3040a 100644
--- a/agg-plot/Makefile
+++ b/agg-plot/Makefile
@@ -44,7 +44,7 @@ endif
INCLUDES += $(AGG_INCLUDES) -I$(GSH_BASE_DIR) -I$(LUADIR)/src
-AGGPLOT_SRC_FILES = $(PLATSUP_SRC_FILES) utils.cpp units.cpp colors.cpp markers.cpp lua-draw.cpp lua-text.cpp text.cpp agg-parse-trans.cpp window.cpp lua-plot.cpp canvas-window.cpp
+AGGPLOT_SRC_FILES = $(PLATSUP_SRC_FILES) utils.cpp units.cpp colors.cpp markers.cpp lua-draw.cpp lua-text.cpp text.cpp agg-parse-trans.cpp window.cpp lua-plot.cpp canvas-window.cpp bitmap-plot.cpp
AGGPLOT_OBJ_FILES := $(AGGPLOT_SRC_FILES:%.cpp=%.o)
diff --git a/agg-plot/agg-pixfmt-config.h b/agg-plot/agg-pixfmt-config.h
new file mode 100644
index 00000000..01ffabba
--- /dev/null
+++ b/agg-plot/agg-pixfmt-config.h
@@ -0,0 +1,16 @@
+#ifndef AGG_PIXFMT_CONFIG_H
+#define AGG_PIXFMT_CONFIG_H
+
+#include "platform/agg_platform_support.h"
+
+namespace gslshell
+{
+ const agg::pix_format_e pixel_format = agg::pix_format_bgr24;
+ const bool flip_y = true;
+
+ extern unsigned bpp;
+ extern agg::pix_format_e sys_pixel_format;
+ extern unsigned sys_bpp;
+}
+
+#endif
diff --git a/agg-plot/agg_platform_support_win32.cpp b/agg-plot/agg_platform_support_win32.cpp
index e096c82f..2cd1861d 100644
--- a/agg-plot/agg_platform_support_win32.cpp
+++ b/agg-plot/agg_platform_support_win32.cpp
@@ -26,6 +26,7 @@
#include <string.h>
#include <pthread.h>
#include <new>
+#include "agg-pixfmt-config.h"
#include "platform_support_ext.h"
#include "platform/win32/agg_win32_bmp.h"
#include "util/agg_color_conv_rgb8.h"
@@ -925,6 +926,23 @@ namespace agg
void platform_support::on_post_draw(void* raw_handler) {}
}
+agg::pix_format_e gslshell::sys_pixel_format = agg::pix_format_bgr24;
+unsigned gslshell::bpp = 24;
+unsigned gslshell::sys_bpp = 24;
+
+bool
+platform_support_ext::save_image_file (agg::rendering_buffer& src, const char *fn)
+{
+ agg::pixel_map pmap;
+ pmap.create(src.width(), src.height(), agg::org_e(gslshell::sys_bpp));
+
+ agg::rendering_buffer rbuf_tmp;
+ pixel_map_attach (pmap, &rbuf_tmp, gslshell::flip_y);
+
+ agg::convert_pmap (&rbuf_tmp, &src, gslshell::pixel_format, true);
+ return pmap.save_as_bmp (fn);
+}
+
void
platform_support_ext::prepare()
{
diff --git a/agg-plot/agg_platform_support_x11.cpp b/agg-plot/agg_platform_support_x11.cpp
index d25b3a94..83499394 100644
--- a/agg-plot/agg_platform_support_x11.cpp
+++ b/agg-plot/agg_platform_support_x11.cpp
@@ -35,6 +35,7 @@
#include <new>
#include "agg_basics.h"
#include "util/agg_color_conv_rgb8.h"
+#include "agg-pixfmt-config.h"
#include "platform_support_ext.h"
#include "rect.h"
@@ -107,6 +108,11 @@ public:
XImage * ximage() { return m_img; };
};
+unsigned gslshell::bpp = 24;
+
+agg::pix_format_e gslshell::sys_pixel_format = agg::pix_format_undefined;
+unsigned gslshell::sys_bpp = 0;
+
buffer_image::buffer_image(unsigned bpp, unsigned byte_order,
unsigned width, unsigned height, x_connection *xc)
{
@@ -421,6 +427,8 @@ namespace agg
0, 0, x_dst, y_dst, w, h);
}
+ bool platform_specific::initialized = false;
+
//------------------------------------------------------------------------
platform_support::platform_support(pix_format_e format, bool flip_y) :
m_specific(new platform_specific(format, flip_y)),
@@ -589,6 +597,9 @@ namespace agg
return false;
}
+ gslshell::sys_pixel_format = m_specific->m_sys_format;
+ gslshell::sys_bpp = m_specific->m_sys_bpp;
+
XSetWindowAttributes *win_attr = &m_specific->m_window_attributes;
memset(win_attr, 0, sizeof(XSetWindowAttributes));
@@ -970,80 +981,13 @@ namespace agg
}
-
-
//------------------------------------------------------------------------
bool platform_support::save_img(unsigned idx, const char* file)
{
- if(idx < max_images && rbuf_img(idx).buf())
- {
- char buf[1024];
- strcpy(buf, file);
- int len = strlen(buf);
- if(len < 4 || strcasecmp(buf + len - 4, ".ppm") != 0)
- {
- strcat(buf, ".ppm");
- }
-
- FILE* fd = fopen(buf, "wb");
- if(fd == 0) return false;
-
- unsigned w = rbuf_img(idx).width();
- unsigned h = rbuf_img(idx).height();
-
- fprintf(fd, "P6\n%d %d\n255\n", w, h);
-
- unsigned y;
- unsigned char* tmp_buf = new unsigned char [w * 3];
- for(y = 0; y < rbuf_img(idx).height(); y++)
- {
- const unsigned char* src = rbuf_img(idx).row_ptr(m_flip_y ? h - 1 - y : y);
- switch(m_format)
- {
- default: break;
- case pix_format_rgb555:
- color_conv_row(tmp_buf, src, w, color_conv_rgb555_to_rgb24());
- break;
-
- case pix_format_rgb565:
- color_conv_row(tmp_buf, src, w, color_conv_rgb565_to_rgb24());
- break;
-
- case pix_format_bgr24:
- color_conv_row(tmp_buf, src, w, color_conv_bgr24_to_rgb24());
- break;
-
- case pix_format_rgb24:
- color_conv_row(tmp_buf, src, w, color_conv_rgb24_to_rgb24());
- break;
-
- case pix_format_rgba32:
- color_conv_row(tmp_buf, src, w, color_conv_rgba32_to_rgb24());
- break;
-
- case pix_format_argb32:
- color_conv_row(tmp_buf, src, w, color_conv_argb32_to_rgb24());
- break;
-
- case pix_format_bgra32:
- color_conv_row(tmp_buf, src, w, color_conv_bgra32_to_rgb24());
- break;
-
- case pix_format_abgr32:
- color_conv_row(tmp_buf, src, w, color_conv_abgr32_to_rgb24());
- break;
- }
- fwrite(tmp_buf, 1, w * 3, fd);
- }
- delete [] tmp_buf;
- fclose(fd);
- return true;
- }
return false;
}
-
//------------------------------------------------------------------------
bool platform_support::create_img(unsigned idx, unsigned width, unsigned height)
{
@@ -1107,8 +1051,6 @@ namespace agg
void platform_support::on_post_draw(void* raw_handler) {}
}
-bool agg::platform_specific::initialized = false;
-
void
platform_support_ext::prepare()
{
@@ -1168,3 +1110,61 @@ platform_support_ext::do_window_update()
agg::rect_base<int> r(0, 0, rbuf_window().width(), rbuf_window().height());
update_region(r);
}
+
+bool
+platform_support_ext::save_image_file (agg::rendering_buffer& rbuf, const char *filename)
+{
+ FILE* fd = fopen(filename, "wb");
+ if(fd == 0) return false;
+
+ unsigned w = rbuf.width();
+ unsigned h = rbuf.height();
+
+ fprintf(fd, "P6\n%d %d\n255\n", w, h);
+
+ unsigned y;
+ unsigned char* tmp_buf = new unsigned char [w * 3];
+ for(y = 0; y < rbuf.height(); y++)
+ {
+ const unsigned char* src = rbuf.row_ptr(gslshell::flip_y ? h - 1 - y : y);
+ switch(gslshell::pixel_format)
+ {
+ default: break;
+ case agg::pix_format_rgb555:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_rgb555_to_rgb24());
+ break;
+
+ case agg::pix_format_rgb565:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_rgb565_to_rgb24());
+ break;
+
+ case agg::pix_format_bgr24:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_bgr24_to_rgb24());
+ break;
+
+ case agg::pix_format_rgb24:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_rgb24_to_rgb24());
+ break;
+
+ case agg::pix_format_rgba32:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_rgba32_to_rgb24());
+ break;
+
+ case agg::pix_format_argb32:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_argb32_to_rgb24());
+ break;
+
+ case agg::pix_format_bgra32:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_bgra32_to_rgb24());
+ break;
+
+ case agg::pix_format_abgr32:
+ agg::color_conv_row(tmp_buf, src, w, agg::color_conv_abgr32_to_rgb24());
+ break;
+ }
+ fwrite(tmp_buf, 1, w * 3, fd);
+ }
+ delete [] tmp_buf;
+ fclose(fd);
+ return true;
+}
diff --git a/agg-plot/bitmap-plot.cpp b/agg-plot/bitmap-plot.cpp
new file mode 100644
index 00000000..73109503
--- /dev/null
+++ b/agg-plot/bitmap-plot.cpp
@@ -0,0 +1,49 @@
+
+extern "C" {
+#include "lua.h"
+#include "lauxlib.h"
+}
+
+#include "bitmap-plot.h"
+#include "lua-cpp-utils.h"
+#include "lua-plot-cpp.h"
+#include "gs-types.h"
+#include "canvas.h"
+#include "colors.h"
+#include "agg-pixfmt-config.h"
+#include "platform_support_ext.h"
+
+int
+bitmap_save_image (lua_State *L)
+{
+ lua_plot *p = object_check<lua_plot>(L, 1, GS_PLOT);
+ const char *fn = lua_tostring (L, 2);
+
+ if (fn == NULL)
+ return gs_type_error (L, 2, "string");
+
+ unsigned w = 480, h = 480;
+
+ agg::rendering_buffer rbuf_tmp;
+ unsigned row_size = h * (gslshell::bpp / 8);
+ unsigned buf_size = w * row_size;
+ unsigned char *buffer = new unsigned char[buf_size];
+ rbuf_tmp.attach(buffer, w, h, gslshell::flip_y ? row_size : -row_size);
+
+ canvas can(rbuf_tmp, w, h, colors::white);
+ agg::trans_affine mtx(w, 0.0, 0.0, h, 0.0, 0.0);
+
+ agg::rect_base<int> r = rect_of_slot_matrix<int>(mtx);
+ can.clear_box(r);
+
+ p->draw(can, mtx);
+
+ if (! platform_support_ext::save_image_file (rbuf_tmp, fn))
+ {
+ delete buffer;
+ return luaL_error (L, "error saving image in filename %s", fn);
+ }
+
+ delete buffer;
+ return 0;
+}
diff --git a/agg-plot/bitmap-plot.h b/agg-plot/bitmap-plot.h
new file mode 100644
index 00000000..68aa9e08
--- /dev/null
+++ b/agg-plot/bitmap-plot.h
@@ -0,0 +1,12 @@
+#ifndef BITMAP_PLOT_H
+#define BITMAP_PLOT_H
+
+extern "C" {
+
+#include "lua.h"
+
+extern int bitmap_save_image (lua_State *L);
+
+}
+
+#endif
diff --git a/agg-plot/lua-plot.cpp b/agg-plot/lua-plot.cpp
index af130527..12971bb7 100644
--- a/agg-plot/lua-plot.cpp
+++ b/agg-plot/lua-plot.cpp
@@ -25,6 +25,7 @@ extern "C" {
#include "lua-plot.h"
#include "lua-plot-cpp.h"
+#include "bitmap-plot.h"
#include "window.h"
#include "gs-types.h"
#include "lua-utils.h"
@@ -83,6 +84,7 @@ static const struct luaL_Reg plot_methods[] = {
{"pushlayer", plot_push_layer },
{"poplayer", plot_pop_layer },
{"clear", plot_clear },
+ {"save", bitmap_save_image },
{"__index", plot_index },
{"__newindex", plot_newindex },
{"__gc", plot_free },
diff --git a/agg-plot/platform_support_ext.h b/agg-plot/platform_support_ext.h
index e3d8f320..21b32e11 100644
--- a/agg-plot/platform_support_ext.h
+++ b/agg-plot/platform_support_ext.h
@@ -19,6 +19,8 @@ public:
void do_window_update ();
static void prepare();
+
+ static bool save_image_file (agg::rendering_buffer& src, const char *fn);
};
template<class RenBufDst, class RenBufSrc, class CopyRow>
generated by cgit v1.2.3 (git 2.39.1) at 2025年09月16日 08:30:23 +0000

AltStyle によって変換されたページ (->オリジナル) /