1 /** \file lvimg.h 2 \brief Image formats support 3 4 CoolReader Engine C-compatible API 5 6 (c) Vadim Lopatin, 2000-2006 7 This source code is distributed under the terms of 8 GNU General Public License. 9 10 See LICENSE file for details. 11 12 */ 13 14 #ifndef __LVIMG_H_INCLUDED__ 15 #define __LVIMG_H_INCLUDED__ 16 17 18 #include "lvref.h" 19 #include "lvstream.h" 20 21 class CacheableObject; 22 class CacheObjectListener { 23 public: onCachedObjectDeleted(lUInt32 objectId)24 virtual void onCachedObjectDeleted(lUInt32 objectId) { CR_UNUSED(objectId); } ~CacheObjectListener()25 virtual ~CacheObjectListener() {} 26 }; 27 28 /// object deletion listener callback function type 29 typedef void(*onObjectDestroyedCallback_t)(CacheObjectListener * pcache, lUInt32 pobject); 30 31 /// to handle object deletion listener 32 class CacheableObject { 33 onObjectDestroyedCallback_t _callback; 34 CacheObjectListener * _cache; 35 lUInt32 _objectId; 36 public: 37 CacheableObject(); ~CacheableObject()38 virtual ~CacheableObject() { 39 if (_callback) 40 _callback(_cache, _objectId); 41 } getObjectId()42 virtual lUInt32 getObjectId() { return _objectId; } 43 /// set callback to call on object destroy setOnObjectDestroyedCallback(onObjectDestroyedCallback_t callback,CacheObjectListener * pcache)44 void setOnObjectDestroyedCallback(onObjectDestroyedCallback_t callback, CacheObjectListener * pcache) { 45 _callback = callback; 46 _cache = pcache; 47 } 48 }; 49 50 class LVImageSource; 51 struct ldomNode; 52 class LVColorDrawBuf; 53 54 /// image decoding callback interface 55 class LVImageDecoderCallback 56 { 57 public: 58 virtual ~LVImageDecoderCallback(); 59 virtual void OnStartDecode( LVImageSource * obj ) = 0; 60 virtual bool OnLineDecoded( LVImageSource * obj, int y, lUInt32 * data ) = 0; 61 virtual void OnEndDecode( LVImageSource * obj, bool errors ) = 0; 62 }; 63 64 struct CR9PatchInfo { 65 lvRect frame; 66 lvRect padding; 67 /// caclulate dst and src rectangles (src rect includes 1 pixel layout frame) 68 void calcRectangles(const lvRect & dst, const lvRect & src, lvRect dstitems[9], lvRect srcitems[9]) const; 69 /// for each side, apply max(padding.C, dstPadding.C) to dstPadding 70 void applyPadding(lvRect & dstPadding) const; 71 }; 72 73 74 class LVImageSource : public CacheableObject 75 { 76 CR9PatchInfo * _ninePatch; 77 public: GetNinePatchInfo()78 virtual const CR9PatchInfo * GetNinePatchInfo() { return _ninePatch; } 79 virtual CR9PatchInfo * DetectNinePatch(); 80 virtual ldomNode * GetSourceNode() = 0; 81 virtual LVStream * GetSourceStream() = 0; 82 virtual void Compact() = 0; 83 virtual int GetWidth() = 0; 84 virtual int GetHeight() = 0; 85 virtual bool Decode( LVImageDecoderCallback * callback ) = 0; LVImageSource()86 LVImageSource() : _ninePatch(NULL) {} 87 virtual ~LVImageSource(); 88 }; 89 90 typedef LVRef< LVImageSource > LVImageSourceRef; 91 92 /// type of image transform 93 enum ImageTransform { 94 IMG_TRANSFORM_NONE, // just draw w/o any resizing/tiling 95 IMG_TRANSFORM_SPLIT, // split at specified pixel, fill extra middle space with value of this pixel 96 IMG_TRANSFORM_STRETCH, // stretch image proportionally to fill whole area 97 IMG_TRANSFORM_TILE // tile image 98 }; 99 100 101 102 /// creates image which stretches source image by filling center with pixels at splitX, splitY 103 LVImageSourceRef LVCreateStretchFilledTransform( LVImageSourceRef src, int newWidth, int newHeight, ImageTransform hTransform=IMG_TRANSFORM_SPLIT, ImageTransform vTransform=IMG_TRANSFORM_SPLIT, int splitX=-1, int splitY=-1 ); 104 /// creates image which fills area with tiled copy 105 LVImageSourceRef LVCreateTileTransform( LVImageSourceRef src, int newWidth, int newHeight, int offsetX, int offsetY ); 106 /// creates XPM image 107 LVImageSourceRef LVCreateXPMImageSource( const char * data[] ); 108 LVImageSourceRef LVCreateNodeImageSource( ldomNode * node ); 109 LVImageSourceRef LVCreateDummyImageSource( ldomNode * node, int width, int height ); 110 /// creates image source from stream 111 LVImageSourceRef LVCreateStreamImageSource( LVStreamRef stream ); 112 /// creates image source as memory copy of file contents 113 LVImageSourceRef LVCreateFileCopyImageSource( lString32 fname ); 114 /// creates image source as memory copy of stream contents 115 LVImageSourceRef LVCreateStreamCopyImageSource( LVStreamRef stream ); 116 /// creates decoded memory copy of image, if it's unpacked size is less than maxSize (8 bit gray or 32 bit color) 117 LVImageSourceRef LVCreateUnpackedImageSource( LVImageSourceRef srcImage, int maxSize = MAX_SKIN_IMAGE_CACHE_ITEM_UNPACKED_SIZE, bool gray=false ); 118 /// creates decoded memory copy of image, if it's unpacked size is less than maxSize; bpp: 8,16,32 supported 119 LVImageSourceRef LVCreateUnpackedImageSource( LVImageSourceRef srcImage, int maxSize, int bpp ); 120 /// creates image source based on draw buffer 121 LVImageSourceRef LVCreateDrawBufImageSource( LVColorDrawBuf * buf, bool own ); 122 123 #define COLOR_TRANSFORM_BRIGHTNESS_NONE 0x808080 124 #define COLOR_TRANSFORM_CONTRAST_NONE 0x404040 125 126 /// creates image source which transforms colors of another image source (add RGB components added first, then multiplyed by multiplyRGB fixed point components (0x20 is 1.0f) 127 LVImageSourceRef LVCreateColorTransformImageSource(LVImageSourceRef srcImage, lUInt32 addRGB, lUInt32 multiplyRGB); 128 /// creates image source which applies alpha to another image source (0 is no change, 255 is totally transparent) 129 LVImageSourceRef LVCreateAlphaTransformImageSource(LVImageSourceRef srcImage, int alpha); 130 131 132 class LVFont; 133 class LVDrawBuf; 134 135 /// draws battery icon in specified rectangle of draw buffer; if font is specified, draws charge % 136 // first icon is for charging, the rest - indicate progress icon[1] is lowest level, icon[n-1] is full power 137 // if no icons provided, battery will be drawn 138 void LVDrawBatteryIcon( LVDrawBuf * drawbuf, const lvRect & batteryRc, int percent, bool charging, LVRefVec<LVImageSource> icons, LVFont * font ); 139 140 unsigned char * convertSVGtoPNG(unsigned char *svg_data, int svg_data_size, float zoom_factor, int *png_data_len); 141 142 #define IMAGE_SOURCE_FROM_BYTES( imgvar , bufvar ) \ 143 extern unsigned char bufvar []; \ 144 extern int bufvar ## _size ; \ 145 LVImageSourceRef imgvar = LVCreateStreamImageSource( \ 146 LVCreateMemoryStream( bufvar , bufvar ## _size ) ) 147 148 149 #endif 150