zjf
2023-03-06 392b76515f40376b6d36f40a114850ef63650384
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
/////////////////////////////////////////////////////////////////////////////// 
// Copyright (C) 2002-2016, Open Design Alliance (the "Alliance"). 
// All rights reserved. 
// 
// This software and its documentation and related materials are owned by 
// the Alliance. The software may only be incorporated into application 
// programs owned by members of the Alliance, subject to a signed 
// Membership Agreement and Supplemental Software License Agreement with the
// Alliance. The structure and organization of this software are the valuable  
// trade secrets of the Alliance and its suppliers. The software is also 
// protected by copyright law and international treaty provisions. Application  
// programs incorporating this software must include the following statement 
// with their copyright notices:
//   
//   This application incorporates Teigha(R) software pursuant to a license 
//   agreement with Open Design Alliance.
//   Teigha(R) Copyright (C) 2002-2016 by Open Design Alliance. 
//   All rights reserved.
//
// By use of this software, its documentation or related materials, you 
// acknowledge and accept the above terms.
///////////////////////////////////////////////////////////////////////////////
 
#ifndef _OD_ODEXGIRASTERIMAGE_H_
#define _OD_ODEXGIRASTERIMAGE_H_
 
#include "TD_PackPush.h"
 
#include "OdBinaryData.h"
#include "Gi/GiRasterImage.h"
#include "FlatMemStream.h"
#include "OdPlatform.h"
#include "RxRasterServices.h"
#include "StaticRxObject.h"
 
class OdExGiRasterImage;
/** \details
  This template class is a specialization of the OdSmartPtr class for OdExGiRasterImage object pointers.
*/
typedef OdSmartPtr<OdExGiRasterImage> OdExGiRasterImagePtr;
/** \details
    This class represents a raster image within the Teigha vectorization framework.
    <group ExServices_Classes> 
    Library: Source code provided. 
*/
class OdExGiRasterImage : public OdGiRasterImageParam
{
public:
  OdRxObjectPtr clone() const
  {
    OdSmartPtr<OdExGiRasterImage> pRes = OdRxObjectImpl<OdExGiRasterImage>::createObject();
    pRes->m_bits = m_bits;
    pRes->m_palette = m_palette;
    pRes->m_header = m_header;
    pRes->m_imageSource = m_imageSource;
    pRes->m_transparencyMode = m_transparencyMode;
    return (OdRxObject*)pRes;
  }
 
  struct Header
  {
    Header(OdUInt32 width, OdUInt32 height, OdUInt16 bitPerPixel)
      : m_width(width), m_height(height)
      , m_bitPerPixel(bitPerPixel)
      , m_xPelsPerUnit(0.0)
      , m_yPelsPerUnit(0.0)
      , m_resUnits(OdGiRasterImage::kNone)
     {}
    Header()
      : m_width(0), m_height(0)
      , m_bitPerPixel(0)
      , m_xPelsPerUnit(0.0)
      , m_yPelsPerUnit(0.0)
      , m_resUnits(OdGiRasterImage::kNone)
    {}
    
    OdUInt32  m_width;
    OdUInt32  m_height;
    OdUInt16  m_bitPerPixel;
    double    m_xPelsPerUnit,
              m_yPelsPerUnit;
    OdGiRasterImage::Units 
              m_resUnits;
  };
  
  class Palette
  {
    OdBinaryData m_colorsData;
  public:
    const OdUInt8* getData() const { return m_colorsData.getPtr(); }
    OdBinaryData& data() { return m_colorsData; }
    const OdBinaryData& getBinData() const { return m_colorsData; }
    OdUInt32 numColors() const { return m_colorsData.size() / 4; }
    void setNumColors(OdUInt32 nColors) { m_colorsData.resize(nColors * 4); }
    void setColorAt(OdUInt32 nIndex, OdUInt8 blue, OdUInt8 green, OdUInt8 red, OdUInt8 alpha = 0)
    {
      ODA_ASSERT(nIndex < m_colorsData.size());
      nIndex *= 4;
      m_colorsData[nIndex]     = blue;
      m_colorsData[nIndex + 1] = green;
      m_colorsData[nIndex + 2] = red;
      m_colorsData[nIndex + 3] = alpha;
    }
    
    void colorAt(OdUInt32 nIndex, OdUInt8& blue, OdUInt8& green, OdUInt8& red, OdUInt8* pAlpha = 0) const
    {
      ODA_ASSERT(nIndex < m_colorsData.size());
      nIndex *= 4;
      blue    = m_colorsData[nIndex];
      green   = m_colorsData[nIndex + 1];
      red     = m_colorsData[nIndex + 2];
      if(pAlpha)
      {
        *pAlpha = m_colorsData[nIndex + 3];
      }
    }
  };
  
  private:
    Header           m_header;
    OdBinaryData     m_bits;
    Palette          m_palette;
    ImageSource      m_imageSource;
    TransparencyMode m_transparencyMode;
  public:
    /** \param bitsPerPixel [in]  Color depth.
      \param xPixels [in]  X pixels.
      \param yPixels [in]  Y pixels.
    */
    OdExGiRasterImage(OdUInt32 xPixels, OdUInt32 yPixels, OdUInt8 bitsPerPixel);
    OdExGiRasterImage();
    
    /** \details
      Returns the size in bytes of the pixel data for a specified (or this) RasterImage object.
      \param bitsPerPixel [in]  Color depth.
      \param xPixels [in]  X pixels.
      \param yPixels [in]  Y pixels.
    */
    static OdUInt32 bitDataSize(OdUInt32 xPixels, OdUInt32 yPixels, OdUInt16 bitsPerPixel)
    {
      return ((xPixels * bitsPerPixel +31) & ~31) /8 * yPixels;
    }
    
    OdUInt32 bitDataSize()
    {
      return OdExGiRasterImage::bitDataSize(m_header.m_width, m_header.m_height, 
        m_header.m_bitPerPixel);
    }
    
    /** \details
      Returns a reference to the binary data for this RasterImage object.
    */
    OdBinaryData& bits() { return m_bits; }
    /** \details
      Returns a the binary data for this RasterImage object.
    */
    const OdBinaryData& getBits() const { return m_bits; }
 
    /** \details
      Returns an array of scan lines for this RasterImage object.
      \param numBytes [out]  Receives the number of bytes in the array.
    */
    const OdUInt8* getScanLines(OdUInt32& numBytes) const
    {
      numBytes = m_bits.size();
      return m_bits.asArrayPtr();
    }
 
    /** \details
      Sets the binary data for this RasterImage object.
      \param data [in]  Binary data.
      \param numBytes [in]  Number of bytes in the array.
    */
        void setBits(const OdBinaryData& data)
    {
      ODA_ASSERT(data.size() <= bitDataSize());
      m_bits = data;
    }
    
    void setBits(const OdUInt8* data, OdUInt32 numBytes)
    {
      ODA_ASSERT(numBytes <= bitDataSize());
      m_bits.resize(numBytes);
      ::memcpy(m_bits.asArrayPtr(), data, numBytes);
    }
    
    /** \details
      Sets the metrics of this ReasterImage object.
      
      \param bitsPerPixel [in]  Color depth.
      \param xPixels [in]  X pixels.
      \param yPixels [in]  Y pixels.
    */
    void setMetrics(OdUInt32 xPixels, OdUInt32 yPixels, OdUInt16 bitsPerPixel)
    {
      m_header.m_width = xPixels;
      m_header.m_height = yPixels;
      m_header.m_bitPerPixel = bitsPerPixel;
      m_transparencyMode = (bitsPerPixel < 32) ? kTransparencyDef : kTransparency8Bit;
    }
    
    /** \details
      Sets the default image resolution in pixels per unit of this RasterImage object.
 
      \param xPelsPerUnit [in]  Pixels per unit value (x direction).
      \param yPelsPerUnit [in]  Pixels per unit value (y direction).
      \param units [in]  Units.
    */
     void setDefaultResolution(OdGiRasterImage::Units units, double xPelsPerUnit, double yPelsPerUnit)
    {
      m_header.m_resUnits = units;
      m_header.m_xPelsPerUnit = xPelsPerUnit;
      m_header.m_yPelsPerUnit = yPelsPerUnit;
    }
    
    /** \details
      Sets the number of colors in the palette of this RasterImage object.
      \param numColors [in]  Number of colors.
    */
    void setPalNumColors(OdUInt32 numColors)
    {
      m_palette.setNumColors(numColors);
    }
    
    /** \details
      Returns the number of colors in the palette of this RasterImage object.
    */
    OdUInt32 getPalNumColors() const
    {
      return m_palette.numColors();
    }
    
    /** \details
      Returns the palette of this RasterImage object.
    */
    const Palette& getPalette() const
    {
      return m_palette;
    }
 
    /** \details
      Returns the size in bytes of the palette of this RasterImage object.
    */
    OdUInt32 paletteDataSize() const
    {
      return m_palette.getBinData().size();
    }
 
    /** \details
        Returns the palette data of this RasterImage object.
        \param bytes [in]  Receives the data.
    */
    void paletteData(OdUInt8* bytes) const
    {
      const OdBinaryData& palData = m_palette.getBinData();
      memcpy(bytes, palData.asArrayPtr(), palData.size());
    }
 
    /** \details
        Returns the palette of this RasterImage object.
        \param bytes [in]  Receives the data.
    */
    Palette& palette()
    {
      return m_palette;
    }
    /** \details
      Sets the specified color in the palette of this RasterImage object.
      
      \param paletteIndex [in]  Color number.
      \param blue [in]  Blue component.
      \param green [in]  Green component.
      \param red [in]  Red component.
      \param alpha [in]  Alpha component.
    */
    void setPalColorAt(OdUInt32 paletteIndex, OdUInt8 blue, OdUInt8 green, OdUInt8 red, OdUInt8 alpha = 0)
    {
      m_palette.setColorAt(paletteIndex, blue, green, red, alpha);
    }
    
    /** \details
      Returns the specified color in the palette of this RasterImage object.
      
      \param paletteIndex [in]  Color number.
      \param blue [out]  Receives the Blue component.
      \param green [out]  Receives the Green component.
      \param red [out]  Receives the Red component.
      \param pAlpha [out]  Pointer to the object to receive the alpha component..
    */
    void getPalColorAt(OdUInt32 paletteIndex, OdUInt8& blue, OdUInt8& green, OdUInt8& red, OdUInt8* pAlpha = 0) const
    {
      m_palette.colorAt(paletteIndex, blue, green, red, pAlpha);
    }
    
    // Gets the color(RGB) in specified point of raster image
 
    /** \details
      Returns the specified color at the specified pixel of this RasterImage object.
      
      \param x [in]  X-coordinate.
      \param y [in]  Y-Coordinate
      \param blue [out]  Receives the Blue component.
      \param green [out]  Receives the Green component.
      \param red [out]  Receives the Red component.
    */
    void getColorAt(OdUInt32 x, OdUInt32 y, OdUInt8& blue, OdUInt8& green, OdUInt8& red) const;
    
    // virtual overrides
 
    void copyFrom(const OdRxObject* pOtherObj);
    OdUInt32 pixelWidth() const;
    OdUInt32 pixelHeight() const;
    OdUInt32 colorDepth() const;
    OdGiRasterImage::Units defaultResolution(double& xPelsPerUnit, double& yPelsPerUnit) const;
    
    /** Returns bitmap info header
    */
    // void getBitmapInfoHeader(BITMAPINFOHEADER& bmih) const;
 
    
    OdUInt32 numColors() const;
 
    virtual ODCOLORREF color(OdUInt32 colorIndex) const;
 
    void scanLines(OdUInt8* scnLines, OdUInt32 firstScanline, OdUInt32 numLines = 1) const;
 
    /** \details
        Returns pointer to image's pixels in bmp format.
        Note that it is optional to have implementation of this function
        (to prevent dummy copying of pixels data). You can return NULL in your
        implementation if it is inconvenient in some case - any functionality
        uses this interface must take it into account.
    */
 
    virtual const OdUInt8* scanLines() const;
 
    virtual PixelFormatInfo pixelFormat() const;
    
    virtual OdUInt32 scanLinesAlignment() const;
 
    virtual OdUInt32 supportedParams() const;
    virtual ImageSource imageSource() const;
    virtual void setImageSource(ImageSource source);
    virtual TransparencyMode transparencyMode() const;
    virtual void setTransparencyMode(TransparencyMode mode);
};
 
/** \details
    This class implements platform-dependent loading and saving
    of Raster Image files for Teigha.
 
    <group ExServices_Classes> 
    Library: Source code provided. 
*/
class ExRasterModule : public OdRxRasterServices
{
public:
  /** \details
      Loads the specified Raster Image file.
      \param filename [in]  Filename of the Raster Image file to be read.
      \param pStreamBuf [in]  Pointer to the StreamBuf object from which the data are to be read.
      \param pFlagsChain [in]  Optional zero-terminated loading flag pairs array.
 
      \remarks
      The returned pointer is expected to be passed to OdGiViewportGeometry::rasterImageDc().
  */
  OdGiRasterImagePtr loadRasterImage(const OdString &filename, const OdUInt32 *pFlagsChain = NULL);
  OdGiRasterImagePtr loadRasterImage(OdStreamBuf *pStreamBuf, const OdUInt32 *pFlagsChain = NULL);
 
  /** \details
      Saves specified Raster Image to the specified file.
      \param rasterImage [in]  Raster image to be saved.
      \param filename [in]  Filename of the Raster Image file to be written.
      \param type [in]  Image format type to be written.
      \param pFlagsChain [in]  Optional zero-terminated saving flag pairs array.
  */
  bool saveRasterImage(const OdGiRasterImage* rasterImage, const OdString& filename,
                       const OdUInt32 *pFlagsChain = NULL);
  bool saveRasterImage(const OdGiRasterImage* rasterImage, const OdString& filename,
                       OdUInt32 type, const OdUInt32 *pFlagsChain = NULL);
 
  /** \details
      Try to convert raster image (RGB) to JPEG or other type.
      \param pRaster [in]  Raster image to be converted.
      \param type [in]  Image format type to be converted.
      \param pStreamBuf [in]  Pointer to the StreamBuf object to which the data are to be stored.
      \param pFlagsChain [in]  Optional zero-terminated saving flag pairs array.
  */
  bool convertRasterImage(const OdGiRasterImage* pRaster, OdUInt32 type,
                          OdStreamBuf* pStreamBuf, const OdUInt32 *pFlagsChain = NULL);
 
  /** \details
      Try to convert raster image to other type.
      \param pSrcStream [in]  Pointer to the StreamBuf object from which the data are to be converted.
      \param pDstStream [in]  Pointer to the StreamBuf object to which the data are to be converted.
      \param type [in]  Image format type to be converted.
      \param pFlagsChainSrc [in]  Optional zero-terminated loading flag pairs array.
      \param pFlagsChainDst [in]  Optional zero-terminated saving flag pairs array.
  */
  bool convertRasterImage(OdStreamBuf* pSrcStream, OdStreamBuf* pDstStream, OdUInt32 type,
                          const OdUInt32 *pFlagsChainSrc = NULL, const OdUInt32 *pFlagsChainDst = NULL);
 
  /** \details
      Returns array of supported image format types.
  */
  OdUInt32Array getRasterImageTypes() const;
 
  /** \details
      Get file extension and filter name by type.
      \param type [in]  Image format type to be formatted.
      \param psFilterName [out]  Output filter name (can be Null).
  */
  OdString mapTypeToExtension(OdUInt32 type, OdString* psFilterName) const;
 
  /** \details
      Get image format type by file extension.
      \param extension [in]  File extension.
  */
  OdUInt32 mapExtensionToType(const OdString& extension) const;
 
  /** \details
      Try to detect image format type from input stream.
      \param filename [in]  Filename of the Raster Image file to be checked.
      \param pStreamBuf [in]  Pointer to the StreamBuf object from which the data are to be checked.
  */
  OdUInt32 getImageFormat(const OdString &filename) const;
  OdUInt32 getImageFormat(OdStreamBuf* pStreamBuf) const;
 
  /** \details
      Initialize module.
  */
  void initApp();
  /** \details
      Uninitialize module.
  */
  void uninitApp();
};
 
#include "TD_PackPop.h"
 
#endif //#ifndef _OD_ODEXGIRASTERIMAGE_H_