]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/imagbmp.cpp
don't mark GetTraceMask() deprecated as we use it in the same header which provokes...
[wxWidgets.git] / src / common / imagbmp.cpp
index 807d3a7067e7ad878d790beb37acb4e2ea2625d7..93288ddab1b3999b4a3bc2dc856d58629014c36c 100644 (file)
@@ -84,7 +84,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
     if ( !image->Ok() )
     {
         if ( verbose )
+        {
             wxLogError(_("BMP: Couldn't save invalid image."));
+        }
         return false;
     }
 
@@ -118,7 +120,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
             )
         {
             if ( verbose )
+            {
                 wxLogError(_("BMP: wxImage doesn't have own wxPalette."));
+            }
             return false;
         }
         bpp = 8;
@@ -180,9 +184,8 @@ bool wxBMPHandler::SaveDib(wxImage *image,
 
     // get the resolution from the image options  or fall back to 72dpi standard
     // for the BMP format if not specified
-    wxUint32 hres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONX),
-             vres = image->GetOptionInt(wxIMAGE_OPTION_RESOLUTIONY);
-    switch ( image->GetOptionInt(wxIMAGE_OPTION_RESOLUTION) )
+    int hres, vres;
+    switch ( GetResolutionFromOptions(*image, &hres, &vres) )
     {
         default:
             wxFAIL_MSG( _T("unexpected image resolution units") );
@@ -195,8 +198,8 @@ bool wxBMPHandler::SaveDib(wxImage *image,
 
         case wxIMAGE_RESOLUTION_INCHES:
             // convert resolution in inches to resolution in centimeters
-            hres = (wxUint32)(10*mm2inches*hres);
-            vres = (wxUint32)(10*mm2inches*vres);
+            hres = (int)(10*mm2inches*hres);
+            vres = (int)(10*mm2inches*vres);
             // fall through to convert it to resolution in meters
 
         case wxIMAGE_RESOLUTION_CM:
@@ -223,7 +226,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
            )
         {
             if (verbose)
+            {
                 wxLogError(_("BMP: Couldn't write the file (Bitmap) header."));
+                }
             return false;
         }
     }
@@ -244,7 +249,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
            )
         {
             if (verbose)
+            {
                 wxLogError(_("BMP: Couldn't write the file (BitmapInfo) header."));
+            }
             return false;
         }
     }
@@ -318,7 +325,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
             if ( !stream.Write(rgbquad, palette_size*4) )
             {
                 if (verbose)
+                {
                     wxLogError(_("BMP: Couldn't write RGB color map."));
+                }
                 delete[] rgbquad;
 #if wxUSE_PALETTE
                 delete palette;
@@ -451,7 +460,9 @@ bool wxBMPHandler::SaveDib(wxImage *image,
         if ( !stream.Write(buffer, row_width) )
         {
             if (verbose)
+            {
                 wxLogError(_("BMP: Couldn't write data."));
+            }
             delete[] buffer;
 #if wxUSE_PALETTE
             delete palette;
@@ -484,7 +495,7 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
 {
     wxInt32         aDword, rmask = 0, gmask = 0, bmask = 0, amask = 0;
     int             rshift = 0, gshift = 0, bshift = 0, ashift = 0;
-    int             rbits = 0, gbits = 0, bbits = 0, abits = 0;
+    int             rbits = 0, gbits = 0, bbits = 0;
     wxInt32         dbuf[4];
     wxInt8          bbuf[4];
     wxUint8         aByte;
@@ -499,7 +510,9 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
         if ( !cmap )
         {
             if (verbose)
+            {
                 wxLogError(_("BMP: Couldn't allocate memory."));
+            }
             return false;
         }
     }
@@ -519,7 +532,9 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
     if ( !ptr )
     {
         if ( verbose )
+        {
             wxLogError( _("BMP: Couldn't allocate memory.") );
+        }
         return false;
     }
 
@@ -532,7 +547,9 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
         if ( !alpha )
         {
             if ( verbose )
+            {
                 wxLogError(_("BMP: Couldn't allocate memory."));
+            }
             return false;
         }
     }
@@ -631,7 +648,6 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
             rshift = 16;
             gshift = 8;
             bshift = 0;
-            abits = 8;
             rbits = 8;
             gbits = 8;
             bbits = 8;
@@ -642,7 +658,13 @@ bool wxBMPHandler::DoLoadDib(wxImage * image, int width, int height,
      * Reading the image data
      */
     if ( IsBmp )
-        stream.SeekI(bmpOffset); // else icon, just carry on
+    {
+        // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
+        //       load even non-seekable streams (see wxInputStream::SeekI docs)!
+        if (stream.SeekI(bmpOffset, wxFromCurrent) == wxInvalidOffset)
+            return false;
+        //else: icon, just carry on
+    }
 
     unsigned char *data = ptr;
 
@@ -897,15 +919,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
     wxInt32         dbuf[4];
     wxInt8          bbuf[4];
 
-    wxFileOffset offset = 0; // keep gcc quiet
     if ( IsBmp )
     {
         // read the header off the .BMP format file
-
-        offset = stream.TellI();
-        if (offset == wxInvalidOffset)
-            offset = 0;
-
         stream.Read(bbuf, 2);
         stream.Read(dbuf, 16);
     }
@@ -916,7 +932,7 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
     #if 0 // unused
         wxInt32 size = wxINT32_SWAP_ON_BE(dbuf[0]);
     #endif
-    offset = offset + wxINT32_SWAP_ON_BE(dbuf[2]);
+    wxFileOffset offset = wxINT32_SWAP_ON_BE(dbuf[2]);
 
     stream.Read(dbuf, 4 * 2);
     int width = wxINT32_SWAP_ON_BE((int)dbuf[0]);
@@ -926,13 +942,17 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
     if ( width > 32767 )
     {
         if (verbose)
+        {
             wxLogError( _("DIB Header: Image width > 32767 pixels for file.") );
+        }
         return false;
     }
     if ( height > 32767 )
     {
         if (verbose)
+        {
             wxLogError( _("DIB Header: Image height > 32767 pixels for file.") );
+        }
         return false;
     }
 
@@ -946,7 +966,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
     if ( bpp != 1 && bpp != 4 && bpp != 8 && bpp != 16 && bpp != 24 && bpp != 32 )
     {
         if (verbose)
+        {
             wxLogError( _("DIB Header: Unknown bitdepth in file.") );
+        }
         return false;
     }
 
@@ -956,7 +978,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
          comp != BI_BITFIELDS )
     {
         if (verbose)
+        {
             wxLogError( _("DIB Header: Unknown encoding in file.") );
+        }
         return false;
     }
 
@@ -971,7 +995,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
         ((comp == BI_BITFIELDS) && (bpp != 16 && bpp != 32)))
     {
         if (verbose)
+        {
             wxLogError( _("DIB Header: Encoding doesn't match bitdepth.") );
+        }
         return false;
     }
 
@@ -980,7 +1006,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
                     verbose, IsBmp, true) )
     {
         if (verbose)
+        {
             wxLogError( _("Error in reading image DIB.") );
+        }
         return false;
     }
 
@@ -993,7 +1021,9 @@ bool wxBMPHandler::LoadDib(wxImage *image, wxInputStream& stream,
                         verbose, IsBmp, false) )
         {
             if (verbose)
+            {
                 wxLogError( _("ICO: Error in reading mask DIB.") );
+            }
             return false;
         }
         image->SetMaskFromImage(mask, 255, 255, 255);
@@ -1019,7 +1049,7 @@ bool wxBMPHandler::DoCanRead(wxInputStream& stream)
 {
     unsigned char hdr[2];
 
-    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )     // it's ok to modify the stream position here
         return false;
 
     // do we have the BMP file signature?
@@ -1071,13 +1101,17 @@ bool wxICOHandler::SaveFile(wxImage *image,
     if ( image->GetHeight () > 127 )
     {
         if ( verbose )
+        {
             wxLogError(_("ICO: Image too tall for an icon."));
+        }
         return false;
     }
     if ( image->GetWidth () > 255 )
     {
         if ( verbose )
+        {
             wxLogError(_("ICO: Image too wide for an icon."));
+        }
         return false;
     }
 
@@ -1103,7 +1137,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
     if ( !stream.IsOk() )
     {
         if ( verbose )
+        {
             wxLogError(_("ICO: Error writing the image file!"));
+        }
         return false;
     }
 
@@ -1162,7 +1198,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
         if ( !bResult )
         {
             if ( verbose )
+            {
                 wxLogError(_("ICO: Error writing the image file!"));
+            }
             return false;
         }
         IsMask = true;
@@ -1171,7 +1209,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
         if ( !bResult )
         {
             if ( verbose )
+            {
                 wxLogError(_("ICO: Error writing the image file!"));
+            }
             return false;
         }
         wxUint32 Size = cStream.GetSize();
@@ -1226,7 +1266,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
         if ( !stream.IsOk() )
         {
             if ( verbose )
+            {
                 wxLogError(_("ICO: Error writing the image file!"));
+            }
             return false;
         }
 
@@ -1236,7 +1278,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
         if ( !bResult )
         {
             if ( verbose )
+            {
                 wxLogError(_("ICO: Error writing the image file!"));
+            }
             return false;
         }
         IsMask = true;
@@ -1245,7 +1289,9 @@ bool wxICOHandler::SaveFile(wxImage *image,
         if ( !bResult )
         {
             if ( verbose )
+            {
                 wxLogError(_("ICO: Error writing the image file!"));
+            }
             return false;
         }
 
@@ -1257,7 +1303,6 @@ bool wxICOHandler::SaveFile(wxImage *image,
 bool wxICOHandler::LoadFile(wxImage *image, wxInputStream& stream,
                             bool verbose, int index)
 {
-    stream.SeekI(0);
     return DoLoadFile(image, stream, verbose, index);
 }
 
@@ -1269,9 +1314,9 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
 
     ICONDIR IconDir;
 
-    wxFileOffset iPos = stream.TellI();
     stream.Read(&IconDir, sizeof(IconDir));
     wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
+    
     // nType is 1 for Icons, 2 for Cursors:
     wxUint16 nType = wxUINT16_SWAP_ON_BE(IconDir.idType);
 
@@ -1282,9 +1327,13 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
     int colmax = 0;
     int iSel = wxNOT_FOUND;
 
-    for (int i = 0; i < nIcons; i++ )
+    // remember how many bytes we read from the stream:
+    wxFileOffset alreadySeeked = sizeof(IconDir);
+    
+    for (unsigned int i = 0; i < nIcons; i++ )
     {
-        stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY));
+        alreadySeeked += stream.Read(pCurrentEntry, sizeof(ICONDIRENTRY)).LastRead();
+        
         // bHeight and bColorCount are wxUint8
         if ( pCurrentEntry->bWidth >= wMax )
         {
@@ -1298,6 +1347,7 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
                 colmax = pCurrentEntry->bColorCount;
             }
         }
+        
         pCurrentEntry++;
     }
 
@@ -1317,7 +1367,13 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
     {
         // seek to selected icon:
         pCurrentEntry = pIconDirEntry + iSel;
-        stream.SeekI(iPos + wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset), wxFromStart);
+        
+        // NOTE: seeking a positive amount in wxFromCurrent mode allows us to
+        //       load even non-seekable streams (see wxInputStream::SeekI docs)!
+        wxFileOffset offset = wxUINT32_SWAP_ON_BE(pCurrentEntry->dwImageOffset) - alreadySeeked;
+        if (offset != 0 && stream.SeekI(offset, wxFromCurrent) == wxInvalidOffset)
+            return false;
+        
         bResult = LoadDib(image, stream, true, IsBmp);
         bool bIsCursorType = (this->GetType() == wxBITMAP_TYPE_CUR) || (this->GetType() == wxBITMAP_TYPE_ANI);
         if ( bResult && bIsCursorType && nType == 2 )
@@ -1327,26 +1383,27 @@ bool wxICOHandler::DoLoadFile(wxImage *image, wxInputStream& stream,
             image->SetOption(wxIMAGE_OPTION_CUR_HOTSPOT_Y, wxUINT16_SWAP_ON_BE(pCurrentEntry->wBitCount));
         }
     }
-    delete[] pIconDirEntry;
+    
+    delete [] pIconDirEntry;
+    
     return bResult;
 }
 
-int wxICOHandler::GetImageCount(wxInputStream& stream)
+int wxICOHandler::DoGetImageCount(wxInputStream& stream)
 {
     ICONDIR IconDir;
-    wxFileOffset iPos = stream.TellI();
-    stream.SeekI(0);
-    stream.Read(&IconDir, sizeof(IconDir));
-    wxUint16 nIcons = wxUINT16_SWAP_ON_BE(IconDir.idCount);
-    stream.SeekI(iPos);
-    return (int)nIcons;
+
+    if (stream.Read(&IconDir, sizeof(IconDir)).LastRead() != sizeof(IconDir))
+             // it's ok to modify the stream position here
+        return 0;
+    
+    return (int)wxUINT16_SWAP_ON_BE(IconDir.idCount);
 }
 
 bool wxICOHandler::DoCanRead(wxInputStream& stream)
 {
-    stream.SeekI(0);
     unsigned char hdr[4];
-    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )     // it's ok to modify the stream position here
         return false;
 
     // hdr[2] is one for an icon and two for a cursor
@@ -1366,9 +1423,8 @@ IMPLEMENT_DYNAMIC_CLASS(wxCURHandler, wxICOHandler)
 
 bool wxCURHandler::DoCanRead(wxInputStream& stream)
 {
-    stream.SeekI(0);
     unsigned char hdr[4];
-    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )
+    if ( !stream.Read(hdr, WXSIZEOF(hdr)) )     // it's ok to modify the stream position here
         return false;
 
     // hdr[2] is one for an icon and two for a cursor
@@ -1399,12 +1455,13 @@ bool wxANIHandler::DoCanRead(wxInputStream& stream)
 {
     wxANIDecoder decod;
     return decod.CanRead(stream);
+             // it's ok to modify the stream position here
 }
 
-int wxANIHandler::GetImageCount(wxInputStream& stream)
+int wxANIHandler::DoGetImageCount(wxInputStream& stream)
 {
     wxANIDecoder decoder;
-    if (!decoder.Load(stream))
+    if (!decoder.Load(stream))  // it's ok to modify the stream position here
         return wxNOT_FOUND;
 
     return decoder.GetFrameCount();