| 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 |
#include <QImage> |
| 34 |
#include <Inventor/SbImage.h> |
| 35 |
#include <Inventor/errors/SoDebugError.h> |
| 36 |
|
| 37 |
#include "QtCoinCompatibility.h" |
| 38 |
|
| 39 |
|
| 40 |
using namespace SIM::Coin3D::Quarter; |
| 41 |
|
| 42 |
void |
| 43 |
QtCoinCompatibility::QImageToSbImage(const QImage & image, SbImage & sbimage) |
| 44 |
{ |
| 45 |
int w = image.width(); |
| 46 |
int h = image.height(); |
| 47 |
int c; |
| 48 |
|
| 49 |
// Keep in 8-bits mode if that was what we read |
| 50 |
if (image.depth() == 8 && image.isGrayscale()) { |
| 51 |
c = 1; |
| 52 |
} |
| 53 |
else { |
| 54 |
// FIXME: consider if we should detect allGrayscale() and alpha (c = 2) |
| 55 |
c = image.hasAlphaChannel() ? 4 : 3; |
| 56 |
} |
| 57 |
|
| 58 |
SbVec2s size((short) w, (short) h); |
| 59 |
sbimage.setValue(size, c, nullptr); |
| 60 |
unsigned char * buffer = sbimage.getValue(size, c); |
| 61 |
|
| 62 |
if (c == 1) { |
| 63 |
for (int i = 0; i < h; i++) { |
| 64 |
memcpy(buffer + i*w, image.scanLine(h-(i+1)), w); |
| 65 |
} |
| 66 |
} |
| 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); |
| 75 |
if (c == 4) { |
| 76 |
*line++ = qAlpha(*bits); |
| 77 |
} |
| 78 |
bits++; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void |
| 85 |
QtCoinCompatibility::SbImageToQImage(const SbImage & sbimage, QImage & img) |
| 86 |
{ |
| 87 |
SbVec2s ivsize; |
| 88 |
int nc; |
| 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" |
| 95 |
); |
| 96 |
} |
| 97 |
QImage::Format format=QImage::Format_Invalid; |
| 98 |
if (nc==3||nc==4) { |
| 99 |
format=QImage::Format_RGB32; |
| 100 |
} |
| 101 |
else if (nc==1) { |
| 102 |
QVector<QRgb> clut; |
| 103 |
for (int i=0;i<256;++i) { |
| 104 |
clut.append(qRgb(i,i,i)); |
| 105 |
} |
| 106 |
format=QImage::Format_Indexed8; |
| 107 |
} |
| 108 |
img = QImage(size,format); |
| 109 |
assert(img.size()==size); |
| 110 |
if (nc==1) { |
| 111 |
QVector<QRgb> clut; |
| 112 |
for (int i=0;i<256;++i) { |
| 113 |
clut.append(qRgb(i,i,i)); |
| 114 |
} |
| 115 |
img.setColorTable(clut); |
| 116 |
} |
| 117 |
|
| 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) { |
| 121 |
switch (nc) { |
| 122 |
default: |
| 123 |
case 1: |
| 124 |
{ |
| 125 |
img.setPixel(x,size.height()-(y+1),*src++); |
| 126 |
} |
| 127 |
break; |
| 128 |
case 2: |
| 129 |
{ |
| 130 |
unsigned char red=*src++; |
| 131 |
unsigned char alpha=*src++; |
| 132 |
*bits=qRgba(red,red,red,alpha); |
| 133 |
} |
| 134 |
break; |
| 135 |
case 3: |
| 136 |
{ |
| 137 |
unsigned char red=*src++; |
| 138 |
unsigned char green=*src++; |
| 139 |
unsigned char blue=*src++; |
| 140 |
*bits=qRgb(red,green,blue); |
| 141 |
} |
| 142 |
break; |
| 143 |
case 4: |
| 144 |
{ |
| 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); |
| 150 |
} |
| 151 |
break; |
| 152 |
} |
| 153 |
++bits; |
| 154 |
} |
| 155 |
} |
| 156 |
} |