File: MagickCore.cpp

package info (click to toggle)
gem 1%3A0.94-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 25,636 kB
  • sloc: cpp: 174,297; ansic: 42,132; makefile: 3,852; sh: 1,096; objc: 389
file content (230 lines) | stat: -rw-r--r-- 5,486 bytes parent folder | download | duplicates (3)
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
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.at
//
// Implementation file
//
//    Copyright (c) 1997-1999 Mark Danks.
//    Copyright (c) Gnther Geiger.
//    Copyright (c) 2001-2011 IOhannes m zmlnig. forum::fr::umlute. 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

/* this implements ImageMagick loading/saving using MagickCore */

#include "imageMAGICK.h"
#include "Gem/RTE.h"
#include "Gem/GemGL.h"

#ifdef _MSC_VER
# if !defined(_W64)
#  if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
#   define _W64 __w64
#  else
#   define _W64
#  endif
# endif
# ifdef _WIN64
typedef __int64         ssize_t;
# else
typedef _w64 long        ssize_t;
# endif
#endif

#ifdef HAVE_MAGICK7
# include <MagickCore/MagickCore.h>
#else
# include <magick/MagickCore.h>
#endif

using namespace gem::plugins;

namespace
{
static bool showException(ExceptionInfo*exception,
                          const std::string&prefix=std::string())
{
  if(!exception) {
    return true;
  }
  if (exception->severity == UndefinedException) {
    return false;
  }

  bool iswarning=exception->severity < ErrorException;

  std::string message=prefix;
  message+="[";
  message+= SetClientName(0);
  message+="]";

  if(!iswarning) {
    message+"!";
  }

  message+=": ";

  if ( exception->reason != 0 ) {
    message += std::string(exception->reason);
  }
  if ( exception->description != 0 ) {
    message += " (" + std::string(exception->description) + ")";
  }

  if(iswarning) {
    verbose(0, "[GEM:imageMAGICK] %s", message.c_str());
  } else {
    verbose(0, "[GEM:imageMAGICK] %s", message.c_str());
  }
  return (!iswarning);
}
}


/////////////////////////////////////////////////////////
// really open the file ! (OS dependent)
//
/////////////////////////////////////////////////////////
bool imageMAGICK :: load(std::string filename, imageStruct&result,
                         gem::Properties&props)
{
  bool success=false;
  ExceptionInfo*exception=AcquireExceptionInfo();
  ImageInfo*image_info=CloneImageInfo((ImageInfo *) NULL);
  CopyMagickString(image_info->filename,filename.c_str(), MaxTextExtent);

  Image*image=ReadImage(image_info,exception);
  if(showException(exception, "reading problem")) {
    goto cleanup;
  }
  if (image == (Image *) NULL) {
    goto cleanup;
  }

  result.xsize=static_cast<GLint>(image->columns);
  result.ysize=static_cast<GLint>(image->rows);
  result.setCsizeByFormat(GL_RGBA_GEM);
  result.reallocate();

  result.upsidedown=true;

  ExportImagePixels(image, 0, 0, result.xsize, result.ysize,
#ifdef __APPLE__
                    "ARGB",
#else
                    "RGBA",
#endif
                    CharPixel,
                    reinterpret_cast<void*>(result.data),
                    exception);
  if(showException(exception, "decoding problem")) {
    goto cleanup;
  }

  success=true;

cleanup:
  if(image) {
    DestroyImage(image);
  }
  image=NULL;
  if(image_info) {
    image_info=DestroyImageInfo(image_info);
  }
  if(exception) {
    exception=DestroyExceptionInfo(exception);
  }
  return success;
}
bool imageMAGICK::save(const imageStruct&image, const std::string&filename,
                       const std::string&mimetype, const gem::Properties&props)
{
  MagickBooleanType status = MagickFalse;
  imageStruct*img=const_cast<imageStruct*>(&image);
  imageStruct*pImage=img;
  ImageInfo*image_info=CloneImageInfo((ImageInfo *) NULL);
  Image*finalImage=NULL;

  std::string cs;
  switch(img->format) {
  case GL_LUMINANCE:
    cs="K";
    break;
  case GL_RGBA:
#ifdef __APPLE__
    cs="ABGR";
#else
    cs="RGBA";
#endif
    break;
  default:
    pImage=new imageStruct();
    pImage->convertFrom(img, GL_RGB);
  case GL_RGB:
    cs="RGB";
    break;
  case GL_BGRA_EXT:
#ifdef __APPLE__
    cs="ARGB";
#else
    cs="BGRA";
#endif
    break;
  }

  ExceptionInfo*ex = 0;
  ExceptionInfo*exception=AcquireExceptionInfo();
  Image *mimage = ConstituteImage(pImage->xsize, pImage->ysize,
                                  cs.c_str(), CharPixel,
                                  pImage->data, exception);
  if(showException(exception, "conversion problem")) {
    goto cleanup;
  }

  finalImage=(pImage->upsidedown)?mimage:FlipImage( mimage, exception );
  if(showException(exception, "flipping problem")) {
    goto cleanup;
  }

  finalImage->depth=8;
  image_info->depth = 8;

  double quality;
  if(props.get("quality", quality)) {
    finalImage->quality=quality;
    image_info->quality = quality;
  }

  ex = exception;
  CopyMagickString(image_info->filename, filename.c_str(), MaxTextExtent);
  CopyMagickString(finalImage->filename, filename.c_str(), MaxTextExtent);
  SetImageInfo(image_info, 0, ex);

#ifdef HAVE_MAGICK7
  status = WriteImage(image_info, finalImage, ex);
#else
  status = WriteImage(image_info, finalImage);
  ex = &finalImage->exception;
#endif
  if(showException(ex, "writing problem")) {
    goto cleanup;
  }

cleanup:
  if(finalImage!=mimage) {
    finalImage=DestroyImage(finalImage);
  }

  mimage=DestroyImage(mimage);
  exception=DestroyExceptionInfo(exception);
  image_info=DestroyImageInfo(image_info);

  return (status == MagickTrue);
}