File: imageIO.mm

package info (click to toggle)
gem 1%3A0.94-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,704 kB
  • sloc: cpp: 174,301; ansic: 42,132; makefile: 3,867; sh: 1,096; objc: 389
file content (298 lines) | stat: -rw-r--r-- 9,251 bytes parent folder | download | duplicates (4)
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
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.at
//
// Implementation file
//
//    Copyright (c) 1997-1999 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2011 IOhannes m zmölnig. forum::für::umläute. IEM. zmoelnig@iem.at
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <string.h>
#include "imageIO.h"
#include "plugins/PluginFactory.h"

#include "Gem/RTE.h"
#import <Foundation/Foundation.h>
#import <ImageIO/ImageIO.h>

using namespace gem::plugins;

REGISTER_IMAGELOADERFACTORY("imageIO", imageIO);
REGISTER_IMAGESAVERFACTORY("imageIO", imageIO);

CFStringRef mime2uti(const std::string&mimetype) {
  if(mimetype == "image/jpeg")
    return kUTTypeJPEG;
  else if ((mimetype == "image/jp2") || (mimetype == "image/jpx") || (mimetype == "image/jpm"))
    return kUTTypeJPEG2000;
  else if ((mimetype == "image/tiff") || (mimetype == "image/x-tiff"))
    return kUTTypeTIFF;
  else if ((mimetype == "image/pict") || (mimetype == "image/x-pict"))
    return kUTTypePICT;
  else if ((mimetype == "image/gif"))
    return kUTTypeGIF;
  else if ((mimetype == "image/png"))
    return kUTTypePNG;
  else if ((mimetype == "image/x-quicktime"))
    return kUTTypeQuickTimeImage;
  else if ((mimetype == "image/icns"))
    return kUTTypeAppleICNS;
  else if ((mimetype == "image/bmp") || (mimetype == "image/x-windows-bmp"))
    return kUTTypeBMP;
  else if ((mimetype == "image/x-icon"))
    return kUTTypeICO;

  return 0;
}


/////////////////////////////////////////////////////////
//
// imageIO
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////

imageIO :: imageIO(void)
{
}
imageIO :: ~imageIO(void)
{
}

/////////////////////////////////////////////////////////
// really open the file ! (OS dependent)
//
/////////////////////////////////////////////////////////
bool imageIO :: load(std::string filename, imageStruct&result,
                     gem::Properties&props)
{
  bool success = false;
  NSString *path = [NSString stringWithUTF8String:filename.c_str()];
  // Get the URL for the pathname passed to the function.
  NSURL *url = [NSURL fileURLWithPath:path];
  CGImageRef myImage = NULL;
  CGImageSourceRef myImageSource;
  CFDictionaryRef myOptions = NULL;
  CFStringRef myKeys[2];
  CFTypeRef myValues[2];

  // Set up options if you want them. The options here are for
  // caching the image in a decoded form and for using floating-point
  // values if the image format supports them.
  myKeys[0] = kCGImageSourceShouldCache;
  myValues[0] = (CFTypeRef)kCFBooleanTrue;
  myKeys[1] = kCGImageSourceShouldAllowFloat;
  myValues[1] = (CFTypeRef)kCFBooleanTrue;

  // Create the dictionary
  myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
                                 (const void **) myValues, 2,
                                 &kCFTypeDictionaryKeyCallBacks,
                                 & kCFTypeDictionaryValueCallBacks);
  // Create an image source from the URL.
  myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, myOptions);
  CFRelease(myOptions);
  // Make sure the image source exists before continuing
  if (!myImageSource){
    fprintf(stderr, "Image source '%s' is NULL.", filename.c_str());
    return false;
  }

  // Create an image from the first item in the image source.
  myImage = CGImageSourceCreateImageAtIndex(myImageSource,
                                            0,
                                            NULL);

  CFRelease(myImageSource);
  // Make sure the image exists before continuing
  if (!myImage){
    fprintf(stderr, "Image not created from image source.");
    return false;
  }

  size_t w = CGImageGetWidth(myImage);
  size_t h = CGImageGetHeight(myImage);
  result.xsize = w;
  result.ysize = h;
  result.setCsizeByFormat(GEM_RGBA);
  result.reallocate();
  CGRect rect = {{0,0},{w,h}};

  CGColorSpaceRef colorSpace;
  CGContextRef context;
  void*data;
  colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
  if(!colorSpace)
    goto done;
#warning get rid of premultiplied alpha channel
  context = CGBitmapContextCreate(result.data,
                                  result.xsize, result.ysize, 8, result.xsize * result.csize,
                                  colorSpace, kCGImageAlphaPremultipliedFirst);
  if(!context)
    goto done;

  CGContextDrawImage(context, rect, myImage);
  if(CGBitmapContextGetData (context) == result.data)
    success=true;

done:
  if(context)
    CGContextRelease(context);
  if(colorSpace)
    CGColorSpaceRelease(colorSpace);
  CFRelease(myImage);
  return success;
}
bool imageIO::save(const imageStruct&img,
                   const std::string&filename, const std::string&mimetype,
                   const gem::Properties&props)
{
  NSString *path = [NSString stringWithUTF8String:filename.c_str()];
  NSURL *url = [NSURL fileURLWithPath:path];
  bool success = false;

  float compression = 1.0; // Lossless compression if available.
  int orientation = 4; // Origin is at bottom, left.

  CFStringRef uti = mime2uti(mimetype);

  CFStringRef myKeys[3];
  CFTypeRef   myValues[3];
  CFDictionaryRef myOptions = NULL;
  myKeys[0] = kCGImagePropertyOrientation;
  myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
  myKeys[1] = kCGImagePropertyHasAlpha;
  myValues[1] = kCFBooleanTrue;
  myKeys[2] = kCGImageDestinationLossyCompressionQuality;
  myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
  myOptions = CFDictionaryCreate(NULL, (const void **)myKeys, (const void **)myValues, sizeof(myKeys)/sizeof(*myKeys),
                                 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

  CGImageRef myImage = NULL;
  CGImageDestinationRef myImageDest = NULL;
  CGDataProviderRef data = CGDataProviderCreateWithData(NULL,
                                                        img.data,
                                                        img.xsize * img.ysize * img.csize,
                                                        NULL);
  if(!data)
    goto done;
  myImage = CGImageCreate(img.xsize, img.ysize,
                          8, 8 * img.csize,
                          img.xsize * img.csize,
                          CGColorSpaceCreateDeviceRGB(), CGBitmapInfo(kCGBitmapByteOrderDefault | kCGImageAlphaFirst),
                          data,
                          NULL, false, kCGRenderingIntentDefault);
  if(!myImage) {
    fprintf(stderr, "unable to create CGImage from data\n");
    goto done;
  }

  myImageDest = CGImageDestinationCreateWithURL((CFURLRef)url, uti, 1, nil);
  CGImageDestinationAddImage(myImageDest, myImage, myOptions);
  if(!myImageDest) {
    fprintf(stderr, "unable to create destination image for %s\n", filename.c_str());
    goto done;
  }

  CGImageDestinationFinalize(myImageDest);
  success=true;

done:
  if(myImageDest)
    CFRelease(myImageDest);
  if(myImage)
    CFRelease(myImage);
  if(data)
    CFRelease(data);
  if(myOptions)
    CFRelease(myOptions);
  return success;
}


float imageIO::estimateSave(const imageStruct&img,
                            const std::string&filename, const std::string&mimetype,
                            const gem::Properties&props)
{
  float result=0;
  CFStringRef uti = mime2uti(mimetype);

  if(uti)
    result += 100;

#if 0
  if(gem::Properties::UNSET != props.type("xresolution")) {
    result+=1.;
  }
  if(gem::Properties::UNSET != props.type("yresolution")) {
    result+=1.;
  }
  if(gem::Properties::UNSET != props.type("resolutionunit")) {
    result+=1.;
  }
  if(gem::Properties::UNSET != props.type("software")) {
    result+=1.;
  }
  if(gem::Properties::UNSET != props.type("artist")) {
    result+=1.;
  }
  if(gem::Properties::UNSET != props.type("hostcomputer")) {
    result+=1.;
  }
#endif
  return result;
}


void imageIO::getWriteCapabilities(std::vector<std::string>&mimetypes,
                                   gem::Properties&props)
{
  mimetypes.clear();
  props.clear();

  mimetypes.push_back("image/jpeg");
  mimetypes.push_back("image/jp2");
  mimetypes.push_back("image/jpx");
  mimetypes.push_back("image/jpm");
  mimetypes.push_back("image/tiff");
  mimetypes.push_back("image/x-tiff");
  mimetypes.push_back("image/pict");
  mimetypes.push_back("image/x-pict");
  mimetypes.push_back("image/gif");
  mimetypes.push_back("image/png");
  mimetypes.push_back("image/bmp");
  mimetypes.push_back("image/x-windows-bmp");
  mimetypes.push_back("image/x-quicktime");
  mimetypes.push_back("image/x-icon");
  mimetypes.push_back("image/icns");

#if 0
  gem::any value;


  value=72.f;
  props.set("xresolution", value);
  props.set("yresolution", value);

  value=std::string("inch");
  props.set("resolutionunit", value);
  value=std::string("PD/GEM");
  props.set("software", value);
  value=std::string("");
  props.set("artist", value);
  props.set("hostcomputer", value);
#endif
}