1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
|
| $Id: plpicenc.cpp,v 1.10 2004/11/09 15:55:06 artcom Exp $
| Generic Picture Encoder Class
|
| Abstract base class to dump picture data to memory or file.
| Classes derived from this class implement concrete encoders
| for specific file formats.
|
| Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/
#include "plpicenc.h"
#include "plfilesink.h"
#include "plexcept.h"
// only for the tracing facility. This really shouldn't be here.
#include "plpicdec.h"
#include <errno.h>
PLPicEncoder::PLPicEncoder()
: PLObject()
// Creates an encoder
{}
PLPicEncoder::~PLPicEncoder()
{}
// Encodes a picture by creating a file data sink and
// calling SaveBmp with this data sink.
void PLPicEncoder::MakeFileFromBmp (const char * pszFName, PLBmpBase * pBmp)
{
PLFileSink FileSink;
int err;
char sz[1024];
sprintf (sz, "--- Encoding file %s. ---\n", pszFName);
Trace (1, sz);
// We allocate a buffer large enough to hold the raw bitmap
// plus some overhead.In most cases this should be enough to
// hold the uncompressed data in any format, plus headers, etc...
// Some "pathological" cases however may end up with a CODEC
// producing more data than the uncompressed version!
int bufsize = pBmp->GetWidth()*pBmp->GetHeight()*(pBmp->GetBitsPerPixel()/8) + 65536;
/* This worked for years, but it's probably not enough for exif.
int bufsize = pBmp->GetMemUsed();
bufsize = bufsize < 20000 ? bufsize + 4096 : int(1.2 * bufsize);
*/
err = FileSink.Open( pszFName, bufsize );
if (err)
{
sprintf (sz, "Opening %s failed: %s", pszFName, strerror(errno));
raiseError (err, sz);
}
SaveBmp ( pBmp, &FileSink );
FileSink.Close ();
}
#ifdef _WIN32
void PLPicEncoder::MakeFileFromBmpW (const wchar_t * pszwFName, PLBmpBase * pBmp)
{
PLFileSink FileSink;
int err;
char sz[1024];
char sz2[900];
// First calculate how much room the result will take up
int neededSize = ::WideCharToMultiByte(CP_ACP,
0,
pszwFName,
-1,
NULL,
0,
NULL,
NULL);
if (neededSize < 900){ // Prevent overflow of our buffer
::WideCharToMultiByte(CP_ACP,
0,
pszwFName,
-1,
sz2,
900,
NULL,
NULL);
}
else{
strcpy(sz2,"{{Filename too long}}");
}
sprintf (sz, "--- Encoding file %s. ---\n", sz2);
Trace (1, sz);
// We allocate a buffer large enough to hold the raw bitmap
// plus some overhead.In most cases this should be enough to
// hold the uncompressed data in any format, plus headers, etc...
// Some "pathological" cases however may end up with a CODEC
// producing more data than the uncompressed version!
int bufsize = pBmp->GetWidth()*pBmp->GetHeight()*(pBmp->GetBitsPerPixel()/8) + 65536;
err = FileSink.OpenW( pszwFName, bufsize );
if (err)
{
sprintf (sz, "Opening %s failed: %s", sz2, strerror(errno));
raiseError (err, sz);
}
SaveBmp ( pBmp, &FileSink );
FileSink.Close ();
}
#endif
// Encodes a picture by getting the encoded data from pDataSrc.
// Saves the results to pDataSrc
// Actually, a wrapper so thin around "DoEncode", you could see through...
void PLPicEncoder::SaveBmp (PLBmpBase* pBmp, PLDataSink* pDataSnk)
{
DoEncode( pBmp, pDataSnk );
}
////////////////////
// As long as the tracing code lives in the base decoder,
// we'll just forward everything to it without bothering the user
void PLPicEncoder::SetTraceConfig( int Level, char * pszFName )
{
PLPicDecoder::SetTraceConfig( Level, pszFName );
}
void PLPicEncoder::raiseError( int Code, char * pszErr )
{
PLPicDecoder::raiseError( Code, pszErr );
}
void PLPicEncoder::Trace( int TraceLevel, const char * pszMessage )
{
PLPicDecoder::Trace( TraceLevel, pszMessage );
}
/*
/--------------------------------------------------------------------
|
| $Log: plpicenc.cpp,v $
| Revision 1.10 2004/11/09 15:55:06 artcom
| changed #ifdef _WINDOWS to #ifdef _WIN32 (since this is a macro that actually is predefined on win32 platforms)
|
| Revision 1.9 2004/09/11 12:41:35 uzadow
| removed plstdpch.h
|
| Revision 1.8 2004/06/19 18:16:33 uzadow
| Documentation update
|
| Revision 1.7 2003/08/03 12:03:20 uzadow
| Added unicode support; fixed some header includes.
|
| Revision 1.6 2003/04/19 19:03:52 uzadow
| Exif save (windows)
|
| Revision 1.5 2002/08/05 20:43:12 uzadow
| cygwin compatibility changes
|
| Revision 1.4 2002/08/04 20:08:01 uzadow
| Added PLBmpInfo class, ability to extract metainformation from images without loading the whole image and proper greyscale support.
|
| Revision 1.3 2002/02/24 13:00:24 uzadow
| Documentation update; removed buggy PLFilterRotate.
|
| Revision 1.2 2001/10/06 20:44:45 uzadow
| Linux compatibility
|
| Revision 1.1 2001/09/16 19:03:22 uzadow
| Added global name prefix PL, changed most filenames.
|
| Revision 1.8 2001/02/04 14:31:52 uzadow
| Member initialization list cleanup (Erik Hoffmann).
|
| Revision 1.7 2001/02/04 14:07:24 uzadow
| Changed max. filename length.
|
| Revision 1.6 2000/01/16 20:43:14 anonymous
| Removed MFC dependencies
|
| Revision 1.5 2000/01/10 23:52:59 Ulrich von Zadow
| Changed formatting & removed tabs.
|
| Revision 1.4 2000/01/08 15:51:30 Ulrich von Zadow
| Misc. modifications to png encoder.
|
| Revision 1.3 1999/10/03 18:50:51 Ulrich von Zadow
| Added automatic logging of changes.
|
|
--------------------------------------------------------------------
*/
|