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