1 /******************************************************************************
2  *
3  * Project:  OpenCPN
4  * Purpose:  ChartBaseBSB and Friends
5  * Author:   David Register
6  *
7  ***************************************************************************
8  *   Copyright (C) 2010 by David S. Register   *
9  *                                                                         *
10  *   This program is free software; you can redistribute it and/or modify  *
11  *   it under the terms of the GNU General Public License as published by  *
12  *   the Free Software Foundation; either version 2 of the License, or     *
13  *   (at your option) any later version.                                   *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program; if not, write to the                         *
22  *   Free Software Foundation, Inc.,                                       *
23  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.             *
24  ***************************************************************************
25  *
26  *
27  */
28 
29 
30 #ifndef _CHARTIMG_H_
31 #define _CHARTIMG_H_
32 
33 
34 #include "chartbase.h"
35 #include "georef.h"                 // for GeoRef type
36 #include "OCPNRegion.h"
37 #include "viewport.h"
38 
39 typedef enum ScaleTypeEnum
40 {
41       RENDER_LODEF = 0,
42       RENDER_HIDEF,
43 }_ScaleTypeEnum;
44 
45 
46 
47 class WXDLLEXPORT ChartImg;
48 
49 //-----------------------------------------------------------------------------
50 //    Constants, etc.
51 //-----------------------------------------------------------------------------
52 
53 typedef enum PaletteDir
54 {
55       PaletteFwd,
56       PaletteRev
57 }_PaletteDir;
58 
59 
60 typedef enum BSB_Color_Capability
61 {
62     COLOR_RGB_DEFAULT = 0,                   // Default corresponds to bsb entries "RGB"
63     DAY,
64     DUSK,
65     NIGHT,
66     NIGHTRED,
67     GRAY,
68     PRC,
69     PRG,
70     N_BSB_COLORS
71 }_BSB_Color_Capability;
72 
73 //-----------------------------------------------------------------------------
74 //    Fwd Refs
75 //-----------------------------------------------------------------------------
76 
77 class ChartKAP;
78 class ViewPort;
79 class PixelCache;
80 class ocpnBitmap;
81 
82 class wxFFileInputStream;
83 
84 //-----------------------------------------------------------------------------
85 //    Helper classes
86 //-----------------------------------------------------------------------------
87 
88 class Refpoint
89 {
90 public:
91       int         bXValid;
92       int         bYValid;
93       float       xr;
94       float       yr;
95       float       latr;
96       float       lonr;
97       float       xpl_error;
98       float       xlp_error;
99       float       ypl_error;
100       float       ylp_error;
101 
102 };
103 
104 
105 
106 struct TileOffsetCache
107 {
108     int offset; // offset from start of line pointer
109     int pixel; // offset from current pixel
110 };
111 
112 class CachedLine
113 {
114 public:
115       unsigned char    *pPix;
116       TileOffsetCache  *pTileOffset; // entries for random access
117 
118       bool              bValid;
119 };
120 
121 class opncpnPalette
122 {
123     public:
124         opncpnPalette();
125         ~opncpnPalette();
126 
127         int *FwdPalette;
128         int *RevPalette;
129         int nFwd;
130         int nRev;
131 };
132 
133 // ----------------------------------------------------------------------------
134 // ChartBaseBSB
135 // ----------------------------------------------------------------------------
136 
137 class  ChartBaseBSB     :public ChartBase
138 {
139     public:
140       //    Public methods
141 
142       ChartBaseBSB();
143       virtual ~ChartBaseBSB();
144       void FreeLineCacheRows(int start=0, int end=-1);
145       bool HaveLineCacheRow(int row);
146 
147       //    Accessors
148       virtual ThumbData *GetThumbData(int tnx, int tny, float lat, float lon);
GetThumbData()149       virtual ThumbData *GetThumbData() {return pThumbData;}
150       virtual bool UpdateThumbData(double lat, double lon);
151 
GetNativeScale()152       int GetNativeScale(){return m_Chart_Scale;}
153       double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
154       double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
155 
156       virtual InitReturn Init( const wxString& name, ChartInitFlag init_flags );
157 
158       virtual int latlong_to_pix_vp(double lat, double lon, double &pixx, double &pixy, ViewPort& vp);
159       virtual int vp_pix_to_latlong(ViewPort& vp, double pixx, double pixy, double *lat, double *lon);
160 
161       bool RenderRegionViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint, const OCPNRegion &Region);
162 
163       virtual bool RenderRegionViewOnGL(const wxGLContext &glc, const ViewPort& VPoint,
164                                         const OCPNRegion &RectRegion, const LLRegion &Region);
165 
166       virtual bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
167       virtual double GetNearestPreferredScalePPM(double target_scale_ppm);
168 
169       virtual void GetValidCanvasRegion(const ViewPort& VPoint, OCPNRegion *pValidRegion);
170       virtual LLRegion GetValidRegion();
171 
172       virtual bool GetChartExtent(Extent *pext);
173 
174       void SetColorScheme(ColorScheme cs, bool bApplyImmediate);
175 
176       wxImage *GetImage();
177 
178       void SetVPRasterParms(const ViewPort &vpt);
179 
180       virtual void ComputeSourceRectangle(const ViewPort &vp, wxRect *pSourceRect);
181       virtual double GetRasterScaleFactor(const ViewPort &vp);
182       virtual bool GetChartBits( wxRect& source, unsigned char *pPix, int sub_samp );
GetSize_X()183       virtual int GetSize_X(){ return Size_X;}
GetSize_Y()184       virtual int GetSize_Y(){ return Size_Y;}
185 
186       virtual void latlong_to_chartpix(double lat, double lon, double &pixx, double &pixy);
187       virtual void chartpix_to_latlong(double pixx, double pixy, double *plat, double *plon);
188 
GetPPM()189       double GetPPM(){ return m_ppm_avg;}
190 
191 protected:
192 //    Methods
193 
GetSourceRect()194       wxRect GetSourceRect(){ return Rsrc; }
195 
196       virtual bool GetAndScaleData(unsigned char *ppn, size_t data_size,
197                                    wxRect& source, int source_stride, wxRect& dest, int dest_stride,
198                                    double scale_factor, ScaleTypeEnum scale_type);
199       bool RenderViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint);
200 
IsCacheValid()201       bool IsCacheValid(){ return cached_image_ok; }
InvalidateCache()202       void InvalidateCache(){cached_image_ok = 0;}
203       bool IsRenderCacheable( wxRect& source, wxRect& dest );
204 
205       void CreatePaletteEntry(char *buffer, int palette_index);
206       PaletteDir GetPaletteDir(void);
207       int  *GetPalettePtr(BSB_Color_Capability);
208 
209       double GetClosestValidNaturalScalePPM(double target_scale, double scale_factor_min, double scale_factor_max);
210 
211       virtual void InvalidateLineCache();
212       virtual bool CreateLineIndex(void);
213 
214 
215       virtual wxBitmap *CreateThumbnail(int tnx, int tny, ColorScheme cs);
216       virtual int BSBGetScanline( unsigned char *pLineBuf, int y, int xs, int xl, int sub_samp);
217 
218 
219       bool GetViewUsingCache( wxRect& source, wxRect& dest, const OCPNRegion& Region, ScaleTypeEnum scale_type );
220       bool GetView( wxRect& source, wxRect& dest, ScaleTypeEnum scale_type );
221 
222 
223       virtual int BSBScanScanline(wxInputStream *pinStream);
224       virtual int ReadBSBHdrLine( wxInputStream*, char *, int );
225       virtual int AnalyzeRefpoints(bool b_testSolution = true);
226       virtual bool AnalyzeSkew(void);
227 
228       virtual bool SetMinMax(void);
229 
230       InitReturn PreInit( const wxString& name, ChartInitFlag init_flags, ColorScheme cs );
231       InitReturn PostInit(void);
232 
233       double AdjustLongitude(double lon);
234 
235 
236 //    Protected Data
237       PixelCache        *pPixCache;
238 
239       int         Size_X;                 // Chart native pixel dimensions
240       int         Size_Y;
241       int         m_Chart_DU;
242       double      m_cph;
243       double      m_proj_parameter;                     // Mercator:               Projection Latitude
244                                                       // Transverse Mercator:    Central Meridian
245       double      m_dx;                                 // Pixel scale factors, from KAP header
246       double      m_dy;
247 
248       wxString    m_bsb_ver;
249       bool        m_b_SHOM;
250       bool        m_b_apply_dtm;
251 
252       int         m_datum_index;
253       double      m_dtm_lat;
254       double      m_dtm_lon;
255 
256 
257       wxRect      cache_rect;
258       wxRect      cache_rect_scaled;
259       bool        cached_image_ok;
260       ScaleTypeEnum cache_scale_method;
261       double      m_cached_scale_ppm;
262       wxRect      m_last_vprect;
263 
264 
265       wxRect      Rsrc;                   // Current chart source rectangle
266       double      m_raster_scale_factor;
267 
268       int         nRefpoint;
269       Refpoint    *pRefTable;
270 
271 
272       int         nColorSize;
273       int         *pline_table;           // pointer to Line offset table
274 
275       CachedLine  *pLineCache;
276 
277       wxInputStream    *ifs_hdr;
278       wxInputStream    *ifss_bitmap;
279       wxBufferedInputStream *ifs_bitmap;
280 
281       wxString          *pBitmapFilePath;
282 
283       unsigned char     *ifs_buf;
284       unsigned char     *ifs_bufend;
285       int               ifs_bufsize;
286       unsigned char     *ifs_lp;
287       int               ifs_file_offset;
288       int               nFileOffsetDataStart;
289       int               m_nLineOffset;
290 
291       GeoRef            cPoints;
292 
293       double            wpx[12], wpy[12], pwx[12], pwy[12];     // Embedded georef coefficients
294       int               wpx_type, wpy_type, pwx_type, pwy_type;
295       int               n_wpx, n_wpy, n_pwx, n_pwy;
296       bool              bHaveEmbeddedGeoref;
297 
298       opncpnPalette     *pPalettes[N_BSB_COLORS];
299 
300       BSB_Color_Capability m_mapped_color_index;
301 
302 //    Integer digital scale value above which bilinear scaling is not allowed,
303 //      and subsampled scaling must be performed
304       int         m_bilinear_limit;
305 
306 
307       bool        bUseLineCache;
308 
309       float       m_LonMax;
310       float       m_LonMin;
311       float       m_LatMax;
312       float       m_LatMin;
313 
314       int         *pPalette;
315       PaletteDir  palette_direction;
316 
317       bool        bGeoErrorSent;
318 
319       double      m_ppm_avg;              // Calculated true scale factor of the 1X chart,
320                                         // pixels per meter
321       OCPNRegion  m_last_region;
322 
323       int       m_b_cdebug;
324 
325       double    m_proj_lat, m_proj_lon;
326 
327       ViewPort  m_vp_render_last;
328 
329       wxCriticalSection m_critSect;
330       wxULongLong m_filesize;
331 
332 
333 };
334 
335 
336 // ----------------------------------------------------------------------------
337 // ChartKAP
338 // ----------------------------------------------------------------------------
339 
340 class  ChartKAP : public ChartBaseBSB
341 {
342 public:
343 //    Methods
344       ChartKAP();
345       ~ChartKAP();
346 
347       InitReturn Init( const wxString& name, ChartInitFlag init_flags );
348 
349 
350 };
351 
352 // ----------------------------------------------------------------------------
353 // ChartGEO
354 // ----------------------------------------------------------------------------
355 
356 class  ChartGEO : public ChartBaseBSB
357 {
358 public:
359 //    Methods
360       ChartGEO();
361       ~ChartGEO();
362 
363       InitReturn Init( const wxString& name, ChartInitFlag init_flags );
364 
365 
366 };
367 
368 // ----------------------------------------------------------------------------
369 // ChartPlugInWrapper
370 //    This class is a wrapper/interface to PlugIn charts(PlugInChartBase) as defined in ocpn_plugin.h
371 // ----------------------------------------------------------------------------
372 
373 class PlugInChartBase;                  // found in ocpn_plugin.h
374 
375 class ChartPlugInWrapper : public ChartBaseBSB
376 {
377       public:
378             ChartPlugInWrapper();
379             ChartPlugInWrapper(const wxString &chart_class);
380             virtual ~ChartPlugInWrapper();
381 
382             virtual wxString GetFileSearchMask(void);
383 
384             virtual InitReturn Init( const wxString& name, ChartInitFlag init_flags );
385 
386 //    Accessors
387             virtual ThumbData *GetThumbData(int tnx, int tny, float lat, float lon);
388             virtual ThumbData *GetThumbData();
389             virtual bool UpdateThumbData(double lat, double lon);
390 
391             double GetNormalScaleMin(double canvas_scale_factor, bool b_allow_overzoom);
392             double GetNormalScaleMax(double canvas_scale_factor, int canvas_width);
393 
394             virtual bool GetChartExtent(Extent *pext);
395 
396             virtual bool RenderRegionViewOnDC(wxMemoryDC& dc, const ViewPort& VPoint,
397                                               const OCPNRegion &Region);
398 
399             virtual bool RenderRegionViewOnGL(const wxGLContext &glc, const ViewPort& VPoint,
400                                               const OCPNRegion &RectRegion, const LLRegion &Region);
401 
402             virtual bool AdjustVP(ViewPort &vp_last, ViewPort &vp_proposed);
403 
404             virtual void GetValidCanvasRegion(const ViewPort& VPoint, OCPNRegion *pValidRegion);
405 
406             virtual void SetColorScheme(ColorScheme cs, bool bApplyImmediate);
407 
408             virtual double GetNearestPreferredScalePPM(double target_scale_ppm);
409 
GetPlugInChart(void)410             virtual PlugInChartBase *GetPlugInChart(void){ return m_ppicb; }
411 
412             virtual int GetCOVREntries();
413             virtual int GetCOVRTablePoints(int iTable);
414             virtual int GetCOVRTablenPoints(int iTable);
415             virtual float *GetCOVRTableHead(int iTable);
416 
417             virtual int GetNoCOVREntries();
418             virtual int GetNoCOVRTablePoints(int iTable);
419             virtual int  GetNoCOVRTablenPoints(int iTable);
420             virtual float *GetNoCOVRTableHead(int iTable);
421 
422             //    The following set of methods apply to BSB (i.e. Raster) type PlugIn charts only
423             //    and need not be implemented if the ChartFamily is not CHART_FAMILY_RASTER
424             virtual void ComputeSourceRectangle(const ViewPort &vp, wxRect *pSourceRect);
425             virtual double GetRasterScaleFactor(const ViewPort &vp);
426             virtual bool GetChartBits( wxRect& source, unsigned char *pPix, int sub_samp );
427             virtual int GetSize_X();
428             virtual int GetSize_Y();
429             virtual void latlong_to_chartpix(double lat, double lon, double &pixx, double &pixy);
430             virtual void chartpix_to_latlong(double pixx, double pixy, double *plat, double *plon);
431 
432 
433             //  Added for API V 1.14, with PlugInChartBaseExtended
434             virtual bool RenderRegionViewOnDCNoText(wxMemoryDC &dc, const ViewPort& VPoint,
435                                                                 const OCPNRegion &Region);
436 
437             virtual bool RenderRegionViewOnDCTextOnly(wxMemoryDC &dc, const ViewPort& VPoint,
438                                                     const OCPNRegion &Region);
439 
440             virtual bool RenderRegionViewOnGLNoText( const wxGLContext &glc, const ViewPort& VPoint,
441                                                        const OCPNRegion &RectRegion, const LLRegion &Region );
442 
443             virtual bool RenderRegionViewOnGLTextOnly( const wxGLContext &glc, const ViewPort& VPoint,
444                                                          const OCPNRegion &RectRegion );
445 
446             virtual void ClearPLIBTextList();
447 
448       private:
449             PlugInChartBase *m_ppicb;
450             wxObject          *m_ppo;
451             wxCriticalSection m_critSect;
452             bool              m_overlayENC;
453             wxMask           *m_pMask;
454 };
455 
456 #endif
457