27 #if QT_VERSION > 0x040000
29 #else // QT version >= 4.0.0
31 #endif // QT version < 4.0.0
34 #include <qimagereader.h>
35 #include <qimagewriter.h>
38 #define ERR_NO_ERROR 0
41 #define ERR_QIMAGE_WRITE 5
42 #define ERR_UNSUPPORTED_WRITE 6
51 strncpy(buffer,
"QImage loader: Error opening file", buflen);
54 strncpy(buffer,
"QImage loader: Out of memory error", buflen);
57 strncpy(buffer,
"QImage saver: Internal QImage error", buflen);
60 strncpy(buffer,
"QImage saver: Unsupported file format", buflen);
68 const unsigned char *header,
72 return ! QImageReader::imageFormat(QString(ptr)).isEmpty();
74 return QImage::imageFormat(QString(ptr)) != NULL;
82 int * numComponents_ret)
85 if (image.load(filename)) {
86 int w = image.width();
87 int h = image.height();
90 if (image.depth() == 8 && image.isGrayscale())
94 c = image.hasAlphaChannel() ? 4 : 3;
95 image = image.convertToFormat(image.hasAlphaChannel() ?
96 QImage::Format_ARGB32 : QImage::Format_RGB32);
98 image = image.convertDepth(32);
99 c = image.hasAlphaBuffer() ? 4 : 3;
103 unsigned char *buffer = (
unsigned char *)malloc(w*h*c);
104 if (buffer == NULL) {
110 for (
int i = 0; i < h; i++) {
111 memcpy(buffer + i*w, image.scanLine(h-(i+1)), w);
115 QRgb * bits = (QRgb*) image.bits();
116 for (
int y = 0; y < h; y++) {
117 unsigned char * line = &buffer[c*w*(h-(y+1))];
118 for (
int x = 0; x < w; x++) {
119 *line++ = qRed(*bits);
120 *line++ = qGreen(*bits);
121 *line++ = qBlue(*bits);
123 *line++ = qAlpha(*bits);
132 *numComponents_ret = c;
139 #define MAX_EXT_LEN 255
149 while (buf[i] != 0) {
150 buf[i] = toupper(buf[i]);
155 if (strcmp(buf,
"JPG") == 0) strcpy(buf,
"JPEG");
159 QList <QByteArray> list = QImageWriter::supportedImageFormats();
160 const int n = list.size();
161 for (
int i = 0; i < n; i++) {
162 if (strcmp(buf, list[i].constData()) == 0)
return 1;
169 QStrList olist = QImage::outputFormats();
170 const char * qtext = olist.getFirst();
172 if (strcmp(buf, qtext) == 0)
return 1;
173 qtext = olist.next();
188 QList <QByteArray> list = QImageWriter::supportedImageFormats();
189 const int n = list.size();
191 for (
int i = 0; i < n; i++) {
192 if (!first) str +=
",";
196 if (first)
return NULL;
197 const char * asc = str.constData();
198 int len = (int)strlen(asc);
199 char * dst = (
char*) malloc(len+1);
207 QStrList olist = QImage::outputFormats();
208 const char * qtext = olist.getFirst();
210 if (!first) str +=
",";
213 qtext = olist.next();
215 if (first)
return NULL;
216 const char * asc = str.ascii();
217 int len = strlen(asc);
218 char * dst = (
char*) malloc(len+1);
228 const unsigned char * bytes,
232 const char * filetypeext)
242 QImage image(width, height, numcomponents == 2 || numcomponents == 4 ?
243 QImage::Format_ARGB32 : QImage::Format_RGB32);
245 QImage image(width, height, 32);
246 if (numcomponents == 2 || numcomponents == 4) image.setAlphaBuffer(TRUE);
247 else image.setAlphaBuffer(FALSE);
249 QRgb * bits = (QRgb*) image.bits();
251 for (
int y = 0; y < height; y++) {
252 const unsigned char * line =
253 &bytes[width*numcomponents*(height-(y+1))];
254 for (
int x = 0; x < width; x++) {
255 switch (numcomponents) {
258 *bits++ = qRgb(line[0], line[0], line[0]);
261 *bits++ = qRgba(line[0], line[0], line[0], line[1]);
264 *bits++ = qRgb(line[0], line[1], line[2]);
267 *bits++ = qRgba(line[0], line[1], line[2], line[3]);
270 line += numcomponents;
273 bool ret = image.save(QString(filename), ext);
unsigned char * simage_qimage_load(const char *filename, int *width_ret, int *height_ret, int *numComponents_ret)
char * simage_qimage_get_savers(void)
int simage_qimage_error(char *buffer, int buflen)
int simage_qimage_identify(const char *ptr, const unsigned char *header, int headerlen)
int simage_qimage_save(const char *filename, const unsigned char *bytes, int width, int height, int numcomponents, const char *filetypeext)
static int qimage_set_save_format(const char *ext, char *buf)
#define ERR_UNSUPPORTED_WRITE