Skip to content

Commit

Permalink
added texture size and allocation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
racarone committed Dec 12, 2015
1 parent 716955a commit b3fbf5e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion sparrow/src/Classes/SPTexture.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,34 @@ - (instancetype)initWithWidth:(float)width height:(float)height generateMipmaps:
legalHeight = height * scale;
}

CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceRGB();
if (legalWidth < 1 || legalHeight < 1)
[NSException raise:SPExceptionInvalidOperation
format:@"Invalid texture size [%dx%d@%d]."
@"Width and height must be greater than or equal to 1.",
(int)legalWidth, (int)legalHeight, (int)scale];

CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast;
int bytesPerPixel = 4;

void *imageData = calloc(legalWidth * legalHeight * bytesPerPixel, 1);
if (!imageData)
{
SPLog(@"Error allocating image data!");
return nil;
}

CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(imageData, legalWidth, legalHeight, 8,
bytesPerPixel * legalWidth, cgColorSpace,
bitmapInfo);
CGColorSpaceRelease(cgColorSpace);

if (!context)
{
SPLog(@"Error creating CGBitmapContext!");
return nil;
}

// UIKit referential is upside down - we flip it and apply the scale factor
CGContextTranslateCTM(context, 0.0f, legalHeight);
CGContextScaleCTM(context, scale, -scale);
Expand Down

0 comments on commit b3fbf5e

Please sign in to comment.