Skip to content

Commit

Permalink
XMP metadata support in JPEGs
Browse files Browse the repository at this point in the history
git-svn-id: http://xee.googlecode.com/svn/trunk@9 b21bc358-9819-0410-9ac4-3585c3c20154
  • Loading branch information
paracelsus committed Aug 15, 2007
1 parent 956bd80 commit c4fd292
Show file tree
Hide file tree
Showing 37 changed files with 1,570 additions and 5,771 deletions.
3 changes: 2 additions & 1 deletion CSFileHandle.h
Expand Up @@ -15,8 +15,9 @@
-(id)initWithFilePointer:(FILE *)file closeOnDealloc:(BOOL)closeondealloc name:(NSString *)descname;
-(void)dealloc;

-(off_t)offsetInFile;
-(off_t)fileSize;
-(off_t)offsetInFile;
-(BOOL)atEndOfFile;

-(void)seekToFileOffset:(off_t)offs;
-(void)seekToEndOfFile;
Expand Down
13 changes: 9 additions & 4 deletions CSFileHandle.m
Expand Up @@ -57,6 +57,13 @@ -(void)dealloc



-(off_t)fileSize
{
struct stat s;
if(fstat(fileno(fh),&s)) [self _raiseError];
return s.st_size;
}

-(off_t)offsetInFile
{
#ifdef __MINGW__
Expand All @@ -66,11 +73,9 @@ -(off_t)offsetInFile
#endif
}

-(off_t)fileSize
-(BOOL)atEndOfFile
{
struct stat s;
if(fstat(fileno(fh),&s)) [self _raiseError];
return s.st_size;
return feof(fh);
}


Expand Down
6 changes: 5 additions & 1 deletion CSHandle.h
Expand Up @@ -12,8 +12,9 @@
-(id)initWithName:(NSString *)descname;
-(void)dealloc;

-(off_t)offsetInFile;
-(off_t)fileSize;
-(off_t)offsetInFile;
-(BOOL)atEndOfFile;
-(void)seekToFileOffset:(off_t)offs;
-(void)seekToEndOfFile;
-(void)pushBackByte:(int)byte;
Expand Down Expand Up @@ -51,6 +52,9 @@
-(NSData *)copyDataOfLength:(int)length;
-(void)readBytes:(int)num toBuffer:(void *)buffer;

-(CSHandle *)subHandleOfLength:(off_t)length;
-(CSHandle *)subHandleWithRange:(NSRange)range;

-(void)writeInt8:(int8_t)val;
-(void)writeUInt8:(uint8_t)val;

Expand Down
18 changes: 17 additions & 1 deletion CSHandle.m
@@ -1,4 +1,5 @@
#import "CSHandle.h"
#import "CSSubHandle.h"

#include <sys/stat.h>

Expand Down Expand Up @@ -43,9 +44,11 @@ -(void)dealloc



-(off_t)fileSize { [self _raiseNotImplemented]; return 0; }

-(off_t)offsetInFile { [self _raiseNotImplemented]; return 0; }

-(off_t)fileSize { [self _raiseNotImplemented]; return 0; }
-(BOOL)atEndOfFile { [self _raiseNotImplemented]; return NO; }

-(void)seekToFileOffset:(off_t)offs { [self _raiseNotImplemented]; }

Expand Down Expand Up @@ -169,6 +172,19 @@ -(void)readBytes:(int)num toBuffer:(void *)buffer
}



-(CSHandle *)subHandleOfLength:(off_t)length
{
return [[[CSSubHandle alloc] initWithHandle:self from:[self offsetInFile] length:length] autorelease];
}

-(CSHandle *)subHandleWithRange:(NSRange)range;
{
return [[[CSSubHandle alloc] initWithHandle:self from:range.location length:range.length] autorelease];
}



static inline void CSSetBEInt16(uint8_t *b,int16_t n) { b[0]=(n>>8)&0xff; b[1]=n&0xff; }
static inline void CSSetBEInt32(uint8_t *b,int32_t n) { b[0]=(n>>24)&0xff; b[1]=(n>>16)&0xff; b[2]=(n>>8)&0xff; b[3]=n&0xff; }
static inline void CSSetBEUInt16(uint8_t *b,uint16_t n) { b[0]=(n>>8)&0xff; b[1]=n&0xff; }
Expand Down
3 changes: 2 additions & 1 deletion CSMemoryHandle.h
Expand Up @@ -13,8 +13,9 @@
-(id)initWithData:(NSData *)dataobj;
-(void)dealloc;

-(off_t)offsetInFile;
-(off_t)fileSize;
-(off_t)offsetInFile;
-(BOOL)atEndOfFile;

-(void)seekToFileOffset:(off_t)offs;
-(void)seekToEndOfFile;
Expand Down
3 changes: 2 additions & 1 deletion CSMemoryHandle.m
Expand Up @@ -40,10 +40,11 @@ -(void)dealloc



-(off_t)fileSize { return [data length]; }

-(off_t)offsetInFile { return pos; }

-(off_t)fileSize { return [data length]; }
-(BOOL)atEndOfFile { return pos==[data length]; }



Expand Down
20 changes: 20 additions & 0 deletions CSSubHandle.h
@@ -0,0 +1,20 @@
#import "CSHandle.h"

@interface CSSubHandle:CSHandle
{
CSHandle *parent;
off_t start,end;
}

-(id)initWithHandle:(CSHandle *)handle from:(off_t)from length:(off_t)length;
-(void)dealloc;

-(off_t)fileSize;
-(off_t)offsetInFile;
-(BOOL)atEndOfFile;

-(void)seekToFileOffset:(off_t)offs;
-(void)seekToEndOfFile;
-(int)readAtMost:(int)num toBuffer:(void *)buffer;

@end
71 changes: 71 additions & 0 deletions CSSubHandle.m
@@ -0,0 +1,71 @@
#import "CSSubHandle.h"

@implementation CSSubHandle

-(id)initWithHandle:(CSHandle *)handle from:(off_t)from length:(off_t)length
{
if(self=[super init])
{
parent=[handle retain];
start=from;
end=from+length;

if(parent) return self;

[self release];
}
return nil;
}

-(void)dealloc
{
[parent release];
[super dealloc];
}

-(off_t)fileSize
{
off_t parentsize=[parent fileSize];
if(parentsize>end) return end-start;
else if(parentsize<start) return 0;
else return parentsize-start;
}

-(off_t)offsetInFile
{
return [parent offsetInFile]-start;
}

-(BOOL)atEndOfFile
{
return [parent offsetInFile]==end||[parent atEndOfFile];
}

-(void)seekToFileOffset:(off_t)offs
{
if(offs<0) [self _raiseNotSupported];
if(offs>end) [self _raiseEOF];
[parent seekToFileOffset:offs+start];
}

-(void)seekToEndOfFile
{
@try
{
[self seekToFileOffset:end];
}
@catch(NSException *e)
{
if([[e name] isEqual:@"CSEndOfFileException"]) [parent seekToEndOfFile];
else @throw e;
}
}

-(int)readAtMost:(int)num toBuffer:(void *)buffer
{
off_t curr=[parent offsetInFile];
if(curr+num>end) num=end-curr;
return [parent readAtMost:num toBuffer:buffer];
}

@end
5 changes: 4 additions & 1 deletion CSZlibHandle.h
Expand Up @@ -5,8 +5,9 @@
@interface CSZlibHandle:CSHandle
{
CSHandle *fh;
off_t startoffs;
z_stream zs;
BOOL inited;
BOOL inited,eof;
uint8_t inbuffer[128*1024];
}

Expand All @@ -17,8 +18,10 @@
-(void)dealloc;

-(off_t)offsetInFile;
-(BOOL)atEndOfFile;

-(void)seekToFileOffset:(off_t)offs;
-(void)seekToEndOfFile;
-(int)readAtMost:(int)num toBuffer:(void *)buffer;

-(void)_raiseZlib;
Expand Down
56 changes: 47 additions & 9 deletions CSZlibHandle.m
Expand Up @@ -30,7 +30,8 @@ -(id)initWithHandle:(CSHandle *)handle name:(NSString *)descname
if(self=[super initWithName:descname])
{
fh=[handle retain];
inited=NO;
startoffs=[fh offsetInFile];
inited=eof=NO;

zs.zalloc=Z_NULL;
zs.zfree=Z_NULL;
Expand Down Expand Up @@ -64,20 +65,57 @@ -(off_t)offsetInFile
return zs.total_out;
}

-(BOOL)atEndOfFile { return eof; }



-(void)seekToFileOffset:(off_t)offs
{
uint8_t dummybuf[16384];
int skip=offs-zs.total_out;
if(offs==0)
{
if(zs.total_out==0) return;

inflateEnd(&zs);
inited=NO;

zs.avail_in=0;
zs.next_in=Z_NULL;
if(inflateInit(&zs)!=Z_OK) [self _raiseZlib];

inited=YES;
}
else
{
int skip=offs-zs.total_out;

if(skip<0) [self _raiseNotSupported];
if(skip>0)
{
uint8_t dummybuf[16384];
while(skip)
{
int num=sizeof(dummybuf);
if(num>skip) num=skip;
skip-=[self readAtMost:num toBuffer:dummybuf];
}
}
else
{
[self seekToFileOffset:0];
[self seekToFileOffset:offs];
}
}
}

while(skip)
-(void)seekToEndOfFile
{
@try
{
[self seekToFileOffset:0x7fffffff];
}
@catch(NSException *e)
{
int num=sizeof(dummybuf);
if(num>skip) num=skip;
skip-=[self readAtMost:num toBuffer:dummybuf];
if([[e name] isEqual:@"CSEndOfFileException"]) return;
@throw e;
}
}

Expand All @@ -95,7 +133,7 @@ -(int)readAtMost:(int)num toBuffer:(void *)buffer
}

int err=inflate(&zs,0);
if(err==Z_STREAM_END) break;
if(err==Z_STREAM_END) { eof=YES; break; }
else if(err!=Z_OK) [self _raiseZlib];
}

Expand Down

0 comments on commit c4fd292

Please sign in to comment.