summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-01-26 00:36:55 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-01-26 00:36:55 (GMT)
commitc116a0849c7531ec4d0e59b6d61aaf6137dcd568 (patch)
tree39a8de4972d00aff8baaa05a51fda66d75fc6cdb
parente80d847e3d9249d22bf70f257df7b2130487b211 (diff)
undo experiment with object orientation
-rw-r--r--openscad.pro7
-rw-r--r--src/OffscreenContext.h2
-rw-r--r--src/OffscreenContextCGL.mm (renamed from src/OffscreenContext.mm)0
-rw-r--r--src/OffscreenView.h2
-rw-r--r--src/export_png.cc1
-rw-r--r--src/imageutils-lodepng.cc25
-rw-r--r--src/imageutils-macosx.cc59
-rw-r--r--src/imageutils.cc14
-rw-r--r--src/imageutils.h1
9 files changed, 51 insertions, 60 deletions
diff --git a/openscad.pro b/openscad.pro
index 51f4439..4fdd11e 100644
--- a/openscad.pro
+++ b/openscad.pro
@@ -232,7 +232,6 @@ HEADERS += src/version_check.h \
src/svg.h \
\
src/lodepng.h \
- src/OffscreenContext.h \
src/OffscreenView.h \
src/fbo.h \
src/imageutils.h \
@@ -306,16 +305,16 @@ SOURCES += src/version_check.cc \
src/mainwin.cc
unix:!macx {
- SOURCES += src/OffscreenContextGLX.cc
SOURCES += src/imageutils-lodepng.cc
+ SOURCES += src/OffscreenContextGLX.cc
}
macx {
- SOURCES += src/OffscreenContext.mm
SOURCES += src/imageutils-macosx.cc
+ SOURCES += src/OffscreenContextCGL.mm
}
win32* {
- SOURCES += src/OffscreenContextWGL.cc
SOURCES += src/imageutils-lodepng.cc
+ SOURCES += src/OffscreenContextWGL.cc
}
opencsg {
diff --git a/src/OffscreenContext.h b/src/OffscreenContext.h
index 6eebcba..8645da7 100644
--- a/src/OffscreenContext.h
+++ b/src/OffscreenContext.h
@@ -1,7 +1,7 @@
#ifndef OFFSCREENCONTEXT_H_
#define OFFSCREENCONTEXT_H_
-#include <iostream> // for error output
+#include <iostream>
#include <string>
struct OffscreenContext *create_offscreen_context(int w, int h);
diff --git a/src/OffscreenContext.mm b/src/OffscreenContextCGL.mm
index a0995fa..a0995fa 100644
--- a/src/OffscreenContext.mm
+++ b/src/OffscreenContextCGL.mm
diff --git a/src/OffscreenView.h b/src/OffscreenView.h
index a44e0c1..f401bef 100644
--- a/src/OffscreenView.h
+++ b/src/OffscreenView.h
@@ -9,6 +9,7 @@
#include <stdint.h>
#endif
#include "system-gl.h"
+#include <iostream>
class OffscreenView
{
@@ -24,6 +25,7 @@ public:
void setupOrtho(bool offset=false);
void paintGL();
bool save(const char *filename);
+ bool save(std::ostream &output);
std::string getInfo();
GLint shaderinfo[11];
diff --git a/src/export_png.cc b/src/export_png.cc
index 16e2a69..d983bc1 100644
--- a/src/export_png.cc
+++ b/src/export_png.cc
@@ -17,6 +17,7 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output)
csgInfo.glview = new OffscreenView(512,512);
} catch (int error) {
fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error);
+ return;
}
CGALRenderer cgalRenderer(*root_N);
diff --git a/src/imageutils-lodepng.cc b/src/imageutils-lodepng.cc
index a74f107..96d9d90 100644
--- a/src/imageutils-lodepng.cc
+++ b/src/imageutils-lodepng.cc
@@ -1,35 +1,16 @@
+#include "imageutils.h"
#include "lodepng.h"
#include <stdio.h>
#include <stdlib.h>
bool write_png(std::ostream &output, unsigned char *pixels, int width, int height)
{
- size_t dataout_size = -1;
- unsigned char *dataout = (unsigned char *)malloc(width*height*4);
- LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8);
- output.write( dataout, dataout_size );;
- free( dataout );
- return true;
-}
-
-bool write_png(const char *filename, unsigned char *pixels, int width, int height)
-{
//encoder.settings.zlibsettings.windowSize = 2048;
//LodePNG_Text_add(&encoder.infoPng.text, "Comment", "Created with LodePNG");
-
size_t dataout_size = -1;
unsigned char *dataout = (unsigned char *)malloc(width*height*4);
LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8);
- //LodePNG_saveFile(dataout, dataout_size, "blah2.png");
-
- FILE *f = fopen(filename, "wb");
- if (!f) {
- free(dataout);
- return false;
- }
-
- fwrite(dataout, 1, dataout_size, f);
- fclose(f);
- free(dataout);
+ output.write( reinterpret_cast<const char*>(dataout), dataout_size );;
+ free( dataout );
return true;
}
diff --git a/src/imageutils-macosx.cc b/src/imageutils-macosx.cc
index 8074f3f..fc4803b 100644
--- a/src/imageutils-macosx.cc
+++ b/src/imageutils-macosx.cc
@@ -1,40 +1,34 @@
#include <ApplicationServices/ApplicationServices.h>
#include <CGDataConsumer.h>
-#include <iostream>
+#include "imageutils.h"
-CGDataConsumerCallbacks callbacks;
+CGDataConsumerCallbacks dc_callbacks;
size_t write_bytes_to_ostream (void *info,const void *buffer,size_t count)
{
- assert( info );
+ assert( info && buffer );
std::ostream *output = (std::ostream *)info;
- output->write( (const char *) buffer, count );
- return count;
+ size_t startpos = output->tellp();
+ size_t endpos = startpos;
+ try {
+ output->write( reinterpret_cast<const char *>buffer, count );
+ endpos = output->tellp();
+ } catch (const std::ios_base::failure& e)
+ std::cerr << "Error writing to ostream:" << e.what() << "\n";
+ }
+ return endpos-startpos;
}
CGDataConsumerRef dataconsumer CGDataConsumerCreateWithOstream(std::ostream &output)
{
- callbacks.putBytes = write_bytes_to_ostream;
- callbacks.releaseConsumer = NULL;
- CGDataConsumerRef dc = CGDataConsumerCreate ( (void *)output, &callbacks );
+ dc_callbacks.putBytes = write_bytes_to_ostream;
+ dc_callbacks.releaseConsumer = NULL; // ostream closed by caller of write_png
+ CGDataConsumerRef dc = CGDataConsumerCreate ( (void *)(&output), &dc_callbacks );
return dc;
}
-bool write_png(const char *filename, unsigned char *pixels, int width, int height)
-{
- assert( filename );
- std::stringstream dummy;
- write_png_base( filename, dummy, pixels, width, height );
-}
-
bool write_png(std::ostream &output, unsigned char *pixels, int width, int height)
{
- write_png_base( NULL, output, pixels, width, height );
-}
-
-bool write_png_base(const char *filename, std::ostream &output, unsigned char *pixels, int width, int height)
-{
-
size_t rowBytes = width * 4;
// CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
@@ -54,19 +48,18 @@ bool write_png_base(const char *filename, std::ostream &output, unsigned char *p
return false;
}
- if ( filename == NULL ) {
- CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
- } else {
- CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8);
- CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
- fname, kCFURLPOSIXPathStyle, false);
- if (!fileURL) {
- std::cerr << "Unable to create file URL ref.";
- return false;
- }
+ CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
+ /*
+ CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8);
+ CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
+ fname, kCFURLPOSIXPathStyle, false);
+ if (!fileURL) {
+ std::cerr << "Unable to create file URL ref.";
+ return false;
+ }
- CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL);
- }
+ CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL);
+ */
CFIndex fileImageIndex = 1;
CFMutableDictionaryRef fileDict = NULL;
diff --git a/src/imageutils.cc b/src/imageutils.cc
index 7ee14e9..eaab3b7 100644
--- a/src/imageutils.cc
+++ b/src/imageutils.cc
@@ -1,5 +1,6 @@
#include "imageutils.h"
#include <string.h>
+#include <fstream>
void flip_image(const unsigned char *src, unsigned char *dst, size_t pixelsize, size_t width, size_t height)
{
@@ -8,3 +9,16 @@ void flip_image(const unsigned char *src, unsigned char *dst, size_t pixelsize,
memmove(dst + (height - i - 1) * rowBytes, src + i * rowBytes, rowBytes);
}
}
+
+bool write_png(const char *filename, unsigned char *pixels, int width, int height) {
+ std::ofstream fstream( filename );
+ if (fstream.is_open()) {
+ write_png( fstream, pixels, width, height );
+ fstream.close();
+ return true;
+ } else {
+ std::cerr << "Can't open file " << filename << " for export.";
+ return false;
+ }
+}
+
diff --git a/src/imageutils.h b/src/imageutils.h
index b8d1663..c9bb8de 100644
--- a/src/imageutils.h
+++ b/src/imageutils.h
@@ -2,6 +2,7 @@
#define IMAGEUTILS_H_
#include <stdlib.h>
+#include <iostream>
bool write_png(const char *filename, unsigned char *pixels, int width, int height);
bool write_png(std::ostream &output, unsigned char *pixels, int width, int height);
contact: Jan Huwald // Impressum