diff options
author | don bright <hugh.m.bright@gmail.com> | 2013-01-26 00:36:55 (GMT) |
---|---|---|
committer | don bright <hugh.m.bright@gmail.com> | 2013-01-26 00:36:55 (GMT) |
commit | c116a0849c7531ec4d0e59b6d61aaf6137dcd568 (patch) | |
tree | 39a8de4972d00aff8baaa05a51fda66d75fc6cdb /src/imageutils-macosx.cc | |
parent | e80d847e3d9249d22bf70f257df7b2130487b211 (diff) |
undo experiment with object orientation
Diffstat (limited to 'src/imageutils-macosx.cc')
-rw-r--r-- | src/imageutils-macosx.cc | 59 |
1 files changed, 26 insertions, 33 deletions
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; |