| 1 |
#import <Foundation/Foundation.h> |
| 2 |
#import <UIKit/UIKit.h> |
| 3 |
|
| 4 |
#include "allegro5/allegro.h" |
| 5 |
#include "allegro5/allegro_image.h" |
| 6 |
#include "allegro5/internal/aintern_image.h" |
| 7 |
|
| 8 |
#include "iio.h" |
| 9 |
|
| 10 |
ALLEGRO_DEBUG_CHANNEL("image") |
| 11 |
|
| 12 |
static ALLEGRO_BITMAP *really_load_image(char *buffer, int size, int flags) |
| 13 |
{ |
| 14 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 15 |
|
| 16 |
ALLEGRO_BITMAP *bmp = NULL; |
| 17 |
void *pixels = NULL; |
| 18 |
/* Note: buffer is now owned (and later freed) by the data object. */ |
| 19 |
NSData *nsdata = [NSData dataWithBytesNoCopy:buffer length:size]; |
| 20 |
UIImage *uiimage = [UIImage imageWithData:nsdata]; |
| 21 |
int w = uiimage.size.width; |
| 22 |
int h = uiimage.size.height; |
| 23 |
bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA); |
| 24 |
|
| 25 |
/* Now we need to draw the image into a memory buffer. */ |
| 26 |
pixels = al_malloc(w * h * 4); |
| 27 |
CGFloat whitePoint[3] = { |
| 28 |
1, 1, 1 |
| 29 |
}; |
| 30 |
CGFloat blackPoint[3] = { |
| 31 |
0, 0, 0 |
| 32 |
}; |
| 33 |
CGFloat gamma[3] = { |
| 34 |
2.2, 2.2, 2.2 |
| 35 |
}; |
| 36 |
CGFloat matrix[9] = { |
| 37 |
1, 1, 1, |
| 38 |
1, 1, 1, |
| 39 |
1, 1, 1 |
| 40 |
}; |
| 41 |
/* This sets up a color space that results in identical values |
| 42 |
* as the image data itself, which is the same as the standalone |
| 43 |
* libpng loader |
| 44 |
*/ |
| 45 |
CGColorSpaceRef cs = |
| 46 |
CGColorSpaceCreateCalibratedRGB( |
| 47 |
whitePoint, blackPoint, gamma, matrix |
| 48 |
); |
| 49 |
CGContextRef context = CGBitmapContextCreate(pixels, w, h, 8, w * 4, |
| 50 |
cs, kCGImageAlphaPremultipliedLast); |
| 51 |
CGContextSetBlendMode(context, kCGBlendModeCopy); |
| 52 |
CGContextDrawImage(context, CGRectMake(0.0, 0.0, (CGFloat)w, (CGFloat)h), |
| 53 |
uiimage.CGImage); |
| 54 |
CGContextRelease(context); |
| 55 |
CGColorSpaceRelease(cs); |
| 56 |
|
| 57 |
/* Then create a bitmap out of the memory buffer. */ |
| 58 |
bmp = al_create_bitmap(w, h); |
| 59 |
if (!bmp) goto done; |
| 60 |
ALLEGRO_LOCKED_REGION *lock = al_lock_bitmap(bmp, |
| 61 |
ALLEGRO_PIXEL_FORMAT_ABGR_8888, ALLEGRO_LOCK_WRITEONLY); |
| 62 |
|
| 63 |
if (!premul) { |
| 64 |
for (int i = 0; i < h; i++) { |
| 65 |
unsigned char *src_ptr = (unsigned char *)pixels + w * 4 * i; |
| 66 |
unsigned char *dest_ptr = (unsigned char *)lock->data + |
| 67 |
lock->pitch * i; |
| 68 |
for (int x = 0; x < w; x++) { |
| 69 |
unsigned char r, g, b, a; |
| 70 |
r = *src_ptr++; |
| 71 |
g = *src_ptr++; |
| 72 |
b = *src_ptr++; |
| 73 |
a = *src_ptr++; |
| 74 |
// NOTE: avoid divide by zero by adding a fraction |
| 75 |
float alpha_mul = 255.0f / (a+0.001f); |
| 76 |
r *= alpha_mul; |
| 77 |
g *= alpha_mul; |
| 78 |
b *= alpha_mul; |
| 79 |
*dest_ptr++ = r; |
| 80 |
*dest_ptr++ = g; |
| 81 |
*dest_ptr++ = b; |
| 82 |
*dest_ptr++ = a; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
else { |
| 87 |
for (int i = 0; i < h; i++) { |
| 88 |
memcpy(lock->data + lock->pitch * i, pixels + w * 4 * i, w * 4); |
| 89 |
} |
| 90 |
} |
| 91 |
al_unlock_bitmap(bmp); |
| 92 |
done: |
| 93 |
al_free(pixels); |
| 94 |
|
| 95 |
[pool release]; |
| 96 |
|
| 97 |
return bmp; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
ALLEGRO_BITMAP *_al_iphone_load_image_f(ALLEGRO_FILE *f, int flags) |
| 102 |
{ |
| 103 |
ALLEGRO_BITMAP *bmp; |
| 104 |
ALLEGRO_ASSERT(f); |
| 105 |
|
| 106 |
int64_t size = al_fsize(f); |
| 107 |
if (size <= 0) { |
| 108 |
// TODO: Read from stream until we have the whole image |
| 109 |
ALLEGRO_ERROR("Unable to determine file size.\n"); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
/* Note: This *MUST* be the Apple malloc and not any wrapper, as the |
| 113 |
* buffer will be owned and freed by the NSData object not us. |
| 114 |
*/ |
| 115 |
void *buffer = al_malloc(size); |
| 116 |
al_fread(f, buffer, size); |
| 117 |
|
| 118 |
/* Really load the image now. */ |
| 119 |
bmp = really_load_image(buffer, size, flags); |
| 120 |
return bmp; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
ALLEGRO_BITMAP *_al_iphone_load_image(const char *filename, int flags) |
| 125 |
{ |
| 126 |
ALLEGRO_FILE *fp; |
| 127 |
ALLEGRO_BITMAP *bmp; |
| 128 |
|
| 129 |
ALLEGRO_ASSERT(filename); |
| 130 |
|
| 131 |
fp = al_fopen(filename, "rb"); |
| 132 |
if (!fp) { |
| 133 |
ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
|
| 137 |
bmp = _al_iphone_load_image_f(fp, flags); |
| 138 |
|
| 139 |
al_fclose(fp); |
| 140 |
|
| 141 |
return bmp; |
| 142 |
} |