Point Cloud Library (PCL)  1.14.1-dev
opennurbs_bitmap.h
1 /* $NoKeywords: $ */
2 /*
3 //
4 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
5 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
6 // McNeel & Associates.
7 //
8 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
9 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
10 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
11 //
12 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
13 //
14 ////////////////////////////////////////////////////////////////
15 */
16 
17 ////////////////////////////////////////////////////////////////
18 //
19 // Defines ON_WindowsBITMAPINFO class that is used to provide OS independent
20 // serialization of Windows device independent bitmaps (BITMAPINFO) used
21 // to store preview images.
22 //
23 ////////////////////////////////////////////////////////////////
24 
25 #if !defined(OPENNURBS_BITMAP_INC_)
26 #define OPENNURBS_BITMAP_INC_
27 
28 class ON_CLASS ON_Bitmap : public ON_Object
29 {
30  // virtual base class for bitmap objects
31  ON_OBJECT_DECLARE(ON_Bitmap);
32 public:
35 
36  // C++ defaults work fine
37  //ON_Bitmap(const ON_Bitmap&);
38  //ON_Bitmap& operator=(const ON_Bitmap&);
39 
40  // virtual
42 
43 
44  void Dump( ON_TextLog& ) const; // for debugging
45 
47  void Destroy();
48 
49  void Defaults();
50 
51  virtual
52  int Width() const = 0;
53  virtual
54  int Height() const = 0; // >0 means it's a bottom-up bitmap with origin at lower right
55  // <0 means it's a top-down bitmap with origin at upper left
56  virtual
57  int BitsPerPixel() const = 0; // bits per pixel
58  virtual
59  int SizeofScan() const = 0; // number of bytes per scan line
60  virtual
61  int SizeofImage() const = 0; // size of current map in bytes
62 
63  virtual
64  unsigned char* Bits(
65  int // index of scan line
66  ) = 0;
67  virtual
68  const unsigned char* Bits(
69  int // index of scan line
70  ) const = 0;
71 
74  ON_wString m_bitmap_name; // descriptive name
75  ON_wString m_bitmap_filename; // full path to file
76 };
77 
78 /*
79  ON_EmbeddedFile is derived from ON_Bitmap so it can be stored
80  in the Rhino document's CRhinoDoc::m_bitmap_table[] array.
81  The ON_EmbeddedFile class is used to embed any type of file
82  in a Rhino document.
83 */
84 class ON_CLASS ON_EmbeddedFile : public ON_Object
85 {
86  ON_OBJECT_DECLARE(ON_EmbeddedFile);
87 public:
91 
92  virtual ~ON_EmbeddedFile();
94  void Destroy();
95  void DestroyBuffer();
96 
97  /*
98  Description:
99  Store the specified file in an ON_EmbeddedFile class.
100  Parameters:
101  filename - [in]
102  full path to the file.
103  bCompress - [in]
104  true if the image of the file should be compressed.
105  (Pass false if the file is already in a compressed
106  format, like jpg, png, zip files.)
107  Returns:
108  true if successful. When true is returned m_id is set to
109  a new unique id, m_full_file_name is set to filename,
110  and m_relative_file_name is empty.
111  */
112  bool Create(
113  const wchar_t* filename,
114  bool bCompress
115  );
116 
117  /*
118  Description:
119  Store the specified file in an ON_EmbeddedFile class.
120  Parameters:
121  fp - [in]
122  Result of calling ON::OpenFile( ..., "rb" )
123  bCompress - [in]
124  true if the image of the file should be compressed.
125  (Pass false if the file is already in a compressed
126  format, like jpg, png, zip files.)
127  */
128  bool Create(
129  FILE* fp,
130  bool bCompress
131  );
132 
133  /*
134  Description:
135  Store the specified buffer in an ON_EmbeddedFile class.
136  Parameters:
137  source_buffer - [in]
138  source_buffer_size - [in]
139  number of bytes in source_buffer.
140  bCompress - [in]
141  true if the source_buffer should be compressed.
142  (Pass false if source_buffer is already in a compressed format.)
143  */
144  bool Create(
145  const void* source_buffer,
146  ON__UINT64 sizeof_source_buffer,
147  bool bCompress
148  );
149 
150  bool Extract(
151  const wchar_t* destination_filename
152  ) const;
153 
154  bool Extract(
155  FILE* fp
156  ) const;
157 
158  /*
159  Description:
160  Extracts the file into a buffer.
161  Parameters:
162  buffer - [out]
163  buffer must point to FileSize() bytes of memory.
164  The extracted file will be copied to this buffer.
165  Returns:
166  True if successful.
167  False if not successful.
168  */
169  bool Extract(
170  void* buffer
171  ) const;
172 
173  /*
174  Returns
175  full path file name
176  */
177  const wchar_t* FullFileName() const;
178 
179  /*
180  Returns
181  Relative file name. Usually relative to the directory
182  where the archive containing this embedded file was last
183  saved.
184  */
185  const wchar_t* RelativeFileName() const;
186 
187  ON_UUID Id() const;
188 
189  void SetId( ON_UUID id );
190 
191  void SetFullFileName( const wchar_t* full_file_name );
192 
193 
194  void SetRelativeFileName( const wchar_t* relative_file_name );
195 
196  ON__UINT64 FileSize() const;
197  ON__UINT64 FileLastModifiedTime() const;
198  ON__UINT32 FileCRC() const;
199 
200  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
201 
202  ON_BOOL32 Write( ON_BinaryArchive& ) const;
203  ON_BOOL32 Read( ON_BinaryArchive& );
204 
205  // The relative path is typically set when the .3dm file is
206  // saved and is the path to the file relative to the location
207  // of the saved file.
208  // (The full path to the file is in ON_Bitmap::m_bitmap_filename.)
210  ON_wString m_full_file_name; // full path file name
211  ON_wString m_relative_file_name; // relative path when the archive was last saved.
212 
213 private:
214  void* m_reserved;
215 
216 public:
217  ON__UINT64 m_file_size;
218  ON__UINT64 m_file_time; // last modified time returned by ON::GetFileStats()
219  ON__UINT32 m_file_crc; // 32 bit crc of the file from ON_CRC32
220 
221 public:
222  ON__UINT32 m_buffer_crc; // will be different from m_file_crc if the buffer is compressed.
224  unsigned char m_bCompressedBuffer; // true if m_buffer is compressed.
225 
226 private:
227  unsigned char m_reserved3[7];
228 };
229 
230 
231 #if !defined(ON_OS_WINDOWS_GDI)
232 
233 // These are the values of the Windows defines mentioned
234 // in the comment below. If you're running on Windows,
235 // they get defined by Windows system header files.
236 // If you aren't running on Windows, then you don't
237 // need them.
238 //#define BI_RGB 0L
239 //#define BI_RLE8 1L
240 //#define BI_RLE4 2L
241 //#define BI_BITFIELDS 3L
242 
243 // Mimics Windows BITMAPINFOHEADER structure.
244 // For details searh for "BITMAPINFOHEADER" at http://msdn.microsoft.com/default.asp
246 {
247  unsigned int biSize; // DWORD = sizeof(BITMAPINFOHEADER)
248  int biWidth; // LONG = width (in pixels) of (decompressed) bitmap
249  int biHeight; // LONG = height (in pixels) of (decompressed) bitmap
250  // >0 means it's a bottom-up bitmap with origin
251  // in the lower left corner.
252  // <0 means it's a top-down bitmap with origin
253  // in the upper left corner.
254  unsigned short biPlanes; // WORD = number of planes
255  // (always 1 in current Windows versions)
256  unsigned short biBitCount; // WORD = bits per pixel (0,1,4,8,16,24,32 are valid)
257  // 1 See http://msdn.microsoft.com/default.asp
258  // 4 See http://msdn.microsoft.com/default.asp
259  // 8 The bitmap has a maximum of 256 colors,
260  // and the bmiColors member contains up
261  // to 256 entries. In this case, each byte
262  // in the array represents a single pixel.
263  // 16 See http://msdn.microsoft.com/default.asp
264  // 24 If biClrUsed=0 and biCompression=BI_RGB(0),
265  // then each 3-byte triplet in the bitmap
266  // array represents the relative intensities
267  // of blue, green, and red, respectively, for
268  // a pixel. For other possibilities, see
269  // http://msdn.microsoft.com/default.asp
270  // 32 If biClrUsed=0 and biCompression=BI_RGB(0),
271  // then each 4-byte DWORD in the bitmap
272  // array represents the relative intensities
273  // of blue, green, and red, respectively, for
274  // a pixel. The high byte in each DWORD is not
275  // used.
276  // If biClrUsed=3, biCompression=BITFIELDS(3),
277  // biColors[0] = red mask (0x00FF0000),
278  // biColors[1] = green mask (0x0000FF00), and
279  // biColors[2] = blue mask (0x000000FF),
280  // then tese masks are used with each 4-byte
281  // DWORD in the bitmap array to determine
282  // the pixel's relative intensities. //
283  // For other possibilities, see
284  // http://msdn.microsoft.com/default.asp
285  unsigned int biCompression; // DWORD Currently, Windows defines the following
286  // types of compression.
287  // =0 BI_RGB (no compression)
288  // =1 BI_RLE8 (run length encoded used for 8 bpp)
289  // =2 BI_RLE4 (run length encoded used for 4 bpp)
290  // =3 BI_BITFIELDS Specifies that the bitmap is
291  // not compressed and that the color table
292  // consists of three DWORD color masks that
293  // specify the red, green, and blue components,
294  // respectively, of each pixel. This is valid
295  // when used with 16- and 32-bit-per-pixel
296  // bitmaps.
297  // =4 BI_JPEG (not supported in Win 95/NT4)
298  //
299  unsigned int biSizeImage; // DWORD = bytes in image
300  int biXPelsPerMeter; // LONG
301  int biYPelsPerMeter; // LONG
302  unsigned int biClrUsed; // DWORD = 0 or true length of bmiColors[] array. If 0,
303  // then the value of biBitCount determines the
304  // length of the bmiColors[] array.
305  unsigned int biClrImportant; // DWORD
306 };
307 
309  // Mimics Windows RGBQUAD structure.
310  // For details searh for "RGBQUAD" at http://msdn.microsoft.com/default.asp
311  unsigned char rgbBlue; // BYTE
312  unsigned char rgbGreen; // BYTE
313  unsigned char rgbRed; // BYTE
314  unsigned char rgbReserved; // BYTE
315 };
316 
318 {
319  // Mimics Windows BITMAPINFO structure.
320  // For details searh for "BITMAPINFO" at http://msdn.microsoft.com/default.asp
322  ON_WindowsRGBQUAD bmiColors[1]; // The "[1]" is for the compiler. In
323  // practice this array commonly has
324  // length 0, 3, or 256 and a BITMAPINFO*
325  // points to a contiguous piece of memory
326  // that contains
327  //
328  // BITMAPINFOHEADER
329  // RGBQUAD[length determined by flags]
330  // unsigned char[biSizeImage]
331  //
332  // See the ON_WindowsBITMAPINFOHEADER comments
333  // and http://msdn.microsoft.com/default.asp
334  // for more details.
335 };
336 
337 #endif
338 
339 // OBSOLETE // class ON_OpenGLBitmap;
340 
341 class ON_CLASS ON_WindowsBitmap : public ON_Bitmap
342 {
343  ON_OBJECT_DECLARE(ON_WindowsBitmap);
344  // Uncompressed 8 bpp, 24 bpp, or 32 bpp Windows device
345  // independent bitmaps (DIB)
346 public:
347 
351 
353 
355  void Destroy();
356 
357  bool Create(
358  int, // width
359  int, // height
360  int // bits per pixel ( 1, 2, 4, 8, 16, 24, or 32 )
361  );
362 
363  /*
364  Description:
365  Tests an object to see if its data members are correctly
366  initialized.
367  Parameters:
368  text_log - [in] if the object is not valid and text_log
369  is not NULL, then a brief englis description of the
370  reason the object is not valid is appened to the log.
371  The information appended to text_log is suitable for
372  low-level debugging purposes by programmers and is
373  not intended to be useful as a high level user
374  interface tool.
375  Returns:
376  @untitled table
377  true object is valid
378  false object is invalid, uninitialized, etc.
379  Remarks:
380  Overrides virtual ON_Object::IsValid
381  */
382  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
383 
384  ON_BOOL32 Write( ON_BinaryArchive& ) const; // writes compressed image
385  ON_BOOL32 Read( ON_BinaryArchive& ); // reads compressed image
390 
391  int Width() const;
392  int Height() const; // >0 means it's a bottom-up bitmap with origin at lower right
393  // <0 means it's a top-down bitmap with origin at upper left
394 
395  int PaletteColorCount() const; // number of colors in palette
396  int SizeofPalette() const; // number of bytes in palette
397  int BitsPerPixel() const;
398  //int SizeofPixel() const; // number of bytes per pixel
399  int SizeofScan() const; // number of bytes per scan line
400  int SizeofImage() const; // number of bytes in image
401 
402  unsigned char* Bits(
403  int // index of scan line
404  );
405  const unsigned char* Bits(
406  int // index of scan line
407  ) const;
408 
409  //int PaletteIndex( ON_Color ) const; // for 8bpp bitmaps
410 
412  int, // 0 <= i < width
413  int // 0 <= j < height
414  ) const;
416  int, // 0 <= i < width
417  const unsigned char* // value of Bits( j )
418  ) const;
419 
420  //ON_BOOL32 SetColor( // sets entire map to specified color
421  // ON_Color
422  // );
423 
424 #if defined(ON_OS_WINDOWS_GDI)
425 
426  /*
427  Description:
428  Create an ON_WindowsBitmap from a contiguous bitmap.
429  Copies src.
430  Parameters:
431  src - [in] contiguous Windows device independent bitmap.
432  Remarks:
433  If the current Windows BITMAPINFO is identical to ON_WindowsBITMAPINFO,
434  then the result of this call is identical to
435 
436  int color_count = number of colors in bitmap's palette;
437  ON_WindowsBitmap::Create( &src, &src.bmiColors[color_count], true ).
438 
439  See Also:
440  ON_WindowsBitmap::Create
441  */
442  ON_WindowsBitmap( const BITMAPINFO& src );
443 
444  /*
445  Description:
446  Create an ON_WindowsBitmap from a contiguous bitmap.
447  Shares bitmap memory with src.
448  Parameters:
449  src - [in] contiguous Windows device independent bitmap.
450  See Also:
451  ON_WindowsBitmap::Create
452  Remarks:
453  ~ON_WindowsBitmap will not delete src.
454  */
455  ON_WindowsBitmap( const BITMAPINFO* src );
456 
457  /*
458  Description:
459  Create an ON_WindowsBitmap from a contiguous bitmap.
460  Copies src.
461  Parameters:
462  src - [in] contiguous Windows device independent bitmap.
463  See Also:
464  ON_WindowsBitmap::Create
465  */
466  ON_WindowsBitmap& operator=( const BITMAPINFO& src );
467 
468  /*
469  Description:
470  Create and ON_WindowsBitmap from a Windows BITMAPINFO pointer
471  and a pointer to the bits.
472 
473  This is intended to make it easy to write compressed bimaps.
474  For ON_WindowsBitmap classes created with ON_WindowsBitmap::Share,
475  ON_WindowsBitmap::Destroy and ~ON_WindowsBitmap will
476  not free the bmi and bits memory.
477 
478  Parameters:
479  bmi - [in] valid BITMAPINFO
480  bits - [in] bits for BITMAPINFO
481  bCopy - [in] If true, the bmi and bits are copied into a contiguous
482  bitmap that will be deleted by ~ON_WindowsBitmap.
483  If false, the m_bmi and m_bits pointers on this class
484  are simply set to bmi and bits. In this case,
485  ~ON_WindowsBitmap will not free the bmi or bits
486  memory.
487 
488  Example:
489 
490  ON_BinaryArchive archive = ...;
491  BITMAPINFO* bmi = 0;
492  unsigned char* bits = 0;
493  int color_count = ...; // number of colors in palette
494 
495  int sizeof_palette = sizeof(bmi->bmiColors[0]) * color_count;
496 
497  BITMAPINFO* bmi = (LPBITMAPINFO)calloc( 1, sizeof(*bmi) + sizeof_palette );
498 
499  bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
500  bmi->bmiHeader.biWidth = width;
501  bmi->bmiHeader.biHeight = height;
502  bmi->bmiHeader.biPlanes = 1;
503  bmi->bmiHeader.biBitCount = (USHORT)color_depth;
504  bmi->bmiHeader.biCompression = BI_RGB;
505  bmi->bmiHeader.biXPelsPerMeter = 0;
506  bmi->bmiHeader.biYPelsPerMeter = 0;
507  bmi->bmiHeader.biClrUsed = 0;
508  bmi->bmiHeader.biClrImportant = 0;
509  bmi->bmiHeader.biSizeImage = GetStorageSize();
510 
511  // initialize palette
512  ...
513 
514  HBITMAP hbm = ::CreateDIBSection( NULL, bmi, ..., (LPVOID*)&bits, NULL, 0);
515 
516  {
517  // Use ON_WindowsBitmap to write a compressed bitmap to
518  // archive. Does not modify bmi or bits.
519  ON_WindowsBitmap onbm;
520  onbm.Create(bmi,bit,false);
521  onbm.Write( arcive );
522  }
523 
524  */
525  bool Create( const BITMAPINFO* bmi,
526  const unsigned char* bits,
527  bool bCopy
528  );
529 
530 #endif
531 
532  /*
533  Returns:
534  True if m_bmi and m_bits are in a single contiguous
535  block of memory.
536  False if m_bmi and m_bits are in two blocks of memory.
537  */
538  bool IsContiguous() const;
539 
540 #if defined(ON_OS_WINDOWS_GDI)
541  BITMAPINFO* m_bmi;
542 #else
544 #endif
545 
546  unsigned char* m_bits;
547 
548 private:
549  int m_bFreeBMI; // 0 m_bmi and m_bits are not freed by ON_WindowsBitmap::Destroy
550  // 1 m_bmi memory is freed by ON_WindowsBitmap::Destroy
551  // 2 m_bits memory is freed by ON_WindowsBitmap::Destroy
552  // 3 m_bmi and m_bits memory is freed by ON_WindowsBitmap::Destroy
553 };
554 
555 /*
556 Description:
557  ON_WindowsBitmapEx is identical to ON_WindowsBitmap except that
558  it's Read/Write functions save bitmap names.
559 */
560 class ON_CLASS ON_WindowsBitmapEx : public ON_WindowsBitmap
561 {
562  ON_OBJECT_DECLARE(ON_WindowsBitmapEx);
563 public:
566  ON_BOOL32 Write( ON_BinaryArchive& ) const; // writes compressed image
567  ON_BOOL32 Read( ON_BinaryArchive& ); // reads compressed image
568 };
569 
570 class ON_CLASS ON_EmbeddedBitmap : public ON_Bitmap
571 {
572  ON_OBJECT_DECLARE(ON_EmbeddedBitmap);
573 public:
577  void Destroy();
578  void Create( int sizeof_buffer );
579 
580  ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
581 
582  ON_BOOL32 Write( ON_BinaryArchive& ) const;
583  ON_BOOL32 Read( ON_BinaryArchive& );
584 
585  int Width() const;
586  int Height() const;
587  int BitsPerPixel() const;
588  int SizeofScan() const;
589  int SizeofImage() const;
590  unsigned char* Bits(int);
591  const unsigned char* Bits(int) const;
592 
593  void* m_buffer;
594  std::size_t m_sizeof_buffer;
595  int m_free_buffer; // 1 = ~ON_EmbeddedBitmap will onfree m_buffer.
596  ON__UINT32 m_biffer_crc32; // 32 bit crc from ON_CRC32
597 };
598 
599 
600 #endif
virtual int Width() const =0
void Dump(ON_TextLog &) const
virtual int SizeofScan() const =0
ON_wString m_bitmap_name
ON_UUID m_bitmap_id
ON_wString m_bitmap_filename
void EmergencyDestroy()
virtual unsigned char * Bits(int)=0
void Defaults()
virtual int Height() const =0
virtual int SizeofImage() const =0
ON_UUID ModelObjectId() const
virtual const unsigned char * Bits(int) const =0
virtual int BitsPerPixel() const =0
void Destroy()
ON_BOOL32 Read(ON_BinaryArchive &)
int Height() const
int Width() const
void Create(int sizeof_buffer)
int SizeofScan() const
ON_BOOL32 Write(ON_BinaryArchive &) const
ON__UINT32 m_biffer_crc32
std::size_t m_sizeof_buffer
int BitsPerPixel() const
ON_BOOL32 IsValid(ON_TextLog *text_log=NULL) const
const unsigned char * Bits(int) const
unsigned char * Bits(int)
int SizeofImage() const
void SetFullFileName(const wchar_t *full_file_name)
bool Create(const wchar_t *filename, bool bCompress)
ON_BOOL32 Write(ON_BinaryArchive &) const
void SetRelativeFileName(const wchar_t *relative_file_name)
ON_EmbeddedFile & operator=(const ON_EmbeddedFile &src)
const wchar_t * FullFileName() const
bool Create(const void *source_buffer, ON__UINT64 sizeof_source_buffer, bool bCompress)
bool Extract(FILE *fp) const
ON__UINT64 FileSize() const
ON__UINT32 m_file_crc
ON__UINT64 m_file_time
void EmergencyDestroy()
virtual ~ON_EmbeddedFile()
ON_EmbeddedFile(const ON_EmbeddedFile &src)
ON_wString m_full_file_name
unsigned char m_bCompressedBuffer
ON__UINT64 FileLastModifiedTime() const
ON__UINT64 m_file_size
void SetId(ON_UUID id)
bool Extract(void *buffer) const
ON_BOOL32 Read(ON_BinaryArchive &)
ON_UUID Id() const
ON_BOOL32 IsValid(ON_TextLog *text_log=NULL) const
bool Extract(const wchar_t *destination_filename) const
void DestroyBuffer()
ON_wString m_relative_file_name
const wchar_t * RelativeFileName() const
ON__UINT32 m_buffer_crc
ON__UINT32 FileCRC() const
bool Create(FILE *fp, bool bCompress)
ON_Object & operator=(const ON_Object &)
ON_BOOL32 Read(ON_BinaryArchive &)
ON_BOOL32 Write(ON_BinaryArchive &) const
int Width() const
int Height() const
void EmergencyDestroy()
ON_WindowsBitmap(const ON_WindowsBitmap &)
int SizeofImage() const
int BitsPerPixel() const
int PaletteColorCount() const
int SizeofScan() const
const unsigned char * Bits(int) const
unsigned char * m_bits
ON_Color Pixel(int, const unsigned char *) const
bool WriteCompressed(ON_BinaryArchive &) const
bool ReadUncompressed(ON_BinaryArchive &)
bool Create(int, int, int)
ON_WindowsBitmap & operator=(const ON_WindowsBitmap &)
bool ReadCompressed(ON_BinaryArchive &)
ON_BOOL32 Write(ON_BinaryArchive &) const
ON_BOOL32 IsValid(ON_TextLog *text_log=NULL) const
struct ON_WindowsBITMAPINFO * m_bmi
ON_BOOL32 Read(ON_BinaryArchive &)
int SizeofPalette() const
ON_Color Pixel(int, int) const
bool WriteUncompressed(ON_BinaryArchive &) const
bool IsContiguous() const
unsigned char * Bits(int)
ON_WindowsBITMAPINFOHEADER bmiHeader
ON_WindowsRGBQUAD bmiColors[1]
unsigned char rgbGreen
unsigned char rgbReserved
unsigned char rgbRed
unsigned char rgbBlue