| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: imaggif.cpp |
| 3 | // Purpose: wxGIFHandler |
| 4 | // Author: Vaclav Slavik & Guillermo Rodriguez Garcia |
| 5 | // RCS-ID: $Id$ |
| 6 | // Copyright: (c) 1999 Vaclav Slavik & Guillermo Rodriguez Garcia |
| 7 | // Licence: wxWindows licence |
| 8 | ///////////////////////////////////////////////////////////////////////////// |
| 9 | |
| 10 | // For compilers that support precompilation, includes "wx.h". |
| 11 | #include "wx/wxprec.h" |
| 12 | |
| 13 | #ifdef __BORLANDC__ |
| 14 | # pragma hdrstop |
| 15 | #endif |
| 16 | |
| 17 | #ifndef WX_PRECOMP |
| 18 | # include "wx/defs.h" |
| 19 | #endif |
| 20 | |
| 21 | #if wxUSE_IMAGE && wxUSE_GIF |
| 22 | |
| 23 | #include "wx/imaggif.h" |
| 24 | #include "wx/gifdecod.h" |
| 25 | #include "wx/wfstream.h" |
| 26 | #include "wx/log.h" |
| 27 | #include "wx/intl.h" |
| 28 | |
| 29 | IMPLEMENT_DYNAMIC_CLASS(wxGIFHandler,wxImageHandler) |
| 30 | |
| 31 | //----------------------------------------------------------------------------- |
| 32 | // wxGIFHandler |
| 33 | //----------------------------------------------------------------------------- |
| 34 | |
| 35 | #if wxUSE_STREAMS |
| 36 | |
| 37 | bool wxGIFHandler::LoadFile(wxImage *image, wxInputStream& stream, |
| 38 | bool verbose, int index) |
| 39 | { |
| 40 | wxGIFDecoder *decod; |
| 41 | int error; |
| 42 | bool ok = true; |
| 43 | |
| 44 | // image->Destroy(); |
| 45 | decod = new wxGIFDecoder(&stream, true); |
| 46 | error = decod->ReadGIF(); |
| 47 | |
| 48 | if ((error != wxGIF_OK) && (error != wxGIF_TRUNCATED)) |
| 49 | { |
| 50 | if (verbose) |
| 51 | { |
| 52 | switch (error) |
| 53 | { |
| 54 | case wxGIF_INVFORMAT: |
| 55 | wxLogError(_("GIF: error in GIF image format.")); |
| 56 | break; |
| 57 | case wxGIF_MEMERR: |
| 58 | wxLogError(_("GIF: not enough memory.")); |
| 59 | break; |
| 60 | default: |
| 61 | wxLogError(_("GIF: unknown error!!!")); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | delete decod; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if ((error == wxGIF_TRUNCATED) && verbose) |
| 70 | { |
| 71 | wxLogError(_("GIF: data stream seems to be truncated.")); |
| 72 | /* go on; image data is OK */ |
| 73 | } |
| 74 | |
| 75 | if (index != -1) |
| 76 | { |
| 77 | // We're already on index = 0 by default. So no need |
| 78 | // to call GoFrame(0) then. On top of that GoFrame doesn't |
| 79 | // accept an index of 0. (Instead GoFirstFrame() should be used) |
| 80 | // Also if the gif image has only one frame, calling GoFrame(0) |
| 81 | // fails because GoFrame() only works with gif animations. |
| 82 | // (It fails if IsAnimation() returns false) |
| 83 | // All valid reasons to NOT call GoFrame when index equals 0. |
| 84 | if (index != 0) |
| 85 | { |
| 86 | ok = decod->GoFrame(index); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (ok) |
| 91 | { |
| 92 | ok = decod->ConvertToImage(image); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | wxLogError(_("GIF: Invalid gif index.")); |
| 97 | } |
| 98 | |
| 99 | delete decod; |
| 100 | |
| 101 | return ok; |
| 102 | } |
| 103 | |
| 104 | bool wxGIFHandler::SaveFile( wxImage * WXUNUSED(image), |
| 105 | wxOutputStream& WXUNUSED(stream), bool verbose ) |
| 106 | { |
| 107 | if (verbose) |
| 108 | wxLogDebug(wxT("GIF: the handler is read-only!!")); |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | bool wxGIFHandler::DoCanRead( wxInputStream& stream ) |
| 114 | { |
| 115 | wxGIFDecoder decod(&stream); |
| 116 | return decod.CanRead(); |
| 117 | } |
| 118 | |
| 119 | #endif // wxUSE_STREAMS |
| 120 | |
| 121 | #endif // wxUSE_GIF |