File indexing completed on 2025-01-18 10:04:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef Image_PixMap_HeaderFile
0017 #define Image_PixMap_HeaderFile
0018
0019 #include <Image_CompressedFormat.hxx>
0020 #include <Image_PixMapData.hxx>
0021 #include <Standard_Transient.hxx>
0022 #include <Quantity_ColorRGBA.hxx>
0023
0024
0025 class Image_PixMap : public Standard_Transient
0026 {
0027 DEFINE_STANDARD_RTTIEXT(Image_PixMap, Standard_Transient)
0028 public:
0029
0030
0031 static inline bool IsBigEndianHost()
0032 {
0033 union { int myInt; char myChar[sizeof(int)]; } aUnion;
0034 aUnion.myInt = 1;
0035 return !aUnion.myChar[0];
0036 }
0037
0038
0039 Standard_EXPORT static Standard_Size SizePixelBytes (const Image_Format thePixelFormat);
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 Standard_EXPORT static bool SwapRgbaBgra (Image_PixMap& theImage);
0050
0051
0052 Standard_EXPORT static void ToBlackWhite (Image_PixMap& theImage);
0053
0054
0055 Standard_EXPORT static bool FlipY (Image_PixMap& theImage);
0056
0057
0058 Standard_EXPORT static const Handle(NCollection_BaseAllocator)& DefaultAllocator();
0059
0060
0061 Standard_EXPORT static Standard_CString ImageFormatToString (Image_Format theFormat);
0062
0063
0064 Standard_EXPORT static Standard_CString ImageFormatToString (Image_CompressedFormat theFormat);
0065
0066
0067
0068
0069
0070
0071 Standard_EXPORT static Quantity_ColorRGBA ColorFromRawPixel (const Standard_Byte* theRawValue,
0072 const Image_Format theFormat,
0073 const Standard_Boolean theToLinearize = false);
0074
0075
0076
0077
0078
0079
0080 Standard_EXPORT static void ColorToRawPixel (Standard_Byte* theRawValue,
0081 const Image_Format theFormat,
0082 const Quantity_ColorRGBA& theColor,
0083 const Standard_Boolean theToDeLinearize = false);
0084
0085 public:
0086
0087
0088 Image_Format Format() const { return myImgFormat; }
0089
0090
0091
0092
0093
0094 Standard_EXPORT void SetFormat (const Image_Format thePixelFormat);
0095
0096
0097 Standard_Size Width() const { return myData.SizeX; }
0098
0099
0100 Standard_Size Height() const { return myData.SizeY; }
0101
0102
0103 Standard_Size Depth() const { return myData.SizeZ; }
0104
0105
0106 Standard_Size SizeX() const { return myData.SizeX; }
0107
0108
0109 Standard_Size SizeY() const { return myData.SizeY; }
0110
0111
0112 Standard_Size SizeZ() const { return myData.SizeZ; }
0113
0114
0115 NCollection_Vec3<Standard_Size> SizeXYZ() const
0116 {
0117 return NCollection_Vec3<Standard_Size> (myData.SizeX, myData.SizeY, myData.SizeZ);
0118 }
0119
0120
0121 Standard_Real Ratio() const
0122 {
0123 return (SizeY() > 0) ? (Standard_Real(SizeX()) / Standard_Real(SizeY())) : 1.0;
0124 }
0125
0126
0127 bool IsEmpty() const { return myData.IsEmpty(); }
0128
0129
0130 Standard_EXPORT Image_PixMap();
0131
0132
0133 Standard_EXPORT virtual ~Image_PixMap();
0134
0135
0136
0137
0138
0139
0140
0141 Quantity_ColorRGBA PixelColor (Standard_Integer theX,
0142 Standard_Integer theY,
0143 Standard_Boolean theToLinearize = false) const
0144 {
0145 if (IsEmpty()
0146 || theX < 0 || (Standard_Size )theX >= SizeX()
0147 || theY < 0 || (Standard_Size )theY >= SizeY())
0148 {
0149 return Quantity_ColorRGBA (0.0f, 0.0f, 0.0f, 0.0f);
0150 }
0151
0152 const Standard_Byte* aRawPixel = RawValueXY (theX, theY);
0153 return ColorFromRawPixel (aRawPixel, myImgFormat, theToLinearize);
0154 }
0155
0156
0157
0158
0159
0160
0161
0162 void SetPixelColor (const Standard_Integer theX,
0163 const Standard_Integer theY,
0164 const Quantity_Color& theColor,
0165 const Standard_Boolean theToDeLinearize = false)
0166 {
0167 SetPixelColor (theX, theY, Quantity_ColorRGBA (theColor, 1.0f), theToDeLinearize);
0168 }
0169
0170
0171
0172
0173
0174
0175
0176 void SetPixelColor (const Standard_Integer theX,
0177 const Standard_Integer theY,
0178 const Quantity_ColorRGBA& theColor,
0179 const Standard_Boolean theToDeLinearize = false)
0180 {
0181 if (IsEmpty()
0182 || theX < 0 || Standard_Size(theX) >= SizeX()
0183 || theY < 0 || Standard_Size(theY) >= SizeY())
0184 {
0185 return;
0186 }
0187
0188 Standard_Byte* aRawPixel = ChangeRawValueXY (theX, theY);
0189 ColorToRawPixel (aRawPixel, myImgFormat, theColor, theToDeLinearize);
0190 }
0191
0192
0193
0194
0195
0196 Standard_EXPORT virtual bool InitWrapper (Image_Format thePixelFormat,
0197 Standard_Byte* theDataPtr,
0198 const Standard_Size theSizeX,
0199 const Standard_Size theSizeY,
0200 const Standard_Size theSizeRowBytes = 0);
0201
0202
0203
0204 Standard_EXPORT virtual bool InitTrash (Image_Format thePixelFormat,
0205 const Standard_Size theSizeX,
0206 const Standard_Size theSizeY,
0207 const Standard_Size theSizeRowBytes = 0);
0208
0209
0210
0211 Standard_EXPORT virtual bool InitCopy (const Image_PixMap& theCopy);
0212
0213
0214
0215 bool InitZero (Image_Format thePixelFormat,
0216 const Standard_Size theSizeX,
0217 const Standard_Size theSizeY,
0218 const Standard_Size theSizeRowBytes = 0,
0219 const Standard_Byte theValue = 0)
0220 {
0221 return InitZero3D (thePixelFormat, NCollection_Vec3<Standard_Size> (theSizeX, theSizeY, 1), theSizeRowBytes, theValue);
0222 }
0223
0224
0225 Standard_EXPORT virtual void Clear();
0226
0227 public:
0228
0229
0230
0231
0232
0233 Standard_EXPORT virtual bool InitWrapper3D (Image_Format thePixelFormat,
0234 Standard_Byte* theDataPtr,
0235 const NCollection_Vec3<Standard_Size>& theSizeXYZ,
0236 const Standard_Size theSizeRowBytes = 0);
0237
0238
0239
0240 Standard_EXPORT virtual bool InitTrash3D (Image_Format thePixelFormat,
0241 const NCollection_Vec3<Standard_Size>& theSizeXYZ,
0242 const Standard_Size theSizeRowBytes = 0);
0243
0244
0245
0246 Standard_EXPORT bool InitZero3D (Image_Format thePixelFormat,
0247 const NCollection_Vec3<Standard_Size>& theSizeXYZ,
0248 const Standard_Size theSizeRowBytes = 0,
0249 const Standard_Byte theValue = 0);
0250
0251 public:
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262 bool IsTopDown() const { return myData.TopToDown == 1; }
0263
0264
0265
0266
0267 void SetTopDown (const bool theIsTopDown) { myData.SetTopDown (theIsTopDown); }
0268
0269
0270
0271 Standard_Size TopDownInc() const { return myData.TopToDown; }
0272
0273
0274 const Standard_Byte* Data() const { return myData.Data(); }
0275
0276
0277 Standard_Byte* ChangeData() { return myData.ChangeData(); }
0278
0279
0280
0281 const Standard_Byte* Row (Standard_Size theRow) const { return myData.Row (theRow); }
0282
0283
0284
0285 Standard_Byte* ChangeRow (Standard_Size theRow) { return myData.ChangeRow (theRow); }
0286
0287
0288
0289 const Standard_Byte* Slice (Standard_Size theSlice) const { return myData.Slice (theSlice); }
0290
0291
0292
0293 Standard_Byte* ChangeSlice (Standard_Size theSlice) { return myData.ChangeSlice (theSlice); }
0294
0295
0296
0297 const Standard_Byte* SliceRow (Standard_Size theSlice,
0298 Standard_Size theRow) const
0299 {
0300 return myData.SliceRow (theSlice, theRow);
0301 }
0302
0303
0304
0305 Standard_Byte* ChangeSliceRow (Standard_Size theSlice,
0306 Standard_Size theRow)
0307 {
0308 return myData.ChangeSliceRow (theSlice, theRow);
0309 }
0310
0311
0312 Standard_Size SizePixelBytes() const { return myData.SizeBPP; }
0313
0314
0315
0316 Standard_Size SizeRowBytes() const { return myData.SizeRowBytes; }
0317
0318
0319 Standard_Size RowExtraBytes() const
0320 {
0321 return SizeRowBytes() - SizeX() * SizePixelBytes();
0322 }
0323
0324
0325
0326 Standard_Size MaxRowAligmentBytes() const { return myData.MaxRowAligmentBytes(); }
0327
0328
0329 Standard_Size SizeSliceBytes() const { return myData.SizeSliceBytes; }
0330
0331
0332 Standard_Size SizeBytes() const { return myData.Size(); }
0333
0334 public:
0335
0336
0337
0338
0339
0340 template <typename ColorType_t>
0341 const ColorType_t& Value (Standard_Size theRow,
0342 Standard_Size theCol) const
0343 {
0344 return *reinterpret_cast<const ColorType_t*>(myData.Value (theRow, theCol));
0345 }
0346
0347
0348
0349
0350
0351 template <typename ColorType_t>
0352 ColorType_t& ChangeValue (Standard_Size theRow,
0353 Standard_Size theCol)
0354 {
0355 return *reinterpret_cast<ColorType_t* >(myData.ChangeValue (theRow, theCol));
0356 }
0357
0358
0359
0360
0361
0362 const Standard_Byte* RawValue (Standard_Size theRow,
0363 Standard_Size theCol) const
0364 {
0365 return myData.Value (theRow, theCol);
0366 }
0367
0368
0369
0370
0371
0372 Standard_Byte* ChangeRawValue (Standard_Size theRow,
0373 Standard_Size theCol)
0374 {
0375 return myData.ChangeValue (theRow, theCol);
0376 }
0377
0378
0379
0380
0381
0382 template <typename ColorType_t>
0383 const ColorType_t& ValueXY (Standard_Size theX,
0384 Standard_Size theY) const
0385 {
0386 return *reinterpret_cast<const ColorType_t*>(myData.ValueXY (theX, theY));
0387 }
0388
0389
0390
0391
0392
0393 template <typename ColorType_t>
0394 ColorType_t& ChangeValueXY (Standard_Size theX,
0395 Standard_Size theY)
0396 {
0397 return *reinterpret_cast<ColorType_t* >(myData.ChangeValueXY (theX, theY));
0398 }
0399
0400
0401
0402
0403
0404 const Standard_Byte* RawValueXY (Standard_Size theX,
0405 Standard_Size theY) const
0406 {
0407 return myData.ValueXY (theX, theY);
0408 }
0409
0410
0411
0412
0413
0414 Standard_Byte* ChangeRawValueXY (Standard_Size theX,
0415 Standard_Size theY)
0416 {
0417 return myData.ChangeValueXY (theX, theY);
0418 }
0419
0420 public:
0421
0422
0423
0424
0425
0426 template <typename ColorType_t>
0427 const ColorType_t& ValueXYZ (Standard_Size theX,
0428 Standard_Size theY,
0429 Standard_Size theZ) const
0430 {
0431 return *reinterpret_cast<const ColorType_t*>(myData.ValueXYZ (theX, theY, theZ));
0432 }
0433
0434
0435
0436
0437
0438 template <typename ColorType_t>
0439 ColorType_t& ChangeValueXYZ (Standard_Size theX,
0440 Standard_Size theY,
0441 Standard_Size theZ)
0442 {
0443 return *reinterpret_cast<ColorType_t* >(myData.ChangeValueXYZ (theX, theY, theZ));
0444 }
0445
0446
0447
0448
0449
0450 const Standard_Byte* RawValueXYZ (Standard_Size theX,
0451 Standard_Size theY,
0452 Standard_Size theZ) const
0453 {
0454 return myData.ValueXYZ (theX, theY, theZ);
0455 }
0456
0457
0458
0459
0460
0461 Standard_Byte* ChangeRawValueXYZ (Standard_Size theX,
0462 Standard_Size theY,
0463 Standard_Size theZ)
0464 {
0465 return myData.ChangeValueXYZ (theX, theY, theZ);
0466 }
0467
0468 public:
0469
0470
0471 static float ConvertFromHalfFloat (const uint16_t theHalf)
0472 {
0473 union FloatUint32 { float Float32; uint32_t UInt32; };
0474
0475 const uint32_t e = (theHalf & 0x7C00) >> 10;
0476 const uint32_t m = (theHalf & 0x03FF) << 13;
0477 FloatUint32 mf, aRes;
0478 mf.Float32 = (float )m;
0479 const uint32_t v = mf.UInt32 >> 23;
0480 aRes.UInt32 = (theHalf & 0x8000)<<16 | (e != 0) * ((e + 112) << 23 | m) | ((e == 0) & (m != 0)) * ((v - 37) << 23 | ((m << (150 - v)) & 0x007FE000));
0481 return aRes.Float32;
0482 }
0483
0484
0485
0486 static uint16_t ConvertToHalfFloat (const float theFloat)
0487 {
0488 union FloatUint32 { float Float32; uint32_t UInt32; };
0489 FloatUint32 anInput;
0490 anInput.Float32 = theFloat;
0491 const uint32_t b = anInput.UInt32 + 0x00001000;
0492 const uint32_t e = (b & 0x7F800000) >> 23;
0493 const uint32_t m = b & 0x007FFFFF;
0494 return (uint16_t)((b & 0x80000000) >> 16 | (e > 112) * ((((e - 112) << 10) & 0x7C00) | m >> 13)
0495 | ((e < 113) & (e > 101)) * ((((0x007FF000 + m) >> (125 - e)) + 1) >> 1) | (e > 143) * 0x7FFF);
0496 }
0497
0498 protected:
0499
0500 Image_PixMapData myData;
0501 Image_Format myImgFormat;
0502
0503 private:
0504
0505
0506 Image_PixMap (const Image_PixMap& );
0507 Image_PixMap& operator= (const Image_PixMap& );
0508
0509 };
0510
0511 DEFINE_STANDARD_HANDLE(Image_PixMap, Standard_Transient)
0512
0513 #endif