Skip to content

Commit

Permalink
Fixed background error
Browse files Browse the repository at this point in the history
  • Loading branch information
unixpickle committed Nov 5, 2011
1 parent fc7ce1a commit aaf644d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
7 changes: 7 additions & 0 deletions ANGif/LZW/LZWSpoof.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#define BitOutOfRangeException @"BitOutOfRangeException"

void LZWDataAddBit (UInt8 ** _bytePool, NSUInteger * _totalSize, NSUInteger * numBits, BOOL flag);
BOOL LZWDataGetBit (UInt8 * _bytePool, NSUInteger bitIndex);

@interface LZWSpoof : NSObject {
UInt8 * _bytePool;
NSUInteger _totalSize;
Expand All @@ -23,6 +26,10 @@
- (id)initWithData:(NSData *)initialData;
- (void)addBit:(BOOL)flag;
- (BOOL)getBitAtIndex:(NSUInteger)bitIndex;

- (void)addLZWClearCode;
- (void)addByte:(NSUInteger)startBit fromBuffer:(LZWSpoof *)source;

- (NSData *)convertToData;

@end
72 changes: 47 additions & 25 deletions ANGif/LZW/LZWSpoof.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ + (NSData *)lzwExpandData:(NSData *)existingData {
// loop through every byte, write it, and then write a clear code.
for (NSUInteger byteIndex = 0; byteIndex < [existingData length]; byteIndex++) {
// insert NULL start bit
for (NSUInteger bitIndex = byteIndex * 8; bitIndex < (byteIndex + 1) * 8; bitIndex++) {
// NSUInteger bitIndexFlip = (bitIndex - (bitIndex % 8)) + (7 - (bitIndex % 8));
[destinationBuffer addBit:[existingBuffer getBitAtIndex:bitIndex]];
}
// for (NSUInteger bitIndex = byteIndex * 8; bitIndex < (byteIndex + 1) * 8; bitIndex++) {
// // NSUInteger bitIndexFlip = (bitIndex - (bitIndex % 8)) + (7 - (bitIndex % 8));
// [destinationBuffer addBit:[existingBuffer getBitAtIndex:bitIndex]];
// }
[destinationBuffer addByte:(byteIndex * 8) fromBuffer:existingBuffer];
[destinationBuffer addBit:NO];
// add clear code (TODO: make this less frequent)
for (int i = 0; i < 8; i++) {
[destinationBuffer addBit:NO];
}
[destinationBuffer addBit:YES];
[destinationBuffer addLZWClearCode];
}

// LZW "STOP" directive
Expand Down Expand Up @@ -75,20 +73,7 @@ - (id)init {
}

- (void)addBit:(BOOL)flag {
NSUInteger endNumber = numBits + 1;
if (endNumber / 8 + (endNumber % 8 == 0 ? 0 : 1) > _totalSize) {
_totalSize += kAllocBufferSize;
_bytePool = (UInt8 *)realloc(_bytePool, _totalSize);
}
NSUInteger byteIndex = numBits / 8;
// UInt8 byteMask = (1 << (7 - (numBits % 8)));
UInt8 byteMask = (1 << (numBits % 8));
if (flag) {
_bytePool[byteIndex] |= byteMask;
} else {
_bytePool[byteIndex] &= (0xff ^ byteMask);
}
numBits += 1;
LZWDataAddBit(&_bytePool, &_totalSize, &numBits, flag);
}

- (BOOL)getBitAtIndex:(NSUInteger)bitIndex {
Expand All @@ -97,11 +82,26 @@ - (BOOL)getBitAtIndex:(NSUInteger)bitIndex {
reason:@"The specified bit index is beyond the bounds of the buffer."
userInfo:nil];
}
NSUInteger byteIndex = bitIndex / 8;
UInt8 byteMask = (1 << (bitIndex % 8));
return (((_bytePool[byteIndex] & byteMask) == 0) ? NO : YES);
return LZWDataGetBit(_bytePool, bitIndex);
}

#pragma mark LZW

- (void)addLZWClearCode {
for (int i = 0; i < 8; i++) {
LZWDataAddBit(&_bytePool, &_totalSize, &numBits, NO);
}
LZWDataAddBit(&_bytePool, &_totalSize, &numBits, YES);
}

- (void)addByte:(NSUInteger)startBit fromBuffer:(LZWSpoof *)source {
for (NSUInteger bitIndex = startBit; bitIndex < startBit + 8; bitIndex++) {
LZWDataAddBit(&_bytePool, &_totalSize, &numBits, [source getBitAtIndex:bitIndex]);
}
}

#pragma mark Data

- (NSData *)convertToData {
NSUInteger numBytes = numBits / 8 + (numBits % 8 == 0 ? 0 : 1);
return [NSData dataWithBytes:_bytePool length:numBytes];
Expand All @@ -115,3 +115,25 @@ - (void)dealloc {
}

@end

void LZWDataAddBit (UInt8 ** _bytePool, NSUInteger * _totalSize, NSUInteger * numBits, BOOL flag) {
NSUInteger endNumber = *numBits + 1;
if (endNumber / 8 + (endNumber % 8 == 0 ? 0 : 1) > *_totalSize) {
*_totalSize += kAllocBufferSize;
*_bytePool = (UInt8 *)realloc(*_bytePool, *_totalSize);
}
NSUInteger byteIndex = *numBits / 8;
UInt8 byteMask = (1 << (*numBits % 8));
if (flag) {
(*_bytePool)[byteIndex] |= byteMask;
} else {
(*_bytePool)[byteIndex] &= (0xff ^ byteMask);
}
numBits += 1;
}

BOOL LZWDataGetBit (UInt8 * _bytePool, NSUInteger bitIndex) {
NSUInteger byteIndex = bitIndex / 8;
UInt8 byteMask = (1 << (bitIndex % 8));
return (((_bytePool[byteIndex] & byteMask) == 0) ? NO : YES);
}
2 changes: 1 addition & 1 deletion Classes/ExportViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (id)initWithImages:(NSArray *)imageArray {
#else
images = [imageArray retain];
#endif
self.backgroundColor = [UIColor whiteColor];
self.view.backgroundColor = [UIColor whiteColor];
}
return self;
}
Expand Down

0 comments on commit aaf644d

Please sign in to comment.