diff --git a/Package.swift b/Package.swift index 583df3e0..33bba9a2 100644 --- a/Package.swift +++ b/Package.swift @@ -26,6 +26,22 @@ let package = Package( name: "lodepng", dependencies: [], path: "framework/lodepng"), + .target( + name: "CPPAGGRenderer", + dependencies: ["AGG","lodepng"], + path: "framework/CPPAGGRenderer"), + .target( + name: "CAGGRenderer", + dependencies: ["CPPAGGRenderer"], + path: "framework/CAGGRenderer"), + .target( + name: "Util", + dependencies: [], + path: "framework/Util"), + .target( + name: "Renderer", + dependencies: ["Util","CAGGRenderer"], + path: "framework/Renderer"), .target( name: "LinePlot", dependencies: [], diff --git a/framework/CAGGRenderer/CAGGRenderer.cpp b/framework/CAGGRenderer/CAGGRenderer.cpp new file mode 100644 index 00000000..69d57ebf --- /dev/null +++ b/framework/CAGGRenderer/CAGGRenderer.cpp @@ -0,0 +1,47 @@ +#include "include/CAGGRenderer.h" +#include "CPPAGGRenderer.h" +#include + +const void * initializePlot(float w, float h){ + return CPPAGGRenderer::initializePlot(w, h); +} + +void draw_rect(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object){ + CPPAGGRenderer::draw_rect(x, y, thickness, r, g, b, a, object); +} + +void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, const void *object){ + CPPAGGRenderer::draw_solid_rect(x, y, r, g, b, a, object); +} + +void draw_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object){ + CPPAGGRenderer::draw_line(x, y, thickness, r, g, b, a, object); +} + +void draw_transformed_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object){ + CPPAGGRenderer::draw_transformed_line(x, y, thickness, r, g, b, a, object); +} + +void draw_plot_lines(const float *x, const float *y, int size, float thickness, float r, float g, float b, float a, const void *object){ + CPPAGGRenderer::draw_plot_lines(x, y, size, thickness, r, g, b, a, object); +} + +void draw_text(const char *s, float x, float y, float size, float thickness, const void *object){ + CPPAGGRenderer::draw_text(s, x, y, size, thickness, object); +} + +void draw_transformed_text(const char *s, float x, float y, float size, float thickness, const void *object){ + CPPAGGRenderer::draw_transformed_text(s, x, y, size, thickness, object); +} + +void draw_rotated_text(const char *s, float x, float y, float size, float thickness, float angle, const void *object){ + CPPAGGRenderer::draw_rotated_text(s, x, y, size, thickness, angle, object); +} + +float get_text_width(const char *s, float size, const void *object){ + return CPPAGGRenderer::get_text_width(s, size, object); +} + +void save_image(const char *s, const void *object){ + CPPAGGRenderer::save_image(s, object); +} diff --git a/framework/CAGGRenderer/include/CAGGRenderer.h b/framework/CAGGRenderer/include/CAGGRenderer.h new file mode 100644 index 00000000..a6c85d90 --- /dev/null +++ b/framework/CAGGRenderer/include/CAGGRenderer.h @@ -0,0 +1,29 @@ +#ifdef __cplusplus +extern "C" { +#endif + +const void * initializePlot(float w, float h); + +void draw_rect(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + +void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, const void *object); + +void draw_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + +void draw_transformed_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + +void draw_plot_lines(const float *x, const float *y, int size, float thickness, float r, float g, float b, float a, const void *object); + +void draw_text(const char *s, float x, float y, float size, float thickness, const void *object); + +void draw_transformed_text(const char *s, float x, float y, float size, float thickness, const void *object); + +void draw_rotated_text(const char *s, float x, float y, float size, float thickness, float angle, const void *object); + +float get_text_width(const char *s, float size, const void *object); + +void save_image(const char *s, const void *object); + +#ifdef __cplusplus +} +#endif diff --git a/framework/CPPAGGRenderer/CPPAGGRenderer.cpp b/framework/CPPAGGRenderer/CPPAGGRenderer.cpp new file mode 100644 index 00000000..6460743b --- /dev/null +++ b/framework/CPPAGGRenderer/CPPAGGRenderer.cpp @@ -0,0 +1,377 @@ +#include +#include "string.h" + +#include "include/CPPAGGRenderer.h" +//agg rendering library +#include "agg_basics.h" +#include "agg_rendering_buffer.h" +#include "agg_rasterizer_scanline_aa.h" +#include "agg_scanline_p.h" +#include "agg_pixfmt_rgb.h" +#include "agg_path_storage.h" +#include "platform/agg_platform_support.h" +#include "agg_gsv_text.h" +#include "agg_conv_curve.h" +//lodepng library +#include "lodepng.h" +//header to save bitmaps +#include "savebmp.h" + +#define AGG_RGB24 +#include "include/pixel_formats.h" + +typedef agg::pixfmt_rgb24 pixfmt; +typedef agg::renderer_base renderer_base; +typedef agg::renderer_scanline_aa_solid renderer_aa; +typedef agg::renderer_scanline_bin_solid renderer_bin; +typedef agg::rasterizer_scanline_aa<> rasterizer_scanline; +typedef agg::scanline_p8 scanline; +typedef agg::rgba Color; + +const Color black(0.0,0.0,0.0,1.0); +const Color blue_light(0.529,0.808,0.922,1.0); +const Color white(1.0,1.0,1.0,1.0); +const Color white_translucent(1.0,1.0,1.0,0.8); + +int frame_width = 1000; +int frame_height = 660; + +namespace CPPAGGRenderer{ + unsigned char* buffer = new unsigned char[frame_width*frame_height*3]; + agg::rendering_buffer rbuf (buffer, + frame_width, + frame_height, + -frame_width*3); + pixfmt pixf(rbuf); + + static const unsigned char from_base64[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 62, 255, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, 255, 255, + 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 63, + 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255}; + + + static const char to_base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + void write_bmp(const unsigned char* buf, unsigned width, unsigned height, const char* file_name){ + saveBMP(buf, width, height, file_name); + } + + void write_png(std::vector& image, unsigned width, unsigned height, const char* filename) { + //Encode the image + LodePNGColorType colorType = LCT_RGB; + unsigned error = lodepng::encode(filename, image, width, height, colorType); + + //if there's an error, display it + if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl; + } + + bool write_ppm(const unsigned char* buf, + unsigned width, + unsigned height, + const char* file_name){ + FILE* fd = fopen(file_name, "wb"); + if(fd){ + fprintf(fd, "P6 %d %d 255 ", width, height); + fwrite(buf, 1, width*height*3, fd); + fclose(fd); + return true; + } + return false; + } + + std::string write_base64(unsigned char const* buf, float width, float height) { + float bufLen = width*height*3; + size_t ret_size = bufLen+2; + + ret_size = 4*ret_size/3; + + std::string ret; + ret.reserve(ret_size); + + for (unsigned int i=0; i> 2) ]); + ret.push_back(to_base64[ ((b3[0] & 0x03) << 4) + ((b3[1] & 0xf0) >> 4) ]); + ret.push_back(to_base64[ ((b3[1] & 0x0f) << 2) + ((b3[2] & 0xc0) >> 6) ]); + ret.push_back(to_base64[ ((b3[2] & 0x3f)) ]); + } + + return ret; +} + + class Plot{ + + public: + agg::rasterizer_scanline_aa<> m_ras; + agg::scanline_p8 m_sl_p8; + renderer_base rb; + renderer_aa ren_aa; + + Plot(){ + rb = renderer_base(pixf); + ren_aa = renderer_aa(rb); + } + + void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a){ + + agg::path_storage rect_path; + rect_path.move_to(*x, *y); + for (int i = 1; i < 4; i++) { + rect_path.line_to(*(x+i),*(y+i)); + } + rect_path.close_polygon(); + m_ras.add_path(rect_path); + Color c(r, g, b, a); + ren_aa.color(c); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_rect(const float *x, const float *y, float thickness, float r, float g, float b, float a){ + + agg::path_storage rect_path; + rect_path.move_to(*x, *y); + for (int i = 1; i < 4; i++) { + rect_path.line_to(*(x+i),*(y+i)); + } + rect_path.close_polygon(); + agg::conv_stroke rect_path_line(rect_path); + rect_path_line.width(thickness); + m_ras.add_path(rect_path_line); + Color c(r, g, b, a); + ren_aa.color(c); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_transformed_line(const float *x, const float *y, float thickness, float r, float g, float b, float a){ + + agg::path_storage rect_path; + rect_path.move_to(*x, *y); + rect_path.line_to(*(x+1),*(y+1)); + agg::trans_affine matrix; + matrix *= agg::trans_affine_translation(frame_width*0.1f, frame_height*0.1f); + agg::conv_transform trans(rect_path, matrix); + agg::conv_curve> curve(trans); + agg::conv_stroke>> stroke(curve); + stroke.width(thickness); + m_ras.add_path(stroke); + Color c(r, g, b, a); + ren_aa.color(c); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_line(const float *x, const float *y, float thickness, float r, float g, float b, float a){ + + agg::path_storage rect_path; + rect_path.move_to(*x, *y); + rect_path.line_to(*(x+1),*(y+1)); + agg::conv_stroke rect_path_line(rect_path); + rect_path_line.width(thickness); + m_ras.add_path(rect_path_line); + Color c(r, g, b, a); + ren_aa.color(c); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_plot_lines(const float *x, const float *y, int size, float thickness, float r, float g, float b, float a){ + + agg::path_storage rect_path; + rect_path.move_to(*x, *y); + for (int i = 1; i < size; i++) { + rect_path.line_to(*(x+i),*(y+i)); + } + agg::trans_affine matrix; + matrix *= agg::trans_affine_translation(frame_width*0.1f, frame_height*0.1f); + agg::conv_transform trans(rect_path, matrix); + agg::conv_curve> curve(trans); + agg::conv_stroke>> stroke(curve); + stroke.width(thickness); + m_ras.add_path(stroke); + Color c(r, g, b, a); + ren_aa.color(c); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_text(const char *s, float x, float y, float size, float thickness){ + + agg::gsv_text t; + t.size(size); + t.text(s); + t.start_point(x,y); + agg::conv_stroke stroke(t); + stroke.width(thickness); + m_ras.add_path(stroke); + ren_aa.color(black); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_transformed_text(const char *s, float x, float y, float size, float thickness){ + + agg::gsv_text t; + t.size(size); + t.text(s); + t.start_point(x,y); + agg::trans_affine matrix; + matrix *= agg::trans_affine_translation(frame_width*0.1f, frame_height*0.1f); + agg::conv_transform trans(t, matrix); + agg::conv_curve> curve(trans); + agg::conv_stroke>> stroke(curve); + stroke.width(thickness); + m_ras.add_path(stroke); + ren_aa.color(black); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + void draw_rotated_text(const char *s, float x, float y, float size, float thickness, float angle){ + + agg::gsv_text t; + t.size(size); + t.text(s); + t.start_point(0,0); + agg::trans_affine matrix; + matrix *= agg::trans_affine_rotation(agg::deg2rad(angle)); + matrix *= agg::trans_affine_translation(x, y); + agg::conv_transform trans(t, matrix); + agg::conv_curve> curve(trans); + agg::conv_stroke>> stroke(curve); + stroke.width(thickness); + m_ras.add_path(stroke); + ren_aa.color(black); + agg::render_scanlines(m_ras, m_sl_p8, ren_aa); + + } + + float get_text_width(const char *s, float size){ + + agg::gsv_text t; + t.text(s); + t.size(size); + return t.text_width(); + + } + + void save_image(const char *s){ + char* file_ppm = (char *) malloc(1 + strlen(s)+ strlen(".ppm") ); + strcpy(file_ppm, s); + strcat(file_ppm, ".ppm"); + char* file_png = (char *) malloc(1 + strlen(s)+ strlen(".png") ); + strcpy(file_png, s); + strcat(file_png, ".png"); + char* file_bmp = (char *) malloc(1 + strlen(s)+ strlen(".bmp") ); + strcpy(file_bmp, s); + strcat(file_bmp, ".bmp"); + // write_ppm(buffer, frame_width, frame_height, file_ppm); + + std::vector image(buffer, buffer + (frame_width*frame_height*3)); + write_png(image, frame_width, frame_height, file_png); + + // write_bmp(buffer, frame_width, frame_height, file_bmp); + + // ofstream fileBase64; + // fileBase64.open("base64Plot.txt"); + // fileBase64< draw_rect(x, y, thickness, r, g, b, a); + + } + + void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_solid_rect(x, y, r, g, b, a); + + } + + void draw_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_line(x, y, thickness, r, g, b, a); + + } + + void draw_transformed_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_transformed_line(x, y, thickness, r, g, b, a); + + } + + void draw_plot_lines(const float *x, const float *y, int size, float thickness, float r, float g, float b, float a, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_plot_lines(x, y, size, thickness, r, g, b, a); + + } + + void draw_text(const char *s, float x, float y, float size, float thickness, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_text(s, x, y, size, thickness); + + } + + void draw_transformed_text(const char *s, float x, float y, float size, float thickness, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_transformed_text(s, x, y, size, thickness); + + } + + void draw_rotated_text(const char *s, float x, float y, float size, float thickness, float angle, const void *object){ + + Plot *plot = (Plot *)object; + plot -> draw_rotated_text(s, x, y, size, thickness, angle); + + } + + float get_text_width(const char *s, float size, const void *object){ + + Plot *plot = (Plot *)object; + return plot -> get_text_width(s, size); + + } + + void save_image(const char *s, const void *object){ + + Plot *plot = (Plot *)object; + plot -> save_image(s); + + } + +} diff --git a/framework/CPPAGGRenderer/include/CPPAGGRenderer.h b/framework/CPPAGGRenderer/include/CPPAGGRenderer.h new file mode 100644 index 00000000..2d63559f --- /dev/null +++ b/framework/CPPAGGRenderer/include/CPPAGGRenderer.h @@ -0,0 +1,25 @@ +namespace CPPAGGRenderer{ + + const void * initializePlot(float w, float h); + + void draw_rect(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + + void draw_solid_rect(const float *x, const float *y, float r, float g, float b, float a, const void *object); + + void draw_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + + void draw_transformed_line(const float *x, const float *y, float thickness, float r, float g, float b, float a, const void *object); + + void draw_plot_lines(const float *x, const float *y, int size, float thickness, float r, float g, float b, float a, const void *object); + + void draw_text(const char *s, float x, float y, float size, float thickness, const void *object); + + void draw_transformed_text(const char *s, float x, float y, float size, float thickness, const void *object); + + void draw_rotated_text(const char *s, float x, float y, float size, float thickness, float angle, const void *object); + + float get_text_width(const char *s, float size, const void *object); + + void save_image(const char *s, const void *object); + +} diff --git a/framework/CPPAGGRenderer/include/pixel_formats.h b/framework/CPPAGGRenderer/include/pixel_formats.h new file mode 100644 index 00000000..7497d23f --- /dev/null +++ b/framework/CPPAGGRenderer/include/pixel_formats.h @@ -0,0 +1,184 @@ +#ifndef PIXEL_FORMATS_INCLUDED +#define PIXEL_FORMATS_INCLUDED + +#if defined(AGG_GRAY8) + +#include "agg_pixfmt_gray.h" +#define pix_format agg::pix_format_gray8 +typedef agg::pixfmt_gray8 pixfmt; +typedef agg::pixfmt_gray8_pre pixfmt_pre; +typedef agg::gray8 color_type; + +#elif defined(AGG_GRAY16) + +#include "agg_pixfmt_gray.h" +#define pix_format agg::pix_format_gray16 +typedef agg::pixfmt_gray16 pixfmt; +typedef agg::pixfmt_gray16_pre pixfmt_pre; +typedef agg::gray16 color_type; + +#elif defined(AGG_BGR24) + +#include "agg_pixfmt_rgb.h" +#define pix_format agg::pix_format_bgr24 +typedef agg::pixfmt_bgr24 pixfmt; +typedef agg::pixfmt_bgr24_pre pixfmt_pre; +#define pixfmt_gamma agg::pixfmt_bgr24_gamma +typedef agg::rgba8 color_type; +typedef agg::order_bgr component_order; + +#elif defined(AGG_RGB24) + +#include "agg_pixfmt_rgb.h" +#define pix_format agg::pix_format_rgb24 +typedef agg::pixfmt_rgb24 pixfmt; +typedef agg::pixfmt_rgb24_pre pixfmt_pre; +#define pixfmt_gamma agg::pixfmt_rgb24_gamma +typedef agg::rgba8 color_type; +typedef agg::order_rgb component_order; + +#elif defined(AGG_BGR48) + +#include "agg_pixfmt_rgb.h" +#define pix_format agg::pix_format_bgr48 +typedef agg::pixfmt_bgr48 pixfmt; +typedef agg::pixfmt_bgr48_pre pixfmt_pre; +#define pixfmt_gamma agg::pixfmt_bgr48_gamma +typedef agg::rgba16 color_type; +typedef agg::order_bgr component_order; + +#elif defined(AGG_RGB48) + +#include "agg_pixfmt_rgb.h" +#define pix_format agg::pix_format_rgb48 +typedef agg::pixfmt_rgb48 pixfmt; +typedef agg::pixfmt_rgb48_pre pixfmt_pre; +#define pixfmt_gamma agg::pixfmt_rgb48_gamma +typedef agg::rgba16 color_type; +typedef agg::order_rgb component_order; + +#elif defined(AGG_BGRA32) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_bgra32 +typedef agg::pixfmt_bgra32 pixfmt; +typedef agg::pixfmt_bgra32_pre pixfmt_pre; +typedef agg::rgba8 color_type; +typedef agg::order_bgra component_order; + +#elif defined(AGG_RGBA32) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_rgba32 +typedef agg::pixfmt_rgba32 pixfmt; +typedef agg::pixfmt_rgba32_pre pixfmt_pre; +typedef agg::rgba8 color_type; +typedef agg::order_rgba component_order; + +#elif defined(AGG_ARGB32) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_argb32 +typedef agg::pixfmt_argb32 pixfmt; +typedef agg::pixfmt_argb32_pre pixfmt_pre; +typedef agg::rgba8 color_type; +typedef agg::order_argb component_order; + +#elif defined(AGG_ABGR32) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_abgr32 +typedef agg::pixfmt_abgr32 pixfmt; +typedef agg::pixfmt_abgr32_pre pixfmt_pre; +typedef agg::rgba8 color_type; +typedef agg::order_argb component_order; + +#elif defined(AGG_BGRA64) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_bgra64 +typedef agg::pixfmt_bgra64 pixfmt; +typedef agg::pixfmt_bgra64_pre pixfmt_pre; +typedef agg::rgba16 color_type; +typedef agg::order_bgra component_order; + +#elif defined(AGG_RGBA64) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_rgba64 +typedef agg::pixfmt_rgba64 pixfmt; +typedef agg::pixfmt_rgba64_pre pixfmt_pre; +typedef agg::rgba16 color_type; +typedef agg::order_rgba component_order; + +#elif defined(AGG_ARGB64) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_argb64 +typedef agg::pixfmt_argb64 pixfmt; +typedef agg::pixfmt_argb64_pre pixfmt_pre; +typedef agg::rgba16 color_type; +typedef agg::order_argb component_order; + +#elif defined(AGG_ABGR64) + +#include "agg_pixfmt_rgba.h" +#define pix_format agg::pix_format_abgr64 +typedef agg::pixfmt_abgr64 pixfmt; +typedef agg::pixfmt_abgr64_pre pixfmt_pre; +typedef agg::rgba16 color_type; +typedef agg::order_argb component_order; + +#elif defined(AGG_RGB565) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_rgb565 +typedef agg::pixfmt_rgb565 pixfmt; +typedef agg::pixfmt_rgb565_pre pixfmt_pre; +typedef agg::rgba8 color_type; + +#elif defined(AGG_RGB555) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_rgb555 +typedef agg::pixfmt_rgb555 pixfmt; +typedef agg::pixfmt_rgb555_pre pixfmt_pre; +typedef agg::rgba8 color_type; + +#elif defined(AGG_RGB_AAA) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_rgbAAA +typedef agg::pixfmt_rgbAAA pixfmt; +typedef agg::pixfmt_rgbAAA_pre pixfmt_pre; +typedef agg::rgba16 color_type; + +#elif defined(AGG_BGR_AAA) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_bgrAAA +typedef agg::pixfmt_bgrAAA pixfmt; +typedef agg::pixfmt_bgrAAA_pre pixfmt_pre; +typedef agg::rgba16 color_type; + +#elif defined(AGG_RGB_BBA) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_rgbBBA +typedef agg::pixfmt_rgbBBA pixfmt; +typedef agg::pixfmt_rgbBBA_pre pixfmt_pre; +typedef agg::rgba16 color_type; + +#elif defined(AGG_BGR_ABB) + +#include "agg_pixfmt_rgb_packed.h" +#define pix_format agg::pix_format_bgrABB +typedef agg::pixfmt_bgrABB pixfmt; +typedef agg::pixfmt_bgrABB_pre pixfmt_pre; +typedef agg::rgba16 color_type; + +#else +#error Please define pixel format: AGG_GRAY8, AGG_BGR24, AGG_RGB24, etc. See this file above +#endif + +#endif diff --git a/framework/CPPAGGRenderer/include/savebmp.h b/framework/CPPAGGRenderer/include/savebmp.h new file mode 100644 index 00000000..6593badc --- /dev/null +++ b/framework/CPPAGGRenderer/include/savebmp.h @@ -0,0 +1,81 @@ +#include +#include +#include +using namespace std; + +int w = 1000; +int h = 660; + +void saveBMP(const unsigned char* buf, float width, float height, const char* name){ + + FILE *f; + unsigned char *img = NULL; + + w = width; + h = height; + + float red[w][h]; + float blue[w][h]; + float green[w][h]; + + for (int i = 0; i < w; i++) { + for (int j = 0, l = h-1; j < h; j++, l--) { + red[i][l] = *(buf + (3*(w*j + i))); + green[i][l] = *(buf + (3*(w*j + i) + 1)); + blue[i][l] = *(buf + (3*(w*j + i) + 2)); + } + } + + int filesize = 54 + 3*w*h; //w is your image width, h is image height, both int + + img = (unsigned char *)malloc(3*w*h); + memset(img,0,3*w*h); + + for(int i=0; i 255) r=255; + if (g > 255) g=255; + if (b > 255) b=255; + img[(x+y*w)*3+2] = (unsigned char)(r); + img[(x+y*w)*3+1] = (unsigned char)(g); + img[(x+y*w)*3+0] = (unsigned char)(b); + } + } + + unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0}; + unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0}; + unsigned char bmppad[3] = {0,0,0}; + + bmpfileheader[ 2] = (unsigned char)(filesize ); + bmpfileheader[ 3] = (unsigned char)(filesize>> 8); + bmpfileheader[ 4] = (unsigned char)(filesize>>16); + bmpfileheader[ 5] = (unsigned char)(filesize>>24); + + bmpinfoheader[ 4] = (unsigned char)( w ); + bmpinfoheader[ 5] = (unsigned char)( w>> 8); + bmpinfoheader[ 6] = (unsigned char)( w>>16); + bmpinfoheader[ 7] = (unsigned char)( w>>24); + bmpinfoheader[ 8] = (unsigned char)( h ); + bmpinfoheader[ 9] = (unsigned char)( h>> 8); + bmpinfoheader[10] = (unsigned char)( h>>16); + bmpinfoheader[11] = (unsigned char)( h>>24); + + f = fopen(name,"wb"); + fwrite(bmpfileheader,1,14,f); + fwrite(bmpinfoheader,1,40,f); + for(int i=0; i Float { + return get_text_width(text, size, agg_object) + } + + public func savePlotImage(fileName name : String) { + save_image(name, agg_object) + } + +} diff --git a/framework/Renderer/Source/Renderer.swift b/framework/Renderer/Source/Renderer.swift new file mode 100644 index 00000000..e35dbf18 --- /dev/null +++ b/framework/Renderer/Source/Renderer.swift @@ -0,0 +1,15 @@ +import Util +protocol Renderer { + + mutating func drawRect(topLeftPoint p1 : Point, topRightPoint p2 : Point, bottomRightPoint p3 : Point, bottomLeftPoint p4 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color) + mutating func drawSolidRect(topLeftPoint p1 : Point, topRightPoint p2 : Point, bottomRightPoint p3 : Point, bottomLeftPoint p4 : Point, fillColor fillColor : Color) + mutating func drawLine(startPoint p1 : Point, endPoint p2 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color) + mutating func drawTransformedLine(startPoint p1 : Point, endPoint p2 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color) + mutating func drawPlotLines(points p : [Point], strokeWidth thickness : Float, strokeColor strokeColor : Color) + mutating func drawText(text s : String, location p : Point, textSize size : Float, strokeWidth thickness : Float) + mutating func drawTransformedText(text s : String, location p : Point, textSize size : Float, strokeWidth thickness : Float, angle angle : Float) + mutating func drawRotatedText(text s : String, location p : Point, textSize size : Float, strokeWidth thickness : Float, angle angle : Float) + mutating func drawSolidRectWithBorder(topLeftPoint p1 : Point,topRightPoint p2 : Point,bottomRightPoint p3 : Point,bottomLeftPoint p4 : Point, strokeWidth thickness : Float, fillColor fillColor : Color, borderColor borderColor : Color) + func getTextWidth(text text : String, textSize size : Float) -> Float + +} diff --git a/framework/Renderer/Source/SVGRenderer.swift b/framework/Renderer/Source/SVGRenderer.swift new file mode 100644 index 00000000..a51dedb7 --- /dev/null +++ b/framework/Renderer/Source/SVGRenderer.swift @@ -0,0 +1,120 @@ +import Foundation +import Util + +//extension to get ascii value of character +extension Character { + var isAscii: Bool { + return unicodeScalars.allSatisfy { $0.isASCII } + } + var ascii: UInt32? { + return isAscii ? unicodeScalars.first?.value : nil + } +} + +public struct SVGRenderer : Renderer{ + + var LCARS_CHAR_SIZE_ARRAY : [Int]? + + var image : String + var width : Float + var height : Float + + public init(width w : Float = 1000, height h : Float = 660) { + width = w + height = h + image = "" + image = image + "\n" + ""; + LCARS_CHAR_SIZE_ARRAY = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 26, 46, 63, 42, 105, 45, 20, 25, 25, 47, 39, 21, 34, 26, 36, 36, 28, 36, 36, 36, 36, 36, 36, 36, 36, 27, 27, 36, 35, 36, 35, 65, 42, 43, 42, 44, 35, 34, 43, 46, 25, 39, 40, 31, 59, 47, 43, 41, 43, 44, 39, 28, 44, 43, 65, 37, 39, 34, 37, 42, 37, 50, 37, 32, 43, 43, 39, 43, 40, 30, 42, 45, 23, 25, 39, 23, 67, 45, 41, 43, 42, 30, 40, 28, 45, 33, 52, 33, 36, 31, 39, 26, 39, 55] + } + + public mutating func drawRect(topLeftPoint p1 : Point, topRightPoint p2 : Point, bottomRightPoint p3 : Point, bottomLeftPoint p4 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color = black) { + let w : Float = abs(p2.x - p1.x) + let h : Float = abs(p2.y - p3.y) + let rect : String = "" + image = image + "\n" + rect + } + + public mutating func drawSolidRect(topLeftPoint p1 : Point, topRightPoint p2 : Point, bottomRightPoint p3 : Point, bottomLeftPoint p4 : Point, fillColor fillColor : Color = white) { + let w : Float = abs(p2.x - p1.x) + let h : Float = abs(p2.y - p3.y) + let rect : String = "" + image = image + "\n" + rect + } + + public mutating func drawSolidRectWithBorder(topLeftPoint p1 : Point,topRightPoint p2 : Point,bottomRightPoint p3 : Point,bottomLeftPoint p4 : Point, strokeWidth thickness : Float, fillColor fillColor : Color = white, borderColor borderColor : Color = black) { + let w : Float = abs(p2.x - p1.x) + let h : Float = abs(p2.y - p3.y) + let rect : String = "" + image = image + "\n" + rect + } + + public mutating func drawLine(startPoint p1 : Point, endPoint p2 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color = black) { + let x0 = p1.x + var y0 = p1.y + let x1 = p2.x + var y1 = p2.y + y0 = height - y0 + y1 = height - y1 + let line = "" + image = image + "\n" + line + } + + public mutating func drawTransformedLine(startPoint p1 : Point, endPoint p2 : Point, strokeWidth thickness : Float, strokeColor strokeColor : Color) { + let x0 = p1.x + (0.1*width) + var y0 = p1.y + (0.1*height) + let x1 = p2.x + (0.1*width) + var y1 = p2.y + (0.1*height) + y0 = height - y0 + y1 = height - y1 + let line = "" + image = image + "\n" + line + } + + public mutating func drawPlotLines(points p : [Point], strokeWidth thickness : Float, strokeColor strokeColor : Color) { + for i in 0..\(s)" + image = image + "\n" + text + } + + public mutating func drawTransformedText(text s : String, location p : Point, textSize size : Float, strokeWidth thickness : Float, angle angle : Float = 0){ + let x1 = p.x + 0.1*width + let y1 = height - p.y - 0.1*height + let text = "\(s)" + image = image + "\n" + text + } + + public mutating func drawRotatedText(text s : String, location p : Point, textSize size : Float, strokeWidth thickness : Float, angle angle : Float = 0){ + let y1 = height - p.y + let text = "\(s)" + image = image + "\n" + text + } + + public func getTextWidth(text text : String, textSize size : Float) -> Float { + var width : Float = 0 + let scaleFactor = size/100.0 + + for i in 0.." + let url = URL(fileURLWithPath: "\(name).svg") + do { + try image.write(to: url, atomically: true, encoding: String.Encoding.utf8) + } catch { + print("Unable to save SVG image!") + } + } + +} diff --git a/framework/Util/color.swift b/framework/Util/color.swift new file mode 100644 index 00000000..3c7a0f11 --- /dev/null +++ b/framework/Util/color.swift @@ -0,0 +1,18 @@ +public struct Color{ + public var r : Float + public var g : Float + public var b : Float + public var a : Float + public init(_ r : Float, _ g : Float, _ b : Float, _ a : Float){ + self.r = g + self.g = g + self.b = b + self.a = a + } +} + +public let lightBlue : Color = Color(0.529,0.808,0.922,1.0) +public let transluscentWhite : Color = Color(1.0,1.0,1.0,0.8) +public let black : Color = Color(0.0, 0.0, 0.0, 1.0) +public let white : Color = Color(1.0, 1.0, 1.0, 1.0) +public let orange : Color = Color(1.0, 0.647, 0.0, 1.0) diff --git a/framework/Util/point.swift b/framework/Util/point.swift new file mode 100644 index 00000000..7b281745 --- /dev/null +++ b/framework/Util/point.swift @@ -0,0 +1,9 @@ +// class defining a point +public struct Point { + public var x : Float + public var y : Float + public init(_ x : Float, _ y : Float){ + self.x = x + self.y = y + } +}