undo experiment with object orientation

felipesanches-svg
don bright 2013-01-26 01:36:55 +01:00
parent e80d847e3d
commit c116a0849c
9 changed files with 52 additions and 61 deletions

View File

@ -232,7 +232,6 @@ HEADERS += src/version_check.h \
src/svg.h \ src/svg.h \
\ \
src/lodepng.h \ src/lodepng.h \
src/OffscreenContext.h \
src/OffscreenView.h \ src/OffscreenView.h \
src/fbo.h \ src/fbo.h \
src/imageutils.h \ src/imageutils.h \
@ -306,16 +305,16 @@ SOURCES += src/version_check.cc \
src/mainwin.cc src/mainwin.cc
unix:!macx { unix:!macx {
SOURCES += src/OffscreenContextGLX.cc
SOURCES += src/imageutils-lodepng.cc SOURCES += src/imageutils-lodepng.cc
SOURCES += src/OffscreenContextGLX.cc
} }
macx { macx {
SOURCES += src/OffscreenContext.mm
SOURCES += src/imageutils-macosx.cc SOURCES += src/imageutils-macosx.cc
SOURCES += src/OffscreenContextCGL.mm
} }
win32* { win32* {
SOURCES += src/OffscreenContextWGL.cc
SOURCES += src/imageutils-lodepng.cc SOURCES += src/imageutils-lodepng.cc
SOURCES += src/OffscreenContextWGL.cc
} }
opencsg { opencsg {

View File

@ -1,7 +1,7 @@
#ifndef OFFSCREENCONTEXT_H_ #ifndef OFFSCREENCONTEXT_H_
#define OFFSCREENCONTEXT_H_ #define OFFSCREENCONTEXT_H_
#include <iostream> // for error output #include <iostream>
#include <string> #include <string>
struct OffscreenContext *create_offscreen_context(int w, int h); struct OffscreenContext *create_offscreen_context(int w, int h);

View File

@ -9,6 +9,7 @@
#include <stdint.h> #include <stdint.h>
#endif #endif
#include "system-gl.h" #include "system-gl.h"
#include <iostream>
class OffscreenView class OffscreenView
{ {
@ -24,6 +25,7 @@ public:
void setupOrtho(bool offset=false); void setupOrtho(bool offset=false);
void paintGL(); void paintGL();
bool save(const char *filename); bool save(const char *filename);
bool save(std::ostream &output);
std::string getInfo(); std::string getInfo();
GLint shaderinfo[11]; GLint shaderinfo[11];

View File

@ -17,6 +17,7 @@ void export_png_with_cgal(CGAL_Nef_polyhedron *root_N, std::ostream &output)
csgInfo.glview = new OffscreenView(512,512); csgInfo.glview = new OffscreenView(512,512);
} catch (int error) { } catch (int error) {
fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error); fprintf(stderr,"Can't create OpenGL OffscreenView. Code: %i.\n", error);
return;
} }
CGALRenderer cgalRenderer(*root_N); CGALRenderer cgalRenderer(*root_N);

View File

@ -1,35 +1,16 @@
#include "imageutils.h"
#include "lodepng.h" #include "lodepng.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
bool write_png(std::ostream &output, unsigned char *pixels, int width, int height) bool write_png(std::ostream &output, 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; size_t dataout_size = -1;
unsigned char *dataout = (unsigned char *)malloc(width*height*4); unsigned char *dataout = (unsigned char *)malloc(width*height*4);
LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8); LodePNG_encode(&dataout, &dataout_size, pixels, width, height, LCT_RGBA, 8);
output.write( dataout, dataout_size );; output.write( reinterpret_cast<const char*>(dataout), dataout_size );;
free( dataout ); free( dataout );
return true; 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);
return true;
}

View File

@ -1,40 +1,34 @@
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
#include <CGDataConsumer.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) 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; std::ostream *output = (std::ostream *)info;
output->write( (const char *) buffer, count ); size_t startpos = output->tellp();
return count; 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) CGDataConsumerRef dataconsumer CGDataConsumerCreateWithOstream(std::ostream &output)
{ {
callbacks.putBytes = write_bytes_to_ostream; dc_callbacks.putBytes = write_bytes_to_ostream;
callbacks.releaseConsumer = NULL; dc_callbacks.releaseConsumer = NULL; // ostream closed by caller of write_png
CGDataConsumerRef dc = CGDataConsumerCreate ( (void *)output, &callbacks ); CGDataConsumerRef dc = CGDataConsumerCreate ( (void *)(&output), &dc_callbacks );
return dc; 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) 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; size_t rowBytes = width * 4;
// CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); // CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
@ -54,19 +48,18 @@ bool write_png_base(const char *filename, std::ostream &output, unsigned char *p
return false; return false;
} }
if ( filename == NULL ) { CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output);
CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithOstream(output); /*
} else { CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8);
CFStringRef fname = CFStringCreateWithCString(kCFAllocatorDefault, filename, kCFStringEncodingUTF8); CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
CFURLRef fileURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, fname, kCFURLPOSIXPathStyle, false);
fname, kCFURLPOSIXPathStyle, false); if (!fileURL) {
if (!fileURL) { std::cerr << "Unable to create file URL ref.";
std::cerr << "Unable to create file URL ref."; return false;
return false; }
}
CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL); CGDataConsumerRef dataconsumer = CGDataConsumerCreateWithURL(fileURL);
} */
CFIndex fileImageIndex = 1; CFIndex fileImageIndex = 1;
CFMutableDictionaryRef fileDict = NULL; CFMutableDictionaryRef fileDict = NULL;

View File

@ -1,5 +1,6 @@
#include "imageutils.h" #include "imageutils.h"
#include <string.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) 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); 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;
}
}

View File

@ -2,6 +2,7 @@
#define IMAGEUTILS_H_ #define IMAGEUTILS_H_
#include <stdlib.h> #include <stdlib.h>
#include <iostream>
bool write_png(const char *filename, unsigned char *pixels, int width, int height); 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); bool write_png(std::ostream &output, unsigned char *pixels, int width, int height);