## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# cstool/debugimagewriter.h

Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2004 by Jorrit Tyberghein
    00003               (C) 2004 by Frank Richter
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_CSTOOL_DEBUGIMAGEWRITER_H__
    00021 #define __CS_CSTOOL_DEBUGIMAGEWRITER_H__
    00022
    00027 #include "csutil/csstring.h"
    00028 #include "csutil/scf.h"
    00029 #include "igraphic/imageio.h"
    00030 #include "iutil/databuff.h"
    00031 #include "iutil/vfs.h"
    00032 #include "ivaria/reporter.h"
    00033
    00038 class CS_CRYSTALSPACE_EXPORT csDebugImageWriter
    00039 {
    00040   static void Report (int severity, const char* msg, ...)
    00041   {
    00042 #ifdef CS_DEBUG
    00043     va_list arg;
    00044     va_start (arg, msg);
    00045     csReportV (iSCF::SCF->object_reg, severity, "crystalspace.debugimagewriter",
    00046       msg, arg);
    00047     va_end (arg);
    00048 #endif
    00049   }
    00050 public:
    00060   static void DebugImageWrite (iImage* image, const char* filename, ...)
    00061   {
    00062   #ifdef CS_DEBUG
    00063     CS_ASSERT(iSCF::SCF->object_reg);
    00064     csRef<iImageIO> imgsaver =
    00065       CS_QUERY_REGISTRY (iSCF::SCF->object_reg, iImageIO);
    00066     if (!imgsaver)
    00067     {
    00068       Report (CS_REPORTER_SEVERITY_ERROR, "No iImageIO");
    00069       return;
    00070     }
    00071     csRef<iVFS> vfs =
    00072       CS_QUERY_REGISTRY (iSCF::SCF->object_reg, iVFS);
    00073     if (!vfs)
    00074     {
    00075       Report (CS_REPORTER_SEVERITY_ERROR, "No iVFS");
    00076       return;
    00077     }
    00078
    00079     csString finalFilename;
    00080     va_list arg;
    00081     va_start (arg, filename);
    00082     finalFilename.FormatV (filename, arg);
    00083     va_end (arg);
    00084
    00085     csRef<iDataBuffer> buf = imgsaver->Save (image, "image/png");
    00086     if (!buf)
    00087     {
    00088       Report (CS_REPORTER_SEVERITY_ERROR, "Error saving image");
    00089       return;
    00090     }
    00091     vfs->PushDir ();
    00092     vfs->ChDir ("/tmp");
    00093     bool written = vfs->WriteFile (finalFilename, (char*)buf->GetInt8 (),
    00094       buf->GetSize ());
    00095     vfs->PopDir ();
    00096     if (!written)
    00097     {
    00098       Report (CS_REPORTER_SEVERITY_ERROR, "Could not write to %s",
    00099         finalFilename.GetData ());
    00100     }
    00101   #else
    00102     // Make -Wall happy
    00103     (void)image;
    00104     (void)filename;
    00105   #endif
    00106   }
    00107 };
    00108
    00109 #endif // __CS_CSTOOL_DEBUGIMAGEWRITER_H__