00001 /* $NoKeywords: $ */ 00002 /* 00003 // 00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved. 00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert 00006 // McNeel & Associates. 00007 // 00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED. 00011 // 00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>. 00013 // 00015 */ 00016 00018 // 00019 // Defines ON_WindowsBITMAPINFO class that is used to provide OS independent 00020 // serialization of Windows device independent bitmaps (BITMAPINFO) used 00021 // to store preview images. 00022 // 00024 00025 #if !defined(OPENNURBS_BITMAP_INC_) 00026 #define OPENNURBS_BITMAP_INC_ 00027 00028 class ON_CLASS ON_Bitmap : public ON_Object 00029 { 00030 // virtual base class for bitmap objects 00031 ON_OBJECT_DECLARE(ON_Bitmap); 00032 public: 00033 ON_Bitmap(); 00034 ~ON_Bitmap(); 00035 00036 // C++ defaults work fine 00037 //ON_Bitmap(const ON_Bitmap&); 00038 //ON_Bitmap& operator=(const ON_Bitmap&); 00039 00040 // virtual 00041 ON_UUID ModelObjectId() const; 00042 00043 00044 void Dump( ON_TextLog& ) const; // for debugging 00045 00046 void EmergencyDestroy(); 00047 void Destroy(); 00048 00049 void Defaults(); 00050 00051 virtual 00052 int Width() const = 0; 00053 virtual 00054 int Height() const = 0; // >0 means it's a bottom-up bitmap with origin at lower right 00055 // <0 means it's a top-down bitmap with origin at upper left 00056 virtual 00057 int BitsPerPixel() const = 0; // bits per pixel 00058 virtual 00059 int SizeofScan() const = 0; // number of bytes per scan line 00060 virtual 00061 int SizeofImage() const = 0; // size of current map in bytes 00062 00063 virtual 00064 unsigned char* Bits( 00065 int // index of scan line 00066 ) = 0; 00067 virtual 00068 const unsigned char* Bits( 00069 int // index of scan line 00070 ) const = 0; 00071 00072 ON_UUID m_bitmap_id; 00073 int m_bitmap_index; 00074 ON_wString m_bitmap_name; // descriptive name 00075 ON_wString m_bitmap_filename; // full path to file 00076 }; 00077 00078 /* 00079 ON_EmbeddedFile is derived from ON_Bitmap so it can be stored 00080 in the Rhino document's CRhinoDoc::m_bitmap_table[] array. 00081 The ON_EmbeddedFile class is used to embed any type of file 00082 in a Rhino document. 00083 */ 00084 class ON_CLASS ON_EmbeddedFile : public ON_Object 00085 { 00086 ON_OBJECT_DECLARE(ON_EmbeddedFile); 00087 public: 00088 ON_EmbeddedFile(); 00089 ON_EmbeddedFile(const ON_EmbeddedFile& src); 00090 ON_EmbeddedFile& operator=(const ON_EmbeddedFile& src); 00091 00092 virtual ~ON_EmbeddedFile(); 00093 void EmergencyDestroy(); 00094 void Destroy(); 00095 void DestroyBuffer(); 00096 00097 /* 00098 Description: 00099 Store the specified file in an ON_EmbeddedFile class. 00100 Parameters: 00101 filename - [in] 00102 full path to the file. 00103 bCompress - [in] 00104 true if the image of the file should be compressed. 00105 (Pass false if the file is already in a compressed 00106 format, like jpg, png, zip files.) 00107 Returns: 00108 true if successful. When true is returned m_id is set to 00109 a new unique id, m_full_file_name is set to filename, 00110 and m_relative_file_name is empty. 00111 */ 00112 bool Create( 00113 const wchar_t* filename, 00114 bool bCompress 00115 ); 00116 00117 /* 00118 Description: 00119 Store the specified file in an ON_EmbeddedFile class. 00120 Parameters: 00121 fp - [in] 00122 Result of calling ON::OpenFile( ..., "rb" ) 00123 bCompress - [in] 00124 true if the image of the file should be compressed. 00125 (Pass false if the file is already in a compressed 00126 format, like jpg, png, zip files.) 00127 */ 00128 bool Create( 00129 FILE* fp, 00130 bool bCompress 00131 ); 00132 00133 /* 00134 Description: 00135 Store the specified buffer in an ON_EmbeddedFile class. 00136 Parameters: 00137 source_buffer - [in] 00138 source_buffer_size - [in] 00139 number of bytes in source_buffer. 00140 bCompress - [in] 00141 true if the source_buffer should be compressed. 00142 (Pass false if source_buffer is already in a compressed format.) 00143 */ 00144 bool Create( 00145 const void* source_buffer, 00146 ON__UINT64 sizeof_source_buffer, 00147 bool bCompress 00148 ); 00149 00150 bool Extract( 00151 const wchar_t* destination_filename 00152 ) const; 00153 00154 bool Extract( 00155 FILE* fp 00156 ) const; 00157 00158 /* 00159 Description: 00160 Extracts the file into a buffer. 00161 Parameters: 00162 buffer - [out] 00163 buffer must point to FileSize() bytes of memory. 00164 The extracted file will be copied to this buffer. 00165 Returns: 00166 True if successful. 00167 False if not successful. 00168 */ 00169 bool Extract( 00170 void* buffer 00171 ) const; 00172 00173 /* 00174 Returns 00175 full path file name 00176 */ 00177 const wchar_t* FullFileName() const; 00178 00179 /* 00180 Returns 00181 Relative file name. Usually relative to the directory 00182 where the archive containing this embedded file was last 00183 saved. 00184 */ 00185 const wchar_t* RelativeFileName() const; 00186 00187 ON_UUID Id() const; 00188 00189 void SetId( ON_UUID id ); 00190 00191 void SetFullFileName( const wchar_t* full_file_name ); 00192 00193 00194 void SetRelativeFileName( const wchar_t* relative_file_name ); 00195 00196 ON__UINT64 FileSize() const; 00197 ON__UINT64 FileLastModifiedTime() const; 00198 ON__UINT32 FileCRC() const; 00199 00200 ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const; 00201 00202 ON_BOOL32 Write( ON_BinaryArchive& ) const; 00203 ON_BOOL32 Read( ON_BinaryArchive& ); 00204 00205 // The relative path is typically set when the .3dm file is 00206 // saved and is the path to the file relative to the location 00207 // of the saved file. 00208 // (The full path to the file is in ON_Bitmap::m_bitmap_filename.) 00209 ON_UUID m_id; 00210 ON_wString m_full_file_name; // full path file name 00211 ON_wString m_relative_file_name; // relative path when the archive was last saved. 00212 00213 private: 00214 void* m_reserved; 00215 00216 public: 00217 ON__UINT64 m_file_size; 00218 ON__UINT64 m_file_time; // last modified time returned by ON::GetFileStats() 00219 ON__UINT32 m_file_crc; // 32 bit crc of the file from ON_CRC32 00220 00221 public: 00222 ON__UINT32 m_buffer_crc; // will be different from m_file_crc if the buffer is compressed. 00223 ON_Buffer m_buffer; 00224 unsigned char m_bCompressedBuffer; // true if m_buffer is compressed. 00225 00226 private: 00227 unsigned char m_reserved3[7]; 00228 }; 00229 00230 00231 #if !defined(ON_OS_WINDOWS_GDI) 00232 00233 // These are the values of the Windows defines mentioned 00234 // in the comment below. If you're running on Windows, 00235 // they get defined by Windows system header files. 00236 // If you aren't running on Windows, then you don't 00237 // need them. 00238 //#define BI_RGB 0L 00239 //#define BI_RLE8 1L 00240 //#define BI_RLE4 2L 00241 //#define BI_BITFIELDS 3L 00242 00243 // Mimics Windows BITMAPINFOHEADER structure. 00244 // For details searh for "BITMAPINFOHEADER" at http://msdn.microsoft.com/default.asp 00245 struct ON_WindowsBITMAPINFOHEADER 00246 { 00247 unsigned int biSize; // DWORD = sizeof(BITMAPINFOHEADER) 00248 int biWidth; // LONG = width (in pixels) of (decompressed) bitmap 00249 int biHeight; // LONG = height (in pixels) of (decompressed) bitmap 00250 // >0 means it's a bottom-up bitmap with origin 00251 // in the lower left corner. 00252 // <0 means it's a top-down bitmap with origin 00253 // in the upper left corner. 00254 unsigned short biPlanes; // WORD = number of planes 00255 // (always 1 in current Windows versions) 00256 unsigned short biBitCount; // WORD = bits per pixel (0,1,4,8,16,24,32 are valid) 00257 // 1 See http://msdn.microsoft.com/default.asp 00258 // 4 See http://msdn.microsoft.com/default.asp 00259 // 8 The bitmap has a maximum of 256 colors, 00260 // and the bmiColors member contains up 00261 // to 256 entries. In this case, each byte 00262 // in the array represents a single pixel. 00263 // 16 See http://msdn.microsoft.com/default.asp 00264 // 24 If biClrUsed=0 and biCompression=BI_RGB(0), 00265 // then each 3-byte triplet in the bitmap 00266 // array represents the relative intensities 00267 // of blue, green, and red, respectively, for 00268 // a pixel. For other possibilities, see 00269 // http://msdn.microsoft.com/default.asp 00270 // 32 If biClrUsed=0 and biCompression=BI_RGB(0), 00271 // then each 4-byte DWORD in the bitmap 00272 // array represents the relative intensities 00273 // of blue, green, and red, respectively, for 00274 // a pixel. The high byte in each DWORD is not 00275 // used. 00276 // If biClrUsed=3, biCompression=BITFIELDS(3), 00277 // biColors[0] = red mask (0x00FF0000), 00278 // biColors[1] = green mask (0x0000FF00), and 00279 // biColors[2] = blue mask (0x000000FF), 00280 // then tese masks are used with each 4-byte 00281 // DWORD in the bitmap array to determine 00282 // the pixel's relative intensities. // 00283 // For other possibilities, see 00284 // http://msdn.microsoft.com/default.asp 00285 unsigned int biCompression; // DWORD Currently, Windows defines the following 00286 // types of compression. 00287 // =0 BI_RGB (no compression) 00288 // =1 BI_RLE8 (run length encoded used for 8 bpp) 00289 // =2 BI_RLE4 (run length encoded used for 4 bpp) 00290 // =3 BI_BITFIELDS Specifies that the bitmap is 00291 // not compressed and that the color table 00292 // consists of three DWORD color masks that 00293 // specify the red, green, and blue components, 00294 // respectively, of each pixel. This is valid 00295 // when used with 16- and 32-bit-per-pixel 00296 // bitmaps. 00297 // =4 BI_JPEG (not supported in Win 95/NT4) 00298 // 00299 unsigned int biSizeImage; // DWORD = bytes in image 00300 int biXPelsPerMeter; // LONG 00301 int biYPelsPerMeter; // LONG 00302 unsigned int biClrUsed; // DWORD = 0 or true length of bmiColors[] array. If 0, 00303 // then the value of biBitCount determines the 00304 // length of the bmiColors[] array. 00305 unsigned int biClrImportant; // DWORD 00306 }; 00307 00308 struct ON_WindowsRGBQUAD { 00309 // Mimics Windows RGBQUAD structure. 00310 // For details searh for "RGBQUAD" at http://msdn.microsoft.com/default.asp 00311 unsigned char rgbBlue; // BYTE 00312 unsigned char rgbGreen; // BYTE 00313 unsigned char rgbRed; // BYTE 00314 unsigned char rgbReserved; // BYTE 00315 }; 00316 00317 struct ON_WindowsBITMAPINFO 00318 { 00319 // Mimics Windows BITMAPINFO structure. 00320 // For details searh for "BITMAPINFO" at http://msdn.microsoft.com/default.asp 00321 ON_WindowsBITMAPINFOHEADER bmiHeader; 00322 ON_WindowsRGBQUAD bmiColors[1]; // The "[1]" is for the compiler. In 00323 // practice this array commonly has 00324 // length 0, 3, or 256 and a BITMAPINFO* 00325 // points to a contiguous piece of memory 00326 // that contains 00327 // 00328 // BITMAPINFOHEADER 00329 // RGBQUAD[length determined by flags] 00330 // unsigned char[biSizeImage] 00331 // 00332 // See the ON_WindowsBITMAPINFOHEADER comments 00333 // and http://msdn.microsoft.com/default.asp 00334 // for more details. 00335 }; 00336 00337 #endif 00338 00339 // OBSOLETE // class ON_OpenGLBitmap; 00340 00341 class ON_CLASS ON_WindowsBitmap : public ON_Bitmap 00342 { 00343 ON_OBJECT_DECLARE(ON_WindowsBitmap); 00344 // Uncompressed 8 bpp, 24 bpp, or 32 bpp Windows device 00345 // independent bitmaps (DIB) 00346 public: 00347 00348 ON_WindowsBitmap(); 00349 ON_WindowsBitmap( const ON_WindowsBitmap& ); 00350 ~ON_WindowsBitmap(); 00351 00352 ON_WindowsBitmap& operator=( const ON_WindowsBitmap& ); 00353 00354 void EmergencyDestroy(); 00355 void Destroy(); 00356 00357 bool Create( 00358 int, // width 00359 int, // height 00360 int // bits per pixel ( 1, 2, 4, 8, 16, 24, or 32 ) 00361 ); 00362 00363 /* 00364 Description: 00365 Tests an object to see if its data members are correctly 00366 initialized. 00367 Parameters: 00368 text_log - [in] if the object is not valid and text_log 00369 is not NULL, then a brief englis description of the 00370 reason the object is not valid is appened to the log. 00371 The information appended to text_log is suitable for 00372 low-level debugging purposes by programmers and is 00373 not intended to be useful as a high level user 00374 interface tool. 00375 Returns: 00376 @untitled table 00377 true object is valid 00378 false object is invalid, uninitialized, etc. 00379 Remarks: 00380 Overrides virtual ON_Object::IsValid 00381 */ 00382 ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const; 00383 00384 ON_BOOL32 Write( ON_BinaryArchive& ) const; // writes compressed image 00385 ON_BOOL32 Read( ON_BinaryArchive& ); // reads compressed image 00386 bool WriteCompressed( ON_BinaryArchive& ) const; 00387 bool ReadCompressed( ON_BinaryArchive& ); 00388 bool WriteUncompressed( ON_BinaryArchive& ) const; 00389 bool ReadUncompressed( ON_BinaryArchive& ); 00390 00391 int Width() const; 00392 int Height() const; // >0 means it's a bottom-up bitmap with origin at lower right 00393 // <0 means it's a top-down bitmap with origin at upper left 00394 00395 int PaletteColorCount() const; // number of colors in palette 00396 int SizeofPalette() const; // number of bytes in palette 00397 int BitsPerPixel() const; 00398 //int SizeofPixel() const; // number of bytes per pixel 00399 int SizeofScan() const; // number of bytes per scan line 00400 int SizeofImage() const; // number of bytes in image 00401 00402 unsigned char* Bits( 00403 int // index of scan line 00404 ); 00405 const unsigned char* Bits( 00406 int // index of scan line 00407 ) const; 00408 00409 //int PaletteIndex( ON_Color ) const; // for 8bpp bitmaps 00410 00411 ON_Color Pixel( 00412 int, // 0 <= i < width 00413 int // 0 <= j < height 00414 ) const; 00415 ON_Color Pixel( 00416 int, // 0 <= i < width 00417 const unsigned char* // value of Bits( j ) 00418 ) const; 00419 00420 //ON_BOOL32 SetColor( // sets entire map to specified color 00421 // ON_Color 00422 // ); 00423 00424 #if defined(ON_OS_WINDOWS_GDI) 00425 00426 /* 00427 Description: 00428 Create an ON_WindowsBitmap from a contiguous bitmap. 00429 Copies src. 00430 Parameters: 00431 src - [in] contiguous Windows device independent bitmap. 00432 Remarks: 00433 If the current Windows BITMAPINFO is identical to ON_WindowsBITMAPINFO, 00434 then the result of this call is identical to 00435 00436 int color_count = number of colors in bitmap's palette; 00437 ON_WindowsBitmap::Create( &src, &src.bmiColors[color_count], true ). 00438 00439 See Also: 00440 ON_WindowsBitmap::Create 00441 */ 00442 ON_WindowsBitmap( const BITMAPINFO& src ); 00443 00444 /* 00445 Description: 00446 Create an ON_WindowsBitmap from a contiguous bitmap. 00447 Shares bitmap memory with src. 00448 Parameters: 00449 src - [in] contiguous Windows device independent bitmap. 00450 See Also: 00451 ON_WindowsBitmap::Create 00452 Remarks: 00453 ~ON_WindowsBitmap will not delete src. 00454 */ 00455 ON_WindowsBitmap( const BITMAPINFO* src ); 00456 00457 /* 00458 Description: 00459 Create an ON_WindowsBitmap from a contiguous bitmap. 00460 Copies src. 00461 Parameters: 00462 src - [in] contiguous Windows device independent bitmap. 00463 See Also: 00464 ON_WindowsBitmap::Create 00465 */ 00466 ON_WindowsBitmap& operator=( const BITMAPINFO& src ); 00467 00468 /* 00469 Description: 00470 Create and ON_WindowsBitmap from a Windows BITMAPINFO pointer 00471 and a pointer to the bits. 00472 00473 This is intended to make it easy to write compressed bimaps. 00474 For ON_WindowsBitmap classes created with ON_WindowsBitmap::Share, 00475 ON_WindowsBitmap::Destroy and ~ON_WindowsBitmap will 00476 not free the bmi and bits memory. 00477 00478 Parameters: 00479 bmi - [in] valid BITMAPINFO 00480 bits - [in] bits for BITMAPINFO 00481 bCopy - [in] If true, the bmi and bits are copied into a contiguous 00482 bitmap that will be deleted by ~ON_WindowsBitmap. 00483 If false, the m_bmi and m_bits pointers on this class 00484 are simply set to bmi and bits. In this case, 00485 ~ON_WindowsBitmap will not free the bmi or bits 00486 memory. 00487 00488 Example: 00489 00490 ON_BinaryArchive archive = ...; 00491 BITMAPINFO* bmi = 0; 00492 unsigned char* bits = 0; 00493 int color_count = ...; // number of colors in palette 00494 00495 int sizeof_palette = sizeof(bmi->bmiColors[0]) * color_count; 00496 00497 BITMAPINFO* bmi = (LPBITMAPINFO)calloc( 1, sizeof(*bmi) + sizeof_palette ); 00498 00499 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader); 00500 bmi->bmiHeader.biWidth = width; 00501 bmi->bmiHeader.biHeight = height; 00502 bmi->bmiHeader.biPlanes = 1; 00503 bmi->bmiHeader.biBitCount = (USHORT)color_depth; 00504 bmi->bmiHeader.biCompression = BI_RGB; 00505 bmi->bmiHeader.biXPelsPerMeter = 0; 00506 bmi->bmiHeader.biYPelsPerMeter = 0; 00507 bmi->bmiHeader.biClrUsed = 0; 00508 bmi->bmiHeader.biClrImportant = 0; 00509 bmi->bmiHeader.biSizeImage = GetStorageSize(); 00510 00511 // initialize palette 00512 ... 00513 00514 HBITMAP hbm = ::CreateDIBSection( NULL, bmi, ..., (LPVOID*)&bits, NULL, 0); 00515 00516 { 00517 // Use ON_WindowsBitmap to write a compressed bitmap to 00518 // archive. Does not modify bmi or bits. 00519 ON_WindowsBitmap onbm; 00520 onbm.Create(bmi,bit,false); 00521 onbm.Write( arcive ); 00522 } 00523 00524 */ 00525 bool Create( const BITMAPINFO* bmi, 00526 const unsigned char* bits, 00527 bool bCopy 00528 ); 00529 00530 #endif 00531 00532 /* 00533 Returns: 00534 True if m_bmi and m_bits are in a single contiguous 00535 block of memory. 00536 False if m_bmi and m_bits are in two blocks of memory. 00537 */ 00538 bool IsContiguous() const; 00539 00540 #if defined(ON_OS_WINDOWS_GDI) 00541 BITMAPINFO* m_bmi; 00542 #else 00543 struct ON_WindowsBITMAPINFO* m_bmi; 00544 #endif 00545 00546 unsigned char* m_bits; 00547 00548 private: 00549 int m_bFreeBMI; // 0 m_bmi and m_bits are not freed by ON_WindowsBitmap::Destroy 00550 // 1 m_bmi memory is freed by ON_WindowsBitmap::Destroy 00551 // 2 m_bits memory is freed by ON_WindowsBitmap::Destroy 00552 // 3 m_bmi and m_bits memory is freed by ON_WindowsBitmap::Destroy 00553 }; 00554 00555 /* 00556 Description: 00557 ON_WindowsBitmapEx is identical to ON_WindowsBitmap except that 00558 it's Read/Write functions save bitmap names. 00559 */ 00560 class ON_CLASS ON_WindowsBitmapEx : public ON_WindowsBitmap 00561 { 00562 ON_OBJECT_DECLARE(ON_WindowsBitmapEx); 00563 public: 00564 ON_WindowsBitmapEx(); 00565 ~ON_WindowsBitmapEx(); 00566 ON_BOOL32 Write( ON_BinaryArchive& ) const; // writes compressed image 00567 ON_BOOL32 Read( ON_BinaryArchive& ); // reads compressed image 00568 }; 00569 00570 class ON_CLASS ON_EmbeddedBitmap : public ON_Bitmap 00571 { 00572 ON_OBJECT_DECLARE(ON_EmbeddedBitmap); 00573 public: 00574 ON_EmbeddedBitmap(); 00575 ~ON_EmbeddedBitmap(); 00576 void EmergencyDestroy(); 00577 void Destroy(); 00578 void Create( int sizeof_buffer ); 00579 00580 ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const; 00581 00582 ON_BOOL32 Write( ON_BinaryArchive& ) const; 00583 ON_BOOL32 Read( ON_BinaryArchive& ); 00584 00585 int Width() const; 00586 int Height() const; 00587 int BitsPerPixel() const; 00588 int SizeofScan() const; 00589 int SizeofImage() const; 00590 unsigned char* Bits(int); 00591 const unsigned char* Bits(int) const; 00592 00593 void* m_buffer; 00594 size_t m_sizeof_buffer; 00595 int m_free_buffer; // 1 = ~ON_EmbeddedBitmap will onfree m_buffer. 00596 ON__UINT32 m_biffer_crc32; // 32 bit crc from ON_CRC32 00597 }; 00598 00599 00600 #endif