1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 // FIXME: this image reader is also present in Quarter. If you do any
34 // fixes/changes here you should look into updating the Quarter
35 // version as well.  pederb, 2009-02-04
36 
37 #include <Inventor/Qt/SoQtImageReader.h>
38 #include <Inventor/SbImage.h>
39 #include <qimage.h>
40 
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif // HAVE_CONFIG_H
44 
SoQtImageReader(void)45 SoQtImageReader::SoQtImageReader(void)
46 {
47 #if HAVE_SBIMAGE_ADDREADIMAGECB
48   SbImage::addReadImageCB(SoQtImageReader::readImageCB, this);
49 #endif // HAVE_SBIMAGE_ADDREADIMAGECB
50 }
51 
~SoQtImageReader(void)52 SoQtImageReader::~SoQtImageReader(void)
53 {
54 #if HAVE_SBIMAGE_ADDREADIMAGECB
55   SbImage::removeReadImageCB(SoQtImageReader::readImageCB, this);
56 #endif // HAVE_SBIMAGE_ADDREADIMAGECB
57 }
58 
59 SbBool
readImage(const SbString & filename,SbImage * sbimage) const60 SoQtImageReader::readImage(const SbString & filename, SbImage * sbimage) const
61 {
62   QImage image;
63   if (image.load(filename.getString())) {
64     int c;
65     int w = image.width();
66     int h = image.height();
67 
68     // Keep in 8-bits mode if that was what we read
69     if (image.depth() == 8 && image.isGrayscale()) {
70       c = 1;
71     }
72     else {
73       // FIXME: consider if we should detect allGrayscale() and alpha (c = 2)
74 #if QT_VERSION >= 0x040000
75       c = image.hasAlphaChannel() ? 4 : 3;
76       image = image.convertToFormat(image.hasAlphaChannel() ?
77                                     QImage::Format_ARGB32 : QImage::Format_RGB32);
78 #else
79       c = image.hasAlphaBuffer() ? 4 : 3;
80       image = image.convertDepth(32);
81 #endif
82     }
83 
84     SbVec2s size((short) w, (short) h);
85     sbimage->setValue(size, c, NULL);
86     unsigned char * buffer = sbimage->getValue(size, c);
87 
88     if (c == 1) {
89       for (int i = 0; i < h; i++) {
90         memcpy(buffer + i*w, image.scanLine(h-(i+1)), w);
91       }
92     }
93     else { // (c == 3 || c == 4)
94       QRgb * bits = (QRgb*) image.bits();
95       for (int y = 0; y < h; y++) {
96         unsigned char * line = &buffer[c*w*(h-(y+1))];
97         for (int x = 0; x < w; x++) {
98           *line++ = qRed(*bits);
99           *line++ = qGreen(*bits);
100           *line++ = qBlue(*bits);
101           if (c == 4) {
102             *line++ = qAlpha(*bits);
103           }
104           bits++;
105         }
106       }
107     }
108     return true;
109   }
110   return false;
111 }
112 
113 SbBool
readImageCB(const SbString & filename,SbImage * image,void * closure)114 SoQtImageReader::readImageCB(const SbString & filename, SbImage * image, void * closure)
115 {
116   return ((SoQtImageReader*)closure)->readImage(filename, image);
117 }
118