summaryrefslogtreecommitdiff
path: root/src/Image/Image_PixMap.cxx
blob: 02c31bb1f6887366180a55c1b13118ccfae1ba36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// File       Image_PixMap.cxx
// Created    16 September 2010
// Author     KGV
// Copyright  OpenCASCADE 2010

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#ifdef HAVE_FREEIMAGE
  #include <FreeImagePlus.h>
  #include <Image_PixMap.ixx>
  /* OCC22216 NOTE: linker dependency can be switched off by undefining macro */ 
  
  #ifdef __BORLANDC__
  #pragma link "FreeImage.lib"
  #pragma link "FreeImagePlus.lib"
  #endif
#else
  #include <Image_PixMap.ixx>
  #include <fstream>

  #include <stdio.h>

  #if (defined(BYTE_ORDER)   && BYTE_ORDER==BIG_ENDIAN) || \
      (defined(__BYTE_ORDER) && __BYTE_ORDER==__BIG_ENDIAN) || \
       defined(__BIG_ENDIAN__)
    #define THE_BIGENDIAN
  #endif

  // dummy class which can only dump to PPM format
  class fipImage
  {
  public:

    typedef struct tagRGBQuad {
    #ifndef THE_BIGENDIAN
      Standard_Byte rgbBlue;
      Standard_Byte rgbGreen;
      Standard_Byte rgbRed;
    #else
      Standard_Byte rgbRed;
      Standard_Byte rgbGreen;
      Standard_Byte rgbBlue;
    #endif
      Standard_Byte rgbReserved;
    } RGBQuad_t;

  public:

    fipImage()
    : myDataPtr(NULL),
      mySizeX(0),
      mySizeY(0),
      myBytesPerLine(0),
      myBytesPerPixel(3)
    {
      //
    }

    fipImage (const Standard_Integer theSizeX, const Standard_Integer theSizeY,
              const Standard_Integer theBytesPerLine = 0, const Standard_Integer theBytesPerPixel = 3)
    : myDataPtr(NULL),
      mySizeX (theSizeX),
      mySizeY (theSizeY),
      myBytesPerLine (theBytesPerLine),
      myBytesPerPixel (theBytesPerPixel)
    {
      if (myBytesPerLine == 0)
      {
        myBytesPerLine = mySizeX * myBytesPerPixel;
      }
      myDataPtr = new Standard_Byte[myBytesPerLine * mySizeY];
    }

    ~fipImage()
    {
      delete[] myDataPtr;
    }

    Standard_Integer getHeight() const
    {
      return mySizeY;
    }

    Standard_Integer getWidth() const
    {
      return mySizeX;
    }

    Standard_Integer getBytesPerPixel() const
    {
      return myBytesPerPixel;
    }

    Standard_Integer getBytesPerLine() const
    {
      return myBytesPerLine;
    }

    Standard_Byte* getData() const
    {
      return myDataPtr;
    }

    Standard_Byte* getScanLine (const Standard_Integer theRow) const
    {
      return &myDataPtr[theRow * myBytesPerLine];
    }

    Quantity_Color getPixelColor (const Standard_Integer theCol,
                                  const Standard_Integer theRow) const
    {
      RGBQuad_t* aPixel = (RGBQuad_t* )&getScanLine (theRow)[theCol * myBytesPerPixel];
      return Quantity_Color (Standard_Real (aPixel->rgbRed)   / 255.0,
                             Standard_Real (aPixel->rgbGreen) / 255.0,
                             Standard_Real (aPixel->rgbBlue)  / 255.0,
                             Quantity_TOC_RGB);
    }

    Standard_Boolean savePPM (const Standard_CString theFileName) const
    {
      // Open file
      FILE* pFile = fopen (theFileName, "wb");
      if (pFile == NULL)
      {
        return Standard_False;
      }

      // Write header
      fprintf (pFile, "P6\n%d %d\n255\n", mySizeX, mySizeY);

      // Write pixel data
      Standard_Byte* aScanLine;
      RGBQuad_t* aPixel;
      // image stored upside-down
      for (Standard_Integer aRow = mySizeY - 1; aRow >= 0; --aRow)
      {
        aScanLine = getScanLine (aRow);
        for (Standard_Integer aCol = 0; aCol < mySizeX; ++aCol)
        {
          aPixel = (RGBQuad_t* )&aScanLine[aCol * myBytesPerPixel];
          fwrite (&aPixel->rgbRed,   1, 1, pFile);
          fwrite (&aPixel->rgbGreen, 1, 1, pFile);
          fwrite (&aPixel->rgbBlue,  1, 1, pFile);
        }
      }

      // Close file
      fclose (pFile);
      return Standard_True;
    }

    Standard_Integer getMaxRowAligmentBytes() const
    {
      Standard_Integer aDeltaBytes = myBytesPerLine - myBytesPerPixel * mySizeX;
      for (Standard_Integer anAligment = 16; anAligment > 1; anAligment /= 2)
      {
        if (isRowAlignedTo (anAligment, aDeltaBytes))
        {
          return anAligment;
        }
      }
      return 1;
    }

  private:

    Standard_Boolean isRowAlignedTo (const Standard_Integer theAligmentBytes,
                                     const Standard_Integer theDeltaBytes) const
    {
      return (theDeltaBytes < theAligmentBytes) &&
             ((myBytesPerLine % theAligmentBytes) == 0);
    }

  private:

    Standard_Byte* myDataPtr;
    Standard_Integer mySizeX;
    Standard_Integer mySizeY;
    Standard_Integer myBytesPerLine;
    Standard_Integer myBytesPerPixel;

  };
#endif

#include <gp.hxx>
#include <TCollection_AsciiString.hxx>

//=======================================================================
//function : Image_PixMap
//purpose  :
//=======================================================================
Image_PixMap::Image_PixMap (const Standard_Integer theWidth,
                            const Standard_Integer theHeight,
                            const Image_TypeOfImage theType)
:	Aspect_PixMap (theWidth, theHeight, 1),
	myImage()
{
#ifdef HAVE_FREEIMAGE
  FREE_IMAGE_TYPE aFIType = FIT_UNKNOWN;
  int aBitsPerPixel = 0;
  switch (theType)
  {
    case Image_TOI_RGBA:
      aFIType = FIT_BITMAP;
      aBitsPerPixel = 32;
      break;
    case Image_TOI_RGBF:
      aFIType = FIT_RGBF;
      aBitsPerPixel = 96;
      break;
    case Image_TOI_RGBAF:
      aFIType = FIT_RGBAF;
      aBitsPerPixel = 128;
      break;
    case Image_TOI_FLOAT:
      aFIType = FIT_FLOAT;
      aBitsPerPixel = 32;
      break;
    case Image_TOI_RGB:
    default:
      aFIType = FIT_BITMAP;
      aBitsPerPixel = 24;
      break;
  }
  myImage = new fipImage (aFIType, theWidth, theHeight, aBitsPerPixel);
#else
  Standard_Integer aBytesPerPixel = 0;
  switch (theType)
  {
    case Image_TOI_RGBAF:
      std::cerr << "Float formats not supported\n";
    case Image_TOI_RGBA:
      aBytesPerPixel = 4;
      break;
    case Image_TOI_RGBF:
    case Image_TOI_FLOAT:
      std::cerr << "Float formats not supported\n";
    case Image_TOI_RGB:
    default:
      aBytesPerPixel = 3;
      break;
  }
  myImage = new fipImage (theWidth, theHeight, 0, aBytesPerPixel);
  //
#endif
}

//=======================================================================
//function : Image_PixMap
//purpose  :
//=======================================================================
Image_PixMap::Image_PixMap (const Standard_PByte theDataPtr,
                            const Standard_Integer theWidth, const Standard_Integer theHeight,
                            const Standard_Integer thePitch, const Standard_Integer theBitsPerPixel,
                            const Standard_Boolean theIsTopDown)
:	Aspect_PixMap (theWidth, theHeight, 1),
	myImage (new fipImage())
{
#ifdef HAVE_FREEIMAGE
  *myImage = FreeImage_ConvertFromRawBits (theDataPtr,
                                           theWidth, theHeight,
                                           thePitch, theBitsPerPixel,
                                           0, 0, 0,
                                           theIsTopDown);
  if (theBitsPerPixel != 24)
  {
    myImage->convertTo24Bits();
  }
#else
  myImage = new fipImage (theWidth, theHeight, thePitch, theBitsPerPixel / 8);
  Standard_Integer aRowStart = !theIsTopDown ? 0 : (theHeight - 1);
  Standard_Integer aRowDelta = !theIsTopDown ? 1 : -1;
  for (Standard_Integer aRowFrom (aRowStart), aRowTo (0);
       aRowFrom >= 0 && aRowFrom < theHeight;
       aRowFrom += aRowDelta, ++aRowTo)
  {
    memcpy (myImage->getScanLine (aRowTo),
            &theDataPtr[aRowFrom * thePitch],
            myImage->getBytesPerLine());
  }
#endif
}

//=======================================================================
//function : Destroy
//purpose  :
//=======================================================================
void Image_PixMap::Destroy()
{
  myImage = Image_HPrivateImage();
}

//=======================================================================
//function : Dump
//purpose  :
//=======================================================================
Standard_Boolean Image_PixMap::Dump (const Standard_CString theFilename,
                                     const Standard_Real theGammaCorr) const
{
#ifdef HAVE_FREEIMAGE
  FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilename (theFilename);
  if (anImageFormat == FIF_UNKNOWN)
  {
    std::cerr << "Image_PixMap, image format doesn't supported!\n";
    return Standard_False;
  }

  Standard_Boolean isCopied = Standard_False;
  Image_HPrivateImage anImageToDump = myImage;
  if (Abs (theGammaCorr - 1.0) > gp::Resolution())
  {
    if (!isCopied)
    {
      isCopied = Standard_True;
      anImageToDump = new fipImage (*myImage);
    }
    anImageToDump->adjustGamma (theGammaCorr);
  }

  switch (anImageFormat)
  {
    case FIF_GIF:
      if (!isCopied)
      {
        isCopied = Standard_True;
        anImageToDump = new fipImage (*myImage);
      }
      // need convertion to image with pallete
      anImageToDump->colorQuantize (FIQ_NNQUANT);
      break;
    case FIF_EXR:
      if (myImage->getImageType() == FIT_BITMAP)
      {
        if (!isCopied)
        {
          isCopied = Standard_True;
          anImageToDump = new fipImage (*myImage);
        }
        anImageToDump->convertToType (FIT_RGBF);
      }
      break;
    default:
      if (myImage->getImageType() != FIT_BITMAP)
      {
        if (!isCopied)
        {
          isCopied = Standard_True;
          anImageToDump = new fipImage (*myImage);
        }
        anImageToDump->convertToType (FIT_BITMAP);
      }
      break;
  }
  return anImageToDump->save (theFilename);
#else
  return myImage->savePPM (theFilename);
#endif
}

Aspect_Handle Image_PixMap::PixmapID() const
{
  return Aspect_Handle();
}

void Image_PixMap::AccessBuffer (Image_CRawBufferData& theBuffer) const
{
  theBuffer.widthPx  = myImage->getWidth();
  theBuffer.heightPx = myImage->getHeight();
#ifdef HAVE_FREEIMAGE
  theBuffer.rowAligmentBytes = 4; // 32 bits according to FreeImage documentation
  switch (myImage->getImageType())
  {
    case FIT_FLOAT:
      theBuffer.format = TDepthComponent;
      theBuffer.type = TFloat;
      break;
    case FIT_RGBF:
      theBuffer.format = TRGB;
      theBuffer.type = TFloat;
      break;
    case FIT_RGBAF:
      theBuffer.format = TRGBA;
      theBuffer.type = TFloat;
      break;
    case FIT_BITMAP:
    default:
    #if defined(FREEIMAGE_BIGENDIAN)
      theBuffer.format = (myImage->getColorType() == FIC_RGBALPHA) ? TRGBA : TRGB;
    #else
      theBuffer.format = (myImage->getColorType() == FIC_RGBALPHA) ? TBGRA : TBGR;
    #endif
      theBuffer.type = TUByte;
      break;
  }
  theBuffer.dataPtr = myImage->accessPixels();
#else
  theBuffer.rowAligmentBytes = myImage->getMaxRowAligmentBytes();
  theBuffer.format = (myImage->getBytesPerPixel() == 4) ? TBGRA : TBGR;
  theBuffer.type = TUByte;
  theBuffer.dataPtr = myImage->getData();
#endif
}

Quantity_Color Image_PixMap::PixelColor (const Standard_Integer theX,
                                         const Standard_Integer theY) const
{
  Standard_Integer aScanlineId = myImage->getHeight() - theY - 1;
  if (theX < 0 || theX >= myImage->getWidth() ||
      theY < 0 || theY >= myImage->getHeight())
  {
    return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);
  }
#ifdef HAVE_FREEIMAGE
  else if (myImage->getImageType() == FIT_BITMAP)
  {
    RGBQUAD aValue; memset (&aValue, 0, sizeof(aValue));
    myImage->getPixelColor (theX, aScanlineId, &aValue);
    return Quantity_Color (Standard_Real (aValue.rgbRed)   / 255.0,
                           Standard_Real (aValue.rgbGreen) / 255.0,
                           Standard_Real (aValue.rgbBlue)  / 255.0,
                           Quantity_TOC_RGB);
  }
  else
  {
    switch (myImage->getImageType())
    {
      case FIT_FLOAT:
      {
        float* aScanLine = (float* )myImage->getScanLine (aScanlineId);
        Quantity_Parameter aValue = Quantity_Parameter (aScanLine[theX]);
        return Quantity_Color (aValue, aValue, aValue, Quantity_TOC_RGB);
      }
      case FIT_RGBF:
      {
        FIRGBF* aScanLine = (FIRGBF* )myImage->getScanLine (aScanlineId);
        FIRGBF* aPixel = &aScanLine[theX];
        return Quantity_Color (Quantity_Parameter (aPixel->red),
                               Quantity_Parameter (aPixel->green),
                               Quantity_Parameter (aPixel->blue),
                               Quantity_TOC_RGB);
      }
      case FIT_RGBAF:
      {
        FIRGBAF* aScanLine = (FIRGBAF* )myImage->getScanLine (aScanlineId);
        FIRGBAF* aPixel = &aScanLine[theX];
        return Quantity_Color (Quantity_Parameter (aPixel->red),
                               Quantity_Parameter (aPixel->green),
                               Quantity_Parameter (aPixel->blue),
                               Quantity_TOC_RGB);
      }
      default:
      {
        // not supported image type
        return Quantity_Color (0.0, 0.0, 0.0, Quantity_TOC_RGB);
      }
    }
  }
#else
  return myImage->getPixelColor (theX, aScanlineId);
#endif
}