1 #ifndef __Geom_Draw_hrr__
2 #define __Geom_Draw_hrr__
3 
4 namespace Upp {
5 
6 //#include "Stream.h"
7 
8 class ImageWriter : public RasterEncoder {
9 public:
ImageWriter()10 	ImageWriter() : output(NULL) {}
11 	ImageWriter(ImageBuffer& output, bool merge = true)                       { Open(output, merge); }
12 	ImageWriter(ImageBuffer& output, Point pos, bool merge = true)            { Open(output, pos, merge); }
13 	ImageWriter(ImageBuffer& output, Point pos, Rect clip, bool merge = true) { Open(output, pos, clip, merge); }
14 
15 	void         Open(ImageBuffer& output, bool merge = true)                 { Open(output, Point(0, 0), merge); }
16 	void         Open(ImageBuffer& output, Point pos, bool merge = true)      { Open(output, pos, Rect(output.GetSize()), merge); }
17 	void         Open(ImageBuffer& output, Point pos, Rect clip, bool merge = true);
18 
19 	virtual void Start(Size sz);
20 	virtual void WriteLineRaw(const byte *data);
21 
22 private:
23 	ImageBuffer  *output;
24 	Point        pos;
25 	int          left, width, offset;
26 	Rect         clip;
27 	int          line;
28 	Size         src_size;
29 	bool         merge;
30 };
31 
32 class ImageBufferRaster : public Raster {
33 public:
34 	ImageBufferRaster(const ImageBuffer& buffer);
35 	ImageBufferRaster(const ImageBuffer& buffer, const Rect& crop);
36 
37 	virtual Size       GetSize();
38 	virtual Info       GetInfo();
39 	virtual Line       GetLine(int line);
40 
41 private:
42 	const ImageBuffer& buffer;
43 	Rect               crop;
44 };
45 
46 class HRRInfo
47 {
48 	friend class HRR;
49 
50 public:
51 	HRRInfo();
52 	HRRInfo(const Rectf& log_rect, const Rectf& map_rect = Null,
53 		int levels = 5, Color background = White,
54 		int method = METHOD_JPG, int quality = DFLT_JPG_QUALITY,
55 		bool mono = false, Color mono_black = Black, Color mono_white = White);
56 
57 	void               Serialize(Stream& stream);
58 
IsEmpty()59 	bool               IsEmpty() const       { return levels == 0; }
60 
GetLevels()61 	int                GetLevels() const     { return levels; }
GetMaxSize()62 	int                GetMaxSize() const    { return levels ? UNIT << (levels - 1) : 0; }
GetLogRect()63 	Rectf              GetLogRect() const    { return log_rect; }
GetMapRect()64 	Rectf              GetMapRect() const    { return map_rect; }
GetBackground()65 	Color              GetBackground() const { return background; }
IsMono()66 	bool               IsMono() const        { return mono; }
GetMonoBlack()67 	Color              GetMonoBlack() const  { return mono_black; }
GetMonoWhite()68 	Color              GetMonoWhite() const  { return mono_white; }
GetMethod()69 	int                GetMethod() const     { return method; }
GetQuality()70 	int                GetQuality() const    { return quality; }
71 
72 	One<StreamRaster>  GetDecoder() const;
73 	One<StreamRasterEncoder> GetEncoder() const;
74 
75 	static double      GetEstimatedFileSize(int _levels, int method, int quality);
76 
Pack(int method,int quality)77 	static int         Pack(int method, int quality) { return (method << 16) | (quality & 0xFFFF); }
Method(int p)78 	static int         Method(int p)         { return p >> 16; }
Quality(int p)79 	static int         Quality(int p)        { return p & 0xFFFF; }
80 
81 	static Vector<int> EnumMethods();
82 	static Vector<int> EnumQualities(int method);
83 	static String      GetName(int method, int quality);
84 	static VectorMap<int, String> GetPackMap();
85 
86 public:
87 	enum
88 	{
89 		HALF_BITS = 8,
90 		HALF      = 1 << HALF_BITS,
91 		UNIT      = HALF << 1,
92 		STDLEVEL  = 6,
93 		MAXLEVELS = 12,
94 		LCOUNT    = 2,
95 		UCOUNT    = 1 << LCOUNT,
96 	};
97 
98 	enum
99 	{
100 		METHOD_JPG,
101 		METHOD_GIF,
102 		METHOD_RLE,
103 		METHOD_PNG,
104 		METHOD_ZIM,
105 		METHOD_BZM,
106 
107 		DFLT_JPG_QUALITY = 50,
108 	};
109 
110 protected:
111 	Color      background;
112 	Color      mono_black;
113 	Color      mono_white;
114 	Rectf      log_rect;
115 	Rectf      map_rect; // logical rectangle inflated to tightly bounding square
116 	int        levels;
117 	int        method;
118 	int        quality;
119 	bool       mono;
120 };
121 
122 class HRR
123 {
124 public:
125 	struct Block
126 	{
BlockBlock127 		Block(const HRR& hrr) : hrr(hrr) {}
128 
129 		void        Init(Size size, RGBA color);
130 
131 		ImageBuffer block;
132 		Size        size;
133 		Rect        area;
134 		Rectf       log_area;
135 		int         level;
136 		const HRR&  hrr;
137 	};
138 
139 	struct Cursor {
140 		Cursor(HRR& owner, const Rectf& extent, double measure, int alpha = 100,
141 			Color mono_black = Null, Color mono_white = Null, Color blend_bgnd = Null);
142 
143 		bool Fetch(Rectf& part_rc);
144 		Image Get();
145 
146 		HRR& owner;
147 		Rectf extent;
148 		double measure;
149 		Color mono_black;
150 		Color mono_white;
151 		int alpha;
152 		Color blend_bgnd;
153 
154 		One<StreamRaster> raster;
155 		int level;
156 		int total;
157 		Rect rc;
158 		Point block;
159 		int cimg;
160 	};
161 
162 	friend struct Cursor;
163 
164 	enum
165 	{
166 		DEFAULT_CACHE_SIZEOF_LIMIT = 20000000,
167 	};
168 
169 	typedef Gate1<Block&>      Writeback;
170 
171 	HRR();
172 	HRR(const char *path, bool read_only = true);
173 
174 	bool                       Open(const char *path, bool read_only = true);
175 	void                       Close();
176 
177 	bool                       Create(const HRRInfo& _info, const char *path);
178 	void                       Write(Writeback drawback, bool downscale = true);
179 	Rectf                      GetLogBlockRect(int level, const Rect& rc) const;
180 	Matrixf                    GetPixMapMatrix(int level, int x, int y) const;
181 	int64                      GetFileWriteSize() const;
182 
SetCacheSizeLimit(int cl)183 	void                       SetCacheSizeLimit(int cl)       { FlushCache(cache_sizeof_limit = cl); }
GetCacheSizeLimit()184 	int                        GetCacheSizeLimit() const       { return cache_sizeof_limit; }
GetCacheSize()185 	int                        GetCacheSize() const            { return cache_sizeof + directory_sizeof; }
186 	void                       ClearCache();
187 
GetMap(String key)188 	String                     GetMap(String key) const        { return map.Get(key, Null); }
189 	void                       SetMap(String key, String value);
ClearMap(String key)190 	void                       ClearMap(String key)            { SetMap(key, Null); }
ClearMap()191 	void                       ClearMap()                      { map.Clear(); }
GetMap()192 	const VectorMap<String, String>& GetMap() const            { return map; }
193 	void                       FlushMap();
194 
IsOpen()195 	bool                       IsOpen() const                  { return stream.IsOpen(); }
IsError()196 	bool                       IsError() const                 { return stream.IsError(); }
197 	int                        SizeOfInstance() const;
198 
199 	void                       Paint(Draw& draw, Rect dest, Rectf src,
200 		int wash = 100, int max_pixel = 1, Color mono_black = Null, Color mono_white = Null, Color blend_bgnd = Null);
201 
202 	void                       Paint(Draw& draw, const Matrixf& trg_pix, GisTransform transform,
203 		int wash = 100, int max_pixel = 1, Color mono_black = Null, Color mono_white = Null, Color blend_bgnd = Null);
204 
205 	static int                 GetProgressCount(int levels, bool downscale = true);
206 
GetInfo()207 	const HRRInfo&             GetInfo() const                 { return info; }
208 
209 	static One<StreamRasterEncoder> (*CreateEncoder)(const HRRInfo& info);
210 	static One<StreamRaster>        (*CreateDecoder)(const HRRInfo& info);
211 	static One<StreamRasterEncoder> StdCreateEncoder(const HRRInfo& info);
212 	static One<StreamRaster>        StdCreateDecoder(const HRRInfo& info);
213 
214 private:
215 	bool                       Write(Writeback drawback, bool downscale, int level, int px, int py,
216 		StreamRasterEncoder& format, Block *put);
217 	void                       Serialize();
218 	void                       FlushCache(int limit);
219 
220 private:
221 	HRRInfo                    info;
222 	FileStream                 stream;
223 	int                        version;
224 	Vector<int64>              pixel_directory_offset;
225 	Vector<int64>              mask_directory_offset;
226 //	Vector< Vector<int> >      pixel_directory;
227 //	Vector< Vector<int> >      mask_directory;
228 	int                        directory_sizeof;
229 	ArrayMap<Point, Image>     image_cache;
230 	VectorMap<int, Size>       size_cache;
231 	int                        cache_sizeof;
232 	int                        cache_sizeof_limit;
233 	VectorMap<String, String>  map;
234 	int                        map_offset;
235 };
236 
237 }
238 
239 #endif//__Geom_Draw_hrr__
240