From 60d43ad883f16c458928fa7e4e1dbb96f492e77a Mon Sep 17 00:00:00 2001
From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= <vslavik@fastmail.fm>
Date: Tue, 1 Jan 2002 23:13:37 +0000
Subject: [PATCH] support for multiple images in one file

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13306 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 include/wx/image.h      |  20 ++++----
 include/wx/imagtiff.h   |   2 +-
 src/common/image.cpp    | 101 +++++++++++++++++++++++++++++-----------
 src/common/imagtiff.cpp |   5 +-
 4 files changed, 91 insertions(+), 37 deletions(-)

diff --git a/include/wx/image.h b/include/wx/image.h
index af8b9995fb..9cee5d69f8 100644
--- a/include/wx/image.h
+++ b/include/wx/image.h
@@ -46,7 +46,7 @@ public:
     virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
     virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
 
-    virtual int GetImageCount( wxInputStream& stream );
+    virtual int GetImagesCount( wxInputStream& stream );
 
     bool CanRead( wxInputStream& stream ) { return DoCanRead(stream); }
     bool CanRead( const wxString& name );
@@ -92,12 +92,12 @@ public:
     wxImage();
     wxImage( int width, int height );
     wxImage( int width, int height, unsigned char* data, bool static_data = FALSE );
-    wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY );
-    wxImage( const wxString& name, const wxString& mimetype );
+    wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
+    wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
 
 #if wxUSE_STREAMS
-    wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
-    wxImage( wxInputStream& stream, const wxString& mimetype );
+    wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
+    wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
 #endif // wxUSE_STREAMS
 
     wxImage( const wxImage& image );
@@ -164,13 +164,15 @@ public:
                           unsigned char mr, unsigned char mg, unsigned char mb);
 
     static bool CanRead( const wxString& name );
-    virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY );
-    virtual bool LoadFile( const wxString& name, const wxString& mimetype );
+    static int GetImagesCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
+    virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
+    virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
 
 #if wxUSE_STREAMS
     static bool CanRead( wxInputStream& stream );
-    virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
-    virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype );
+    static int GetImagesCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
+    virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
+    virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
 #endif
 
     virtual bool SaveFile( const wxString& name, int type );
diff --git a/include/wx/imagtiff.h b/include/wx/imagtiff.h
index 55d3ff0689..6d25bf9ba3 100644
--- a/include/wx/imagtiff.h
+++ b/include/wx/imagtiff.h
@@ -40,7 +40,7 @@ public:
   virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=TRUE, int index=0 );
   virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=TRUE );
   virtual bool DoCanRead( wxInputStream& stream );
-  virtual int GetImageCount( wxInputStream& stream );
+  virtual int GetImagesCount( wxInputStream& stream );
 #endif
 };
 #endif
diff --git a/src/common/image.cpp b/src/common/image.cpp
index ee46631c93..80ee2992d3 100644
--- a/src/common/image.cpp
+++ b/src/common/image.cpp
@@ -108,25 +108,25 @@ wxImage::wxImage( int width, int height, unsigned char* data, bool static_data )
     Create( width, height, data, static_data );
 }
 
-wxImage::wxImage( const wxString& name, long type )
+wxImage::wxImage( const wxString& name, long type, int index )
 {
-    LoadFile( name, type );
+    LoadFile( name, type, index );
 }
 
-wxImage::wxImage( const wxString& name, const wxString& mimetype )
+wxImage::wxImage( const wxString& name, const wxString& mimetype, int index )
 {
-    LoadFile( name, mimetype );
+    LoadFile( name, mimetype, index );
 }
 
 #if wxUSE_STREAMS
-wxImage::wxImage( wxInputStream& stream, long type )
+wxImage::wxImage( wxInputStream& stream, long type, int index )
 {
-    LoadFile( stream, type );
+    LoadFile( stream, type, index );
 }
 
-wxImage::wxImage( wxInputStream& stream, const wxString& mimetype )
+wxImage::wxImage( wxInputStream& stream, const wxString& mimetype, int index )
 {
-    LoadFile( stream, mimetype );
+    LoadFile( stream, mimetype, index );
 }
 #endif // wxUSE_STREAMS
 
@@ -890,14 +890,14 @@ bool wxImage::HasOption(const wxString& name) const
     return (M_IMGDATA->m_optionNames.Index(name, FALSE) != wxNOT_FOUND);
 }
 
-bool wxImage::LoadFile( const wxString& filename, long type )
+bool wxImage::LoadFile( const wxString& filename, long type, int index )
 {
 #if wxUSE_STREAMS
     if (wxFileExists(filename))
     {
         wxFileInputStream stream(filename);
         wxBufferedInputStream bstream( stream );
-        return LoadFile(bstream, type);
+        return LoadFile(bstream, type, index);
     }
     else
     {
@@ -910,14 +910,14 @@ bool wxImage::LoadFile( const wxString& filename, long type )
 #endif // wxUSE_STREAMS
 }
 
-bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
+bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype, int index )
 {
 #if wxUSE_STREAMS
     if (wxFileExists(filename))
     {
         wxFileInputStream stream(filename);
         wxBufferedInputStream bstream( stream );
-        return LoadFile(bstream, mimetype);
+        return LoadFile(bstream, mimetype, index);
     }
     else
     {
@@ -970,23 +970,72 @@ bool wxImage::CanRead( const wxString &name )
 #endif
 }
 
+int wxImage::GetImagesCount( const wxString &name, long type )
+{
+#if wxUSE_STREAMS
+  wxFileInputStream stream(name);
+  return GetImagesCount(stream, type);
+#else
+  return 0;
+#endif
+}
+
 #if wxUSE_STREAMS
 
 bool wxImage::CanRead( wxInputStream &stream )
 {
-  wxList &list=GetHandlers();
+    wxList &list=GetHandlers();
 
-  for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
+    for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
     {
-      wxImageHandler *handler=(wxImageHandler*)node->GetData();
-      if (handler->CanRead( stream ))
-        return TRUE;
+        wxImageHandler *handler=(wxImageHandler*)node->GetData();
+        if (handler->CanRead( stream ))
+            return TRUE;
     }
 
-  return FALSE;
+    return FALSE;
+}
+
+int wxImage::GetImagesCount( wxInputStream &stream, long type )
+{
+    wxImageHandler *handler;
+
+    if ( type == wxBITMAP_TYPE_ANY )
+    {
+        wxList &list=GetHandlers();
+
+        for (wxList::Node *node = list.GetFirst(); node; node = node->GetNext())
+        {
+             handler=(wxImageHandler*)node->GetData();
+             if ( handler->CanRead(stream) )
+                 return handler->GetImagesCount(stream);
+
+        }
+
+        wxLogWarning(_("No handler found for image type."));
+        return 0;
+    }
+
+    handler = FindHandler(type);
+
+    if ( !handler )
+    {
+        wxLogWarning(_("No image handler for type %d defined."), type);
+        return FALSE;
+    }
+
+    if ( handler->CanRead(stream) )
+    {
+        return handler->GetImagesCount(stream);
+    }
+    else
+    {
+        wxLogError(_("Image file is not of type %d."), type);
+        return 0;
+    }
 }
 
-bool wxImage::LoadFile( wxInputStream& stream, long type )
+bool wxImage::LoadFile( wxInputStream& stream, long type, int index )
 {
     UnRef();
 
@@ -994,15 +1043,15 @@ bool wxImage::LoadFile( wxInputStream& stream, long type )
 
     wxImageHandler *handler;
 
-    if (type==wxBITMAP_TYPE_ANY)
+    if ( type == wxBITMAP_TYPE_ANY )
     {
         wxList &list=GetHandlers();
 
         for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
         {
              handler=(wxImageHandler*)node->GetData();
-             if (handler->CanRead( stream ))
-                 return handler->LoadFile( this, stream );
+             if ( handler->CanRead(stream) )
+                 return handler->LoadFile(this, stream, TRUE/*verbose*/, index);
 
         }
 
@@ -1019,10 +1068,10 @@ bool wxImage::LoadFile( wxInputStream& stream, long type )
         return FALSE;
     }
 
-    return handler->LoadFile( this, stream );
+    return handler->LoadFile(this, stream, TRUE/*verbose*/, index);
 }
 
-bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
+bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int index )
 {
     UnRef();
 
@@ -1037,7 +1086,7 @@ bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
         return FALSE;
     }
 
-    return handler->LoadFile( this, stream );
+    return handler->LoadFile( this, stream, index );
 }
 
 bool wxImage::SaveFile( wxOutputStream& stream, int type )
@@ -1190,7 +1239,7 @@ bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSE
     return FALSE;
 }
 
-int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
+int wxImageHandler::GetImagesCount( wxInputStream& WXUNUSED(stream) )
 {
     return 1;
 }
diff --git a/src/common/imagtiff.cpp b/src/common/imagtiff.cpp
index 4cbf865fad..cf49f4aa82 100644
--- a/src/common/imagtiff.cpp
+++ b/src/common/imagtiff.cpp
@@ -157,6 +157,9 @@ TIFFwxOpen(wxOutputStream &stream, const char* name, const char* mode)
 
 bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbose, int index )
 {
+    if (index == -1)
+        index = 0;
+
     image->Destroy();
 
     TIFF *tif = TIFFwxOpen( stream, "image", "r" );
@@ -268,7 +271,7 @@ bool wxTIFFHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
     return TRUE;
 }
 
-int wxTIFFHandler::GetImageCount( wxInputStream& stream )
+int wxTIFFHandler::GetImagesCount( wxInputStream& stream )
 {
     TIFF *tif = TIFFwxOpen( stream, "image", "r" );
 
-- 
2.45.2