Skip to content

Commit

Permalink
O2DataConsumerCreateWithURL implementation is missing + adding a few …
Browse files Browse the repository at this point in the history
…API to have a more complete CGDataConsumer implementation
  • Loading branch information
airy10 committed Nov 4, 2011
1 parent f509741 commit 3dda0a1
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 18 deletions.
12 changes: 12 additions & 0 deletions CoreGraphics/CGDataConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI

typedef struct O2DataConsumer *CGDataConsumerRef;

typedef size_t (*CGDataConsumerPutBytesCallback) (void *info, const void *buffer, size_t count);

typedef void (*CGDataConsumerReleaseInfoCallback) (void *info);

typedef struct O2DataConsumerCallbacks {
CGDataConsumerPutBytesCallback putBytes;
CGDataConsumerReleaseInfoCallback releaseConsumer;
} CGDataConsumerCallbacks;

COREGRAPHICS_EXPORT CGDataConsumerRef CGDataConsumerCreateWithCFData(CFMutableDataRef data);
COREGRAPHICS_EXPORT CGDataConsumerRef CGDataConsumerCreate(void *info, const CGDataConsumerCallbacks *callbacks);
COREGRAPHICS_EXPORT CGDataConsumerRef CGDataConsumerCreateWithURL(CFURLRef url);

COREGRAPHICS_EXPORT void CGDataConsumerRelease(CGDataConsumerRef self);
8 changes: 8 additions & 0 deletions CoreGraphics/CGDataConsumer.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <CoreGraphics/CGDataConsumer.h>
#import <Onyx2D/O2DataConsumer.h>

CGDataConsumerRef CGDataConsumerCreate(void *info, const CGDataConsumerCallbacks *callbacks) {
return O2DataConsumerCreate(info, (const O2DataConsumerCallbacks *)callbacks);
}

CGDataConsumerRef CGDataConsumerCreateWithURL(CFURLRef url) {
return O2DataConsumerCreateWithURL(url);
}

CGDataConsumerRef CGDataConsumerCreateWithCFData(CFMutableDataRef data) {
return O2DataConsumerCreateWithCFData(data);
Expand Down
29 changes: 21 additions & 8 deletions Onyx2D/O2DataConsumer.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
/* Copyright (c) 2009 Christopher J. W. Lloyd <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <Foundation/NSObject.h>
#import <Foundation/NSData.h>
#import <Foundation/NSFileHandle.h>
#import <CoreFoundation/CFData.h>
#import <CoreFoundation/CFURL.h>

@class O2DataConsumer;
typedef O2DataConsumer *O2DataConsumerRef;

// These types and structures must match the corresponding CG ones
typedef size_t (*O2DataConsumerPutBytesCallback)(void *info, const void *buffer, size_t count);

typedef void (*O2DataConsumerReleaseInfoCallback)(void *info);

struct O2DataConsumerCallbacks {
O2DataConsumerPutBytesCallback putBytes;
O2DataConsumerReleaseInfoCallback releaseConsumer;
}
typedef struct O2DataConsumerCallbacks O2DataConsumerCallbacks;

@interface O2DataConsumer : NSObject {
NSMutableData *_data;
size_t _position;
void *_info;
O2DataConsumerCallbacks _callbacks;
}

-(NSMutableData *)mutableData;

O2DataConsumerRef O2DataConsumerCreate(void *info, const O2DataConsumerCallbacks *callbacks);
O2DataConsumerRef O2DataConsumerCreateWithCFData(CFMutableDataRef data);
O2DataConsumerRef O2DataConsumerCreateWithURL(CFURLRef url);
O2DataConsumerRef O2DataConsumerRetain(O2DataConsumerRef self);
Expand Down
109 changes: 100 additions & 9 deletions Onyx2D/O2DataConsumer.m
Original file line number Diff line number Diff line change
@@ -1,29 +1,121 @@
#import <Onyx2D/O2DataConsumer.h>
#import <Onyx2D/O2Exceptions.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSFileManager.h>

@implementation O2DataConsumer

-initWithMutableData:(NSMutableData *)data {
_data=[data retain];
return self;
static size_t O2DataConsumerDataPutBytesCallback(void *info, const void *buffer, size_t count)
{
NSMutableData *data = (NSMutableData *)info;
[data appendBytes:buffer length:count];
return count;
}

static void O2DataConsumerDataReleaseInfoCallback(void *info)
{
NSMutableData *data = (NSMutableData *)info;
[data release];
}

static size_t O2DataConsumerFilePutBytesCallback(void *info, const void *buffer, size_t count)
{
NSFileHandle *handle = (NSFileHandle *)info;
NSData *data = [NSData dataWithBytes:buffer length:count];
@try {
[handle writeData:data];
}
@catch (NSException * e) {
NSLog(@"%@: writing error : %@", handle, e);
count = 0;
}
@finally {
}
return count;
}

static void O2DataConsumerFileReleaseInfoCallback(void *info)
{
NSFileHandle *handle = (NSFileHandle *)info;
[handle closeFile];
[handle release];
}


-initWithInfo:(void *)info callbacks:(const O2DataConsumerCallbacks *)callbacks
{
if ((self = [super init])) {
_info = info;
if (callbacks) {
_callbacks = *callbacks;
}
}
return self;
}

-initWithMutableData:(NSMutableData *)data
{
data=[data retain]; // Will be released by the release callback
O2DataConsumerCallbacks callbacks = {
O2DataConsumerDataPutBytesCallback,
O2DataConsumerDataReleaseInfoCallback
};
return [self initWithInfo:data callbacks:&callbacks];
}

-initWithURL:(NSURL *)url
{
if ([url isFileURL]) {
NSString *path = [url path];
// Create the file and get an handle on it
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];

O2DataConsumerCallbacks callbacks = {
O2DataConsumerFilePutBytesCallback,
O2DataConsumerFileReleaseInfoCallback
};
[fileHandle retain]; // Will be released by the release callback
return [self initWithInfo:fileHandle callbacks:&callbacks];
} else {
NSLog(@"O2DataConsumerCreateWithURL: %@ is not a file URL", url);
[self release];
return nil;
}
return self;
}

-(void)dealloc {
[_data release];
if (_callbacks.releaseConsumer) {
_callbacks.releaseConsumer(_info);
}
[super dealloc];
}

- (size_t)putBytes:(const void *)buffer count:(size_t)count
{
if (_callbacks.putBytes) {
count = _callbacks.putBytes(_info, buffer, count);
}
return count;
}

// Only works with NSData based consumer
-(NSMutableData *)mutableData {
return _data;
return (NSMutableData *)_info;
}

O2DataConsumerRef O2DataConsumerCreate(void *info, const O2DataConsumerCallbacks *callbacks)
{
return [[O2DataConsumer alloc] initWithInfo:info callbacks:callbacks];
}

O2DataConsumerRef O2DataConsumerCreateWithCFData(CFMutableDataRef data) {
return [[O2DataConsumer alloc] initWithMutableData:(NSMutableData *)data];
}

O2DataConsumerRef O2DataConsumerCreateWithURL(CFURLRef url) {
O2UnimplementedFunction();
return NULL;
return [[O2DataConsumer alloc] initWithURL:(NSURL *)url];
}

O2DataConsumerRef O2DataConsumerRetain(O2DataConsumerRef self) {
Expand All @@ -36,8 +128,7 @@ void O2DataConsumerRelease(O2DataConsumerRef self) {
}

size_t O2DataConsumerPutBytes(O2DataConsumerRef self,const void *buffer,size_t count) {
[self->_data appendBytes:buffer length:count];
return count;
return [self putBytes:buffer count:count];
}

@end
Expand Down
4 changes: 3 additions & 1 deletion Onyx2D/O2ImageDestination.m
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ bool O2ImageDestinationFinalize(O2ImageDestinationRef self) {
case O2ImageFileJPEG2000:
break;
}

[self->_consumer release]; // This is needed so the consumer can finalize its work before we exit this function
self->_consumer = nil;

return TRUE;
}

Expand Down

0 comments on commit 3dda0a1

Please sign in to comment.