/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, 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 Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 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" #include "OdVector.h" template class OdMemoryAllocator64 { public: typedef size_t size_type; static inline void copyAssignRangeDisjoint(T *pDestination, const T *pSource, size_type numElements) { memcpy(pDestination, pSource, numElements * sizeof(T)); } static inline void copyAssignRange(T *pDestination, const T *pSource, size_type numElements) { memmove(pDestination, pSource, numElements * sizeof(T)); } static inline void moveAssignRange(T *pDestination, T *pSource, size_type numElements) { memmove(pDestination, pSource, numElements * sizeof(T)); } static inline void defaultConstruct(T *pElement) { *pElement = T(); } static inline void copyConstruct(T *pElement, const T &value) { *pElement = value; } static inline void moveConstruct(T *pElement, T &&value) { *pElement = value; } static inline void copyConstructFill(T *pDestination, size_type numElements, const T &value) { while (numElements--) { pDestination[numElements] = value; } } static inline void copyConstructRange(T *pDestination, const T *pSource, size_type numElements) { copyAssignRangeDisjoint(pDestination, pSource, numElements); } static inline void moveConstructRange(T *pDestination, T *pSource, size_type numElements) { copyAssignRangeDisjoint(pDestination, pSource, numElements); } static inline void defaultConstructFill(T */*pDestination*/, size_type /*numElements*/) { /* DOES NOTHING */ } static inline void destroy(T *pObject) { /* DOES NOTHING */ } static inline void destroyRange(T */*objects*/, size_type /*numObjects*/) { /* DOES NOTHING */ } static inline void rotate(T *first, T *middle, T *last) { const T tmp(*middle); memmove(first + 1, first, (last - first) * sizeof(T)); *first = tmp; } static inline bool useRealloc() { return true; } }; typedef OdVector> OdRasterData; class OdExGiRasterImage; /** \details This template class is a specialization of the OdSmartPtr class for OdExGiRasterImage object pointers. */ typedef OdSmartPtr OdExGiRasterImagePtr; /** \details This class represents a raster image within the ODA vectorization framework. Library: Source code provided. */ class OdExGiRasterImage : public OdGiRasterImageParam { public: OdRxObjectPtr clone() const override { OdSmartPtr pRes = OdRxObjectImpl::createObject(); pRes->m_bits = m_bits; pRes->m_palette = m_palette; pRes->m_header = m_header; pRes->m_imageSource = m_imageSource; pRes->m_sourceFileName = m_sourceFileName; pRes->m_transparencyMode = m_transparencyMode; pRes->m_transparentColor = m_transparentColor; 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() + 3) / 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; OdRasterData m_bits; Palette m_palette; ImageSource m_imageSource; OdString m_sourceFileName; TransparencyMode m_transparencyMode; int m_transparentColor; OdRxModulePtr m_pModule; public: /** \param bitsPerPixel [in] Color depth. \param xPixels [in] X pixels. \param yPixels [in] Y pixels. */ OdExGiRasterImage(OdUInt32 xPixels, OdUInt32 yPixels, OdUInt8 bitsPerPixel); OdExGiRasterImage(); void setModule(const OdRxModulePtr& pModule); /** \details Returns the size in bytes of the pixel data for a specified RasterImage object. \param bitsPerPixel [in] Color depth. \param xPixels [in] X pixels. \param yPixels [in] Y pixels. */ static size_t bitDataSize(OdUInt32 xPixels, OdUInt32 yPixels, OdUInt16 bitsPerPixel) { return size_t(((xPixels * bitsPerPixel +31) & ~31) /8) * yPixels; } /** \details Returns the size in bytes of the pixel data for this RasterImage object. \param bitsPerPixel [in] Color depth. \param xPixels [in] X pixels. \param yPixels [in] Y pixels. */ size_t 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. */ OdRasterData & bits() { return m_bits; } /** \details Returns a the binary data for this RasterImage object. */ const OdRasterData & 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(size_t& 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 OdRasterData& data) { ODA_ASSERT(data.size() <= bitDataSize()); m_bits = data; } void setBits(const OdUInt8* data, size_t 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 transparent color index or -1 if no transparent colors available in this raster image. \param nTransparentColor [in] Transparent color index. */ void setTransparentColor(int nTransparentColor) { m_transparentColor = nTransparentColor; } /** \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 override { 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 override { 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) override; OdUInt32 pixelWidth() const override; OdUInt32 pixelHeight() const override; OdUInt32 colorDepth() const override; OdGiRasterImage::Units defaultResolution(double& xPelsPerUnit, double& yPelsPerUnit) const override; /** Returns bitmap info header */ // void getBitmapInfoHeader(BITMAPINFOHEADER& bmih) const; OdUInt32 numColors() const override; ODCOLORREF color(OdUInt32 colorIndex) const override; int transparentColor() const override; void scanLines(OdUInt8* scnLines, OdUInt32 firstScanline, OdUInt32 numLines = 1) const override; /** \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. */ const OdUInt8* scanLines() const override; PixelFormatInfo pixelFormat() const override; OdUInt32 scanLinesAlignment() const override; OdUInt32 supportedParams() const override; ImageSource imageSource() const override; void setImageSource(ImageSource source) override; const OdString &sourceFileName() const override; void setSourceFileName(const OdString &fileName) override; TransparencyMode transparencyMode() const override; void setTransparencyMode(TransparencyMode mode) override; }; /** \details This class implements platform-dependent loading and saving of Raster Image files. 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 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) override; /** \details Loads the specified Raster Image file. \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(OdStreamBuf *pStreamBuf, const OdUInt32 *pFlagsChain = NULL) override; /** \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 pFlagsChain [in] Optional zero-terminated saving flag pairs array. */ bool saveRasterImage(const OdGiRasterImage* rasterImage, const OdString& filename, const OdUInt32 *pFlagsChain = NULL) override; /** \details Saves specified Raster Image to the specified file. \param rasterImage [in] Raster image to be saved. \param pStream [out] A pointer to an output stream buffer. \param type [in] Image format type to be written. See OdRxRasterServices::ImageType. \param pFlagsChain [in] Optional zero-terminated saving flag pairs array. */ bool saveRasterImage(const OdGiRasterImage* rasterImage, OdStreamBuf* pStream, OdUInt32 type, const OdUInt32* pFlagsChain = NULL) override; /** \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. See OdRxRasterServices::ImageType. \param pFlagsChain [in] Optional zero-terminated saving flag pairs array. */ bool saveRasterImage(const OdGiRasterImage* rasterImage, const OdString& filename, OdUInt32 type, const OdUInt32 *pFlagsChain = NULL) override; /** \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. See OdRxRasterServices::ImageType. \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) override; /** \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. See OdRxRasterServices::ImageType. \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) override; /** \details Returns array of supported image format types. */ OdUInt32Array getRasterImageTypes() const override; /** \details Get file extension and filter name by type. \param type [in] Image format type to be formatted. See OdRxRasterServices::ImageType. \param psFilterName [out] Output filter name (can be Null). */ OdString mapTypeToExtension(OdUInt32 type, OdString* psFilterName) const override; /** \details Get image format type by file extension. \param extension [in] File extension. */ OdUInt32 mapExtensionToType(const OdString& extension) const override; /** \details Try to detect image format type from input stream. \param filename [in] Filename of the Raster Image file to be checked. */ OdUInt32 getImageFormat(const OdString &filename) const override; /** \details Try to detect image format type from input stream. \param pStreamBuf [in] Pointer to the StreamBuf object from which the data are to be checked. */ OdUInt32 getImageFormat(OdStreamBuf* pStreamBuf) const override; /** \details Initialize module. */ void initApp() override; /** \details Uninitialize module. */ void uninitApp() override; }; #include "TD_PackPop.h" #endif //#ifndef _OD_ODEXGIRASTERIMAGE_H_