File: simage_cgimage.c

package info (click to toggle)
simage 1.7.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,908 kB
  • sloc: ansic: 14,231; sh: 12,587; cpp: 930; perl: 854; makefile: 617; lisp: 25
file content (315 lines) | stat: -rw-r--r-- 7,557 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <simage_cgimage.h>

#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

enum {
  ERR_NO_ERROR,
  ERR_OPEN,
  ERR_READ,
  ERR_MEM,
  ERR_OPEN_WRITE,
  ERR_WRITE,
  ERR_NOT_IMPLEMENTED,
  ERR_INIT
};

static int cgimageerror = ERR_NO_ERROR;

static CGImageSourceRef
create_image_source(const char * file)
{
  CFStringRef cfname;
  CFURLRef image_url;
  CGImageSourceRef image_source;

  cfname = CFStringCreateWithCString(kCFAllocatorDefault,
				     file,
				     kCFStringEncodingUTF8);

  if (!cfname) {
    return NULL;
  }

  image_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
					    cfname,
					    kCFURLPOSIXPathStyle,
					    false);

  if (!image_url) {
    CFRelease(cfname);
    return NULL;
  }

  image_source = CGImageSourceCreateWithURL(image_url, NULL);
  CFRelease(image_url);
  CFRelease(cfname);

  if (image_source &&
      (CGImageSourceGetStatus(image_source) != kCGImageStatusComplete ||
       CGImageSourceGetCount(image_source) == 0)) {
    CFRelease(image_source);
    image_source = NULL;
  }
  return image_source;
}

int
simage_cgimage_error(char * cstr, int buflen)
{
  int errval = cgimageerror;
  switch (cgimageerror) {
  case ERR_OPEN:
    strncpy(cstr, "CGImage loader: Error loading file", buflen);
    break;
  }
  cgimageerror = ERR_NO_ERROR;
  return errval;
}

int
simage_cgimage_identify(const char * file, const unsigned char * header,
			int headerlen)
{
  CGImageSourceRef image_source = create_image_source(file);
  if (image_source) {
    CFRelease(image_source);
    return 1;
  }
  return 0;
}

unsigned char *
simage_cgimage_load(const char * file, int * width, int * height, int * numcomponents)
{
  CGColorSpaceRef color_space;
  CGImageSourceRef image_source;
  CGImageRef image;
  CGContextRef context;
  unsigned char * newpx;

  image_source = create_image_source(file);
  if (!image_source) {
    cgimageerror = ERR_OPEN;
    return NULL;
  }

  image = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
  assert(image);

  *width = CGImageGetWidth(image);
  *height = CGImageGetHeight(image);
  *numcomponents = CGImageGetBitsPerPixel(image)/CGImageGetBitsPerComponent(image);

  if (*numcomponents <= 2) {
    *numcomponents = 1;
    color_space = CGColorSpaceCreateDeviceGray();
  }
  else {
    *numcomponents = 4;
    color_space = CGColorSpaceCreateDeviceRGB();
  }
  assert(color_space);

  newpx = (unsigned char *)malloc(*width * *height * *numcomponents);

  context = CGBitmapContextCreate(newpx, *width, *height, 8, *width * *numcomponents,
				  color_space,
				  (*numcomponents == 1) ?
				  kCGImageAlphaNone :
				  kCGImageAlphaPremultipliedLast);
  assert(context);
  /* flip Y axis */
  CGContextScaleCTM(context, 1.0f, -1.0f);
  CGContextTranslateCTM(context, 0.0f, -*height);

  CGContextDrawImage(context, CGRectMake(0, 0, *width, *height), image);

  CFRelease(context);
  CFRelease(color_space);
  CFRelease(image);
  CFRelease(image_source);

  return newpx;
}

char *
simage_cgimage_get_savers(void)
{
  unsigned int idx;
  unsigned int formats_size = 0;
  char * format;
  char * formats = NULL;

  CFArrayRef destinationTypes;
  CFIndex fileext_len = 0;
  CFStringRef fileext;
  CFStringRef jpegStr;
  CFStringRef tiffStr;

  format = (char*)malloc(5);
  memset(format, 0, 5);

  jpegStr = CFSTR("jpeg");
  tiffStr = CFSTR("tiff");

  destinationTypes = CGImageDestinationCopyTypeIdentifiers();
  for (idx = 0; idx < CFArrayGetCount(destinationTypes); idx++) {
    fileext = UTTypeCopyPreferredTagWithClass(CFArrayGetValueAtIndex(destinationTypes, idx),
					      kUTTagClassFilenameExtension);

    fileext_len = CFStringGetLength(fileext);

    /* make JPEG and TIFF strings 3 letter extensions */
    /* FIXME: dirty. wash it! 20100224 tamer. */
    if (fileext_len == 4) {
      if (!CFStringCompare(fileext, jpegStr, 0)) {
	CFRelease(fileext);
	fileext = CFSTR("jpg");
	fileext_len = 3;
      } else if (!CFStringCompare(fileext, tiffStr, 0)) {
	CFRelease(fileext);
	fileext = CFSTR("tif");
	fileext_len = 3;
      }
    }

    formats_size += fileext_len + ((idx == 0) ? 1 : 2);
    formats = (char *)realloc(formats, formats_size);

    /* Assumption: file extension tag not bigger than 4 characters,
       therefore the hardcoded value of 5. 20100224 tamer. */
    CFStringGetCString(fileext, format, 5, kCFStringEncodingUTF8);

    if (idx == 0) {
      strncpy(formats, format, fileext_len+1);
    } else {
      strncat(formats, ",", 2);
      strncat(formats, format, fileext_len+1);
    }

    CFRelease(fileext);
  }

  CFRelease(destinationTypes);
  free(format);

  return formats;
}

int
simage_cgimage_save(const char *filename,
		    const unsigned char * bytes,
		    int width,
		    int height,
		    int numcomponents,
		    const char * ext)
{
  CFStringRef cfname;
  CFStringRef file_ext;
  CFStringRef type_name;
  CFURLRef image_url;
  CGImageRef image_source;
  CGImageDestinationRef image_dest;
  CGColorSpaceRef color_space;
  CGDataProviderRef provider;

  int bitsPerComponent, bitsPerPixel, bytesPerRow;
  int finalized;

  unsigned int pos = 0, imgbufsize = width*height*numcomponents;
  unsigned char * bytes_flipped = NULL;

  /* FIXME: Flipping manually as CGBitmapContexts leads to headaches
     by not supporting 24bits per pixel buffers. 20100225 tamer. */
  bytes_flipped = (unsigned char *)malloc(imgbufsize);
  /* flip buffer horizontally */
  for (pos = 0; pos < imgbufsize; pos+=width*numcomponents) {
    memcpy(bytes_flipped+pos,
	   bytes+imgbufsize-width*numcomponents-pos,
	   width*numcomponents);
  }

  provider = CGDataProviderCreateWithData(NULL, bytes_flipped,
					  imgbufsize,
					  NULL);

  bitsPerComponent = 8;
  bitsPerPixel = bitsPerComponent*numcomponents;
  bytesPerRow = numcomponents * width;
  color_space = CGColorSpaceCreateDeviceRGB();
  image_source = CGImageCreate(width, height, 8, 8*numcomponents,
			       numcomponents*width,
			       color_space,
			       kCGBitmapByteOrderDefault,
			       provider,
			       NULL, 0,
			       kCGRenderingIntentDefault);

  CGDataProviderRelease(provider);
  CGColorSpaceRelease(color_space);
  free(bytes_flipped);

  file_ext = CFStringCreateWithCString(kCFAllocatorDefault,
				       ext,
				       kCFStringEncodingUTF8);
  type_name = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,
						    file_ext,
						    kUTTypeImage);

  CFRelease(file_ext);

  if (!type_name) {
    cgimageerror = ERR_WRITE;
    CFRelease(image_source);
    return 0;
  }

  cfname = CFStringCreateWithCString(kCFAllocatorDefault,
				     filename,
				     kCFStringEncodingUTF8);

  if (!cfname) {
    cgimageerror = ERR_WRITE;
    CFRelease(type_name);
    CFRelease(image_source);
    return 0;
  }

  image_url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
					    cfname,
					    kCFURLPOSIXPathStyle,
					    false);

  CFRelease(cfname);

  if (!image_url) {
    cgimageerror = ERR_WRITE;
    CFRelease(type_name);
    CFRelease(image_source);
    return 0;
  }

  image_dest = CGImageDestinationCreateWithURL(image_url, type_name, 1, NULL);

  CFRelease(type_name);
  CFRelease(image_url);

  if (!image_dest) {
    cgimageerror = ERR_WRITE;
    CFRelease(image_source);
    return 0;
  }

  CGImageDestinationAddImage(image_dest, image_source, NULL);

  finalized = CGImageDestinationFinalize(image_dest);
  if (!finalized) {
    cgimageerror = ERR_WRITE;
  }

  CGImageRelease(image_source);
  CFRelease(image_dest);

  return finalized;
}