1
/**************************************************************************\
2
* Copyright (c) Kongsberg Oil & Gas Technologies AS
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
9
* Redistributions of source code must retain the above copyright notice,
10
* this list of conditions and the following disclaimer.
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.
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.
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
\**************************************************************************/
34
#include <Inventor/SbImage.h>
35
#include <Inventor/errors/SoDebugError.h>
37
#include "QtCoinCompatibility.h"
40
using namespace SIM::Coin3D::Quarter;
43
QtCoinCompatibility::QImageToSbImage(const QImage & image, SbImage & sbimage)
45
int w = image.width();
46
int h = image.height();
49
// Keep in 8-bits mode if that was what we read
50
if (image.depth() == 8 && image.isGrayscale()) {
54
// FIXME: consider if we should detect allGrayscale() and alpha (c = 2)
55
c = image.hasAlphaChannel() ? 4 : 3;
58
SbVec2s size((short) w, (short) h);
59
sbimage.setValue(size, c, nullptr);
60
unsigned char * buffer = sbimage.getValue(size, c);
63
for (int i = 0; i < h; i++) {
64
memcpy(buffer + i*w, image.scanLine(h-(i+1)), w);
67
else { // (c == 3 || c == 4)
68
QRgb * bits = (QRgb*) image.bits();
69
for (int y = 0; y < h; y++) {
70
unsigned char * line = &buffer[c*w*(h-(y+1))];
71
for (int x = 0; x < w; x++) {
72
*line++ = qRed(*bits);
73
*line++ = qGreen(*bits);
74
*line++ = qBlue(*bits);
76
*line++ = qAlpha(*bits);
85
QtCoinCompatibility::SbImageToQImage(const SbImage & sbimage, QImage & img)
89
const unsigned char* src = sbimage.getValue (ivsize, nc);
90
QSize size(ivsize[0],ivsize[1]);
91
assert(src && "Empty image");
92
if (nc!=3&&nc!=1&&nc!=4) {
93
SoDebugError::postWarning("QtCoinCompatibility::SbImageToQImage",
94
"Implementation not tested for 3 colors or more"
97
QImage::Format format=QImage::Format_Invalid;
99
format=QImage::Format_RGB32;
103
for (int i=0;i<256;++i) {
104
clut.append(qRgb(i,i,i));
106
format=QImage::Format_Indexed8;
108
img = QImage(size,format);
109
assert(img.size()==size);
112
for (int i=0;i<256;++i) {
113
clut.append(qRgb(i,i,i));
115
img.setColorTable(clut);
118
for (int y = 0; y < size.height(); ++y) {
119
QRgb * bits = reinterpret_cast<QRgb *>(img.scanLine(size.height() - (y+1)));
120
for (int x = 0; x < size.width(); ++x) {
125
img.setPixel(x,size.height()-(y+1),*src++);
130
unsigned char red=*src++;
131
unsigned char alpha=*src++;
132
*bits=qRgba(red,red,red,alpha);
137
unsigned char red=*src++;
138
unsigned char green=*src++;
139
unsigned char blue=*src++;
140
*bits=qRgb(red,green,blue);
145
unsigned char red=*src++;
146
unsigned char green=*src++;
147
unsigned char blue=*src++;
148
unsigned char alpha=*src++;
149
*bits=qRgba(red,green,blue,alpha);