| 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 | * |
| 5 | * This library is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU Lesser General Public |
| 7 | * License as published by the Free Software Foundation; either |
| 8 | * version 2.1 of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * Further, this software is distributed without any warranty that it is |
| 16 | * free of the rightful claim of any third person regarding infringement |
| 17 | * or the like. Any license provided herein, whether implied or |
| 18 | * otherwise, applies only to this software file. Patent licenses, if |
| 19 | * any, provided herein do not apply to combinations of this program with |
| 20 | * other software, or any other product whatsoever. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public |
| 23 | * License along with this library; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 | * Mountain View, CA 94043, or: |
| 28 | * |
| 29 | * http://www.sgi.com |
| 30 | * |
| 31 | * For further information regarding this notice, see: |
| 32 | * |
| 33 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 | * |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Copyright (C) 1990,91 Silicon Graphics, Inc. |
| 39 | * |
| 40 | _______________________________________________________________________ |
| 41 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 42 | | |
| 43 | | $Revision: 1.1.1.1 $ |
| 44 | | |
| 45 | | Classes: |
| 46 | | SoSFImage |
| 47 | | |
| 48 | | Author(s) : Gavin Bell |
| 49 | | |
| 50 | ______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 51 | _______________________________________________________________________ |
| 52 | */ |
| 53 | |
| 54 | #include <Inventor/fields/SoSFImage.h> |
| 55 | |
| 56 | ////////////////////////////////////////////////////////////////////////////// |
| 57 | // |
| 58 | // SoSFImage class |
| 59 | // |
| 60 | ////////////////////////////////////////////////////////////////////////////// |
| 61 | |
| 62 | // Use most of the standard stuff: |
| 63 | SO__FIELD_ID_SOURCE(SoSFImage); |
| 64 | SO__FIELD_EQ_SAME_SOURCE(SoSFImage); |
| 65 | |
| 66 | //////////////////////////////////////////////////////////////////////// |
| 67 | // |
| 68 | // Description: |
| 69 | // Constructor |
| 70 | // |
| 71 | // Use: public |
| 72 | |
| 73 | SoSFImage::SoSFImage() |
| 74 | // |
| 75 | //////////////////////////////////////////////////////////////////////// |
| 76 | { |
| 77 | size[0] = size[1] = 0; |
| 78 | numComponents = 0; |
| 79 | bytes = NULL; |
| 80 | } |
| 81 | |
| 82 | //////////////////////////////////////////////////////////////////////// |
| 83 | // |
| 84 | // Description: |
| 85 | // Destructor. |
| 86 | // |
| 87 | // Use: public |
| 88 | |
| 89 | SoSFImage::~SoSFImage() |
| 90 | // |
| 91 | //////////////////////////////////////////////////////////////////////// |
| 92 | { |
| 93 | if (bytes != NULL) delete[] bytes; |
| 94 | } |
| 95 | |
| 96 | //////////////////////////////////////////////////////////////////////// |
| 97 | // |
| 98 | // Description: |
| 99 | // Sets value, given image dimensions and bytes... |
| 100 | // |
| 101 | // Use: public |
| 102 | |
| 103 | void |
| 104 | SoSFImage::setValue(const SbVec2s &s, int nc, const unsigned char *b) |
| 105 | // |
| 106 | //////////////////////////////////////////////////////////////////////// |
| 107 | { |
| 108 | if (bytes != NULL) { |
| 109 | delete[] bytes; |
| 110 | bytes = NULL; |
| 111 | } |
| 112 | |
| 113 | size = s; |
| 114 | numComponents = nc; |
| 115 | |
| 116 | int numBytes = size[0]*size[1]*numComponents; |
| 117 | |
| 118 | if (numBytes != 0) { |
| 119 | bytes = new unsigned char[numBytes]; |
| 120 | memcpy(bytes, b, numBytes); |
| 121 | } |
| 122 | else |
| 123 | bytes = NULL; |
| 124 | |
| 125 | valueChanged(); |
| 126 | } |
| 127 | |
| 128 | //////////////////////////////////////////////////////////////////////// |
| 129 | // |
| 130 | // Description: |
| 131 | // Gets value, which is the image's dimensions and bytes. |
| 132 | // |
| 133 | // Use: public |
| 134 | |
| 135 | const unsigned char * |
| 136 | SoSFImage::getValue(SbVec2s &s, int &nc) const |
| 137 | // |
| 138 | //////////////////////////////////////////////////////////////////////// |
| 139 | { |
| 140 | evaluate(); |
| 141 | |
| 142 | s = size; |
| 143 | nc = numComponents; |
| 144 | |
| 145 | return bytes; |
| 146 | } |
| 147 | |
| 148 | //////////////////////////////////////////////////////////////////////// |
| 149 | // |
| 150 | // Description: |
| 151 | // Copy image from another field. |
| 152 | // |
| 153 | // Use: public |
| 154 | |
| 155 | const SoSFImage & |
| 156 | SoSFImage::operator =(const SoSFImage &f) |
| 157 | // |
| 158 | //////////////////////////////////////////////////////////////////////// |
| 159 | { |
| 160 | SbVec2s s; |
| 161 | int nc; |
| 162 | const unsigned char *b = f.getValue(s, nc); |
| 163 | setValue(s, nc, b); |
| 164 | |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | //////////////////////////////////////////////////////////////////////// |
| 169 | // |
| 170 | // Description: |
| 171 | // Returns TRUE if field has same value as given field. |
| 172 | // |
| 173 | // Use: public |
| 174 | |
| 175 | int |
| 176 | SoSFImage::operator ==(const SoSFImage &f) const |
| 177 | // |
| 178 | //////////////////////////////////////////////////////////////////////// |
| 179 | { |
| 180 | // Check easy stuff first |
| 181 | if (size != f.size || numComponents != f.numComponents) |
| 182 | return FALSE; |
| 183 | |
| 184 | if (memcmp(bytes, f.bytes, size[0] * size[1] * numComponents) != 0) |
| 185 | return FALSE; |
| 186 | |
| 187 | return TRUE; |
| 188 | } |
| 189 | |
| 190 | //////////////////////////////////////////////////////////////////////// |
| 191 | // |
| 192 | // Description: |
| 193 | // Edit the values of the image |
| 194 | // |
| 195 | // Use: public |
| 196 | |
| 197 | unsigned char * |
| 198 | SoSFImage::startEditing(SbVec2s &s, int &nc) |
| 199 | // |
| 200 | //////////////////////////////////////////////////////////////////////// |
| 201 | { |
| 202 | s = size; |
| 203 | nc = numComponents; |
| 204 | return bytes; |
| 205 | } |
| 206 | |
| 207 | //////////////////////////////////////////////////////////////////////// |
| 208 | // |
| 209 | // Description: |
| 210 | // Must be called sometime after editValue to let node notify. |
| 211 | // |
| 212 | // Use: public |
| 213 | |
| 214 | void |
| 215 | SoSFImage::finishEditing() |
| 216 | // |
| 217 | //////////////////////////////////////////////////////////////////////// |
| 218 | { |
| 219 | valueChanged(); |
| 220 | } |
| 221 | |
| 222 | //////////////////////////////////////////////////////////////////////// |
| 223 | // |
| 224 | // Description: |
| 225 | // Reads value from file. Returns FALSE on error. |
| 226 | // |
| 227 | // Use: private |
| 228 | |
| 229 | SbBool |
| 230 | SoSFImage::readValue(SoInput *in) |
| 231 | // |
| 232 | //////////////////////////////////////////////////////////////////////// |
| 233 | { |
| 234 | if (!in->read(size[0]) || |
| 235 | !in->read(size[1]) || |
| 236 | !in->read(numComponents)) |
| 237 | return FALSE; |
| 238 | |
| 239 | if (bytes != NULL) delete[] bytes; |
| 240 | bytes = new unsigned char[size[0]*size[1]*numComponents]; |
| 241 | |
| 242 | int byte = 0; |
| 243 | if (in->isBinary()) { |
| 244 | // Inventor version 2.1 and later binary file |
| 245 | if (in->getIVVersion() > 2.0) { |
| 246 | int numBytes = size[0]*size[1]*numComponents; |
| 247 | if (! in->readBinaryArray(bytes, numBytes)) |
| 248 | return FALSE; |
| 249 | } |
| 250 | |
| 251 | // Pre version 2.1 Inventor binary files |
| 252 | else { |
| 253 | for (int i = 0; i < size[0]*size[1]; i++) { |
| 254 | uint32_t l; |
| 255 | |
| 256 | if (!in->read(l)) return FALSE; |
| 257 | for (int j = 0; j < numComponents; j++) { |
| 258 | bytes[byte++] = (unsigned char)( |
| 259 | (l >> (8*(numComponents-j-1))) & 0xFF); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | else { |
| 265 | for (int i = 0; i < size[0]*size[1]; i++) { |
| 266 | uint32_t l; |
| 267 | |
| 268 | if (!in->readHex(l)) return FALSE; |
| 269 | for (int j = 0; j < numComponents; j++) { |
| 270 | bytes[byte++] = (unsigned char)( |
| 271 | (l >> (8*(numComponents-j-1))) & 0xFF); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | //////////////////////////////////////////////////////////////////////// |
| 280 | // |
| 281 | // Description: |
| 282 | // Writes value of field to file. |
| 283 | // |
| 284 | // Use: private |
| 285 | |
| 286 | void |
| 287 | SoSFImage::writeValue(SoOutput *out) const |
| 288 | // |
| 289 | //////////////////////////////////////////////////////////////////////// |
| 290 | { |
| 291 | out->write(size[0]); |
| 292 | |
| 293 | if (! out->isBinary()) |
| 294 | out->write(' '); |
| 295 | |
| 296 | out->write(size[1]); |
| 297 | |
| 298 | if (! out->isBinary()) |
| 299 | out->write(' '); |
| 300 | |
| 301 | out->write(numComponents); |
| 302 | |
| 303 | if (out->isBinary()) { |
| 304 | int numBytes = size[0] * size[1] * numComponents; |
| 305 | out->writeBinaryArray(bytes, numBytes); |
| 306 | } |
| 307 | else { |
| 308 | int byte = 0; |
| 309 | for (int i = 0; i < size[0]*size[1]; i++) { |
| 310 | uint32_t l = 0; |
| 311 | for (int j = 0; j < numComponents; j++) { |
| 312 | l = (l<<8) | bytes[byte++]; |
| 313 | } |
| 314 | out->write(' '); |
| 315 | out->write(l); |
| 316 | } |
| 317 | } |
| 318 | } |