File: simage_qimage.cpp

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 (263 lines) | stat: -rw-r--r-- 6,111 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
#include <simage_qimage.h>

#include <qglobal.h>
#include <qimage.h>
#include <qstring.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#if QT_VERSION > 0x040000
#define QT4 1
#else // QT version >= 4.0.0
#define QT4 0
#endif // QT version < 4.0.0

#if QT4
#include <qimagereader.h>
#include <qimagewriter.h>
#endif // QT4

#define ERR_NO_ERROR          0
#define ERR_OPEN              1
#define ERR_MEM               2
#define ERR_QIMAGE_WRITE      5
#define ERR_UNSUPPORTED_WRITE 6

static int qimageerror = ERR_NO_ERROR;

int
simage_qimage_error(char * buffer, int buflen)
{
  switch (qimageerror) {
  case ERR_OPEN:
    strncpy(buffer, "QImage loader: Error opening file", buflen);
    break;
  case ERR_MEM:
    strncpy(buffer, "QImage loader: Out of memory error", buflen);
    break;
  case ERR_QIMAGE_WRITE:
    strncpy(buffer, "QImage saver: Internal QImage error", buflen);    
    break;
  case ERR_UNSUPPORTED_WRITE:
    strncpy(buffer, "QImage saver: Unsupported file format", buflen);    
    break;
  }
  return qimageerror;
}

int 
simage_qimage_identify(const char * ptr,
                       const unsigned char *header,
                       int headerlen)
{
#if QT4
  return ! QImageReader::imageFormat(QString(ptr)).isEmpty();
#else
  return QImage::imageFormat(QString(ptr)) != NULL;
#endif
}

unsigned char *
simage_qimage_load(const char * filename,
                   int * width_ret,
                   int * height_ret,
                   int * numComponents_ret)
{
  QImage image;
  if (image.load(filename)) {
    int w = image.width();
    int h = image.height();
    // Keep in 8-bits mode if that was what we read
    int c;
    if (image.depth() == 8 && image.isGrayscale()) 
      c = 1;
    else {
#if QT4
      c = image.hasAlphaChannel() ? 4 : 3;
      image = image.convertToFormat(image.hasAlphaChannel() ? 
                                    QImage::Format_ARGB32 : QImage::Format_RGB32);
#else
      image = image.convertDepth(32);
      c = image.hasAlphaBuffer() ? 4 : 3;
#endif
    }

    unsigned char *buffer = (unsigned char *)malloc(w*h*c);
    if (buffer == NULL) {
      qimageerror = ERR_MEM;
      return NULL;
    }
    
    if (c == 1) {
      for (int i = 0; i < h; i++) {
        memcpy(buffer + i*w, image.scanLine(h-(i+1)), w);
      }
    }
    else { // (c == 3 || c == 4)
      QRgb * bits = (QRgb*) image.bits();
      for (int y = 0; y < h; y++) {
        unsigned char * line = &buffer[c*w*(h-(y+1))];
        for (int x = 0; x < w; x++) {
          *line++ = qRed(*bits);
          *line++ = qGreen(*bits);
          *line++ = qBlue(*bits);
          if (c == 4) {
            *line++ = qAlpha(*bits);
          }
          bits++;
        }
      }
    }
    
    *width_ret = w;
    *height_ret = h;
    *numComponents_ret = c;
    return buffer;
  }
  qimageerror = ERR_OPEN;
  return NULL;
}

#define MAX_EXT_LEN 255

static int
qimage_set_save_format(const char * ext, char * buf)
{
  strncpy(buf, ext, MAX_EXT_LEN);
  buf[MAX_EXT_LEN] = 0;
  
  int i = 0;
  // convert to upper case
  while (buf[i] != 0) {
    buf[i] = toupper(buf[i]);
    i++;
  }
  
  // Qt specifies the jpg extension as JPEG
  if (strcmp(buf, "JPG") == 0) strcpy(buf, "JPEG");
  
#if QT4

  QList <QByteArray> list = QImageWriter::supportedImageFormats();
  const int n = list.size();
  for (int i = 0; i < n; i++) {
    if (strcmp(buf, list[i].constData()) == 0) return 1;
  }
  buf[0] = 0;
  return 0;

#else // QT4

  QStrList olist = QImage::outputFormats();
  const char * qtext = olist.getFirst();
  while (qtext) {
    if (strcmp(buf, qtext) == 0) return 1;
    qtext = olist.next(); 
  }
  // clear save format
  buf[0] = 0;
  return 0;

#endif // !QT4
}

char * 
simage_qimage_get_savers(void)
{
#if QT4

  QByteArray str;
  QList <QByteArray> list = QImageWriter::supportedImageFormats();
  const int n = list.size();
  int first = 1;
  for (int i = 0; i < n; i++) {
    if (!first) str += ",";
    first = 0;
    str += list[i];
  }
  if (first) return NULL;
  const char * asc = str.constData(); 
  int len = strlen(asc);
  char * dst = (char*) malloc(len+1);
  strcpy(dst, asc);
  return dst;

#else // QT4

  QString str;
  int first = 1;
  QStrList olist = QImage::outputFormats();
  const char * qtext = olist.getFirst();
  while (qtext) {
    if (!first) str += ",";
    first = 0;
    str += qtext;
    qtext = olist.next();
  }
  if (first) return NULL; // no savers available
  const char * asc = str.ascii();
  int len = strlen(asc);
  char * dst = (char*) malloc(len+1);
  strcpy(dst, asc);
  return dst;

#endif // !QT4  
}


int 
simage_qimage_save(const char * filename,
                   const unsigned char * bytes,
                   int width,
                   int height,
                   int numcomponents,
                   const char * filetypeext)
{
  char ext[MAX_EXT_LEN+1];  
  qimage_set_save_format(filetypeext, ext);  
  if (ext[0] == 0) {
    qimageerror = ERR_UNSUPPORTED_WRITE;
    return 0;
  }
  
#if QT4
  QImage image(width, height, numcomponents == 2 || numcomponents == 4 ? 
               QImage::Format_ARGB32 : QImage::Format_RGB32);
#else // QT4
  QImage image(width, height, 32);
  if (numcomponents == 2 || numcomponents == 4) image.setAlphaBuffer(TRUE);
  else image.setAlphaBuffer(FALSE);
#endif // !QT4
  QRgb * bits = (QRgb*) image.bits();
  
  for (int y = 0; y < height; y++) {
    const unsigned char * line = 
      &bytes[width*numcomponents*(height-(y+1))];
    for (int x = 0; x < width; x++) {
      switch (numcomponents) {
      default:
      case 1:
        *bits++ = qRgb(line[0], line[0], line[0]);
        break;
      case 2:
        *bits++ = qRgba(line[0], line[0], line[0], line[1]);
        break;
      case 3:
        *bits++ = qRgb(line[0], line[1], line[2]);
        break;
      case 4:
        *bits++ = qRgba(line[0], line[1], line[2], line[3]);
        break;
      }
      line += numcomponents;
    }
  }
  bool ret = image.save(QString(filename), ext);
  if (!ret) {
    qimageerror = ERR_QIMAGE_WRITE;
    return 0;
  }
  return 1;
}