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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
|
/*
/--------------------------------------------------------------------
|
| $Id: PictureDecoder.cpp,v 1.14 2004/09/11 12:41:36 uzadow Exp $
|
| Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/
#include "stdafx.h"
#include "PaintX.h"
#include "PictureDecoder.h"
#include "plBmpEnc.h"
#include "plWinBmp.h"
#include "plpixelformat.h"
/////////////////////////////////////////////////////////////////////////////
// CPictureDecoder
STDMETHODIMP CPictureDecoder::InterfaceSupportsErrorInfo(REFIID riid)
{
static const IID* arr[] =
{
&IID_IPictureDecoder,
};
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
{
if (InlineIsEqualGUID(*arr[i],riid))
return S_OK;
}
return S_FALSE;
}
STDMETHODIMP CPictureDecoder::LoadPicture(BSTR Filename, IUnknown ** ppUnk)
{
HRESULT hResult=S_OK;
PLWinBmp * pWinBmp=new PLWinBmp;
IPictureDisp * pIPictureDisp = NULL;
// Big try..catch wrapper to catch any exceptions before they reach COM.
try
{
USES_CONVERSION;
m_AnyPicDecoder.MakeBmpFromFile(W2A(Filename), pWinBmp);
pIPictureDisp = BitmapToPicture(pWinBmp);
}
catch (PLTextException e)
{
// Handle PaintLib exceptions
hResult = this->CreateErrorInfo(e, IID_IPictureDecoder);
}
// Get rid of the bitmap, if it exists
if (pWinBmp)
{
delete pWinBmp;
pWinBmp = NULL;
}
// We have an error condition
// AND an (allocated) Picture, so we must
// get rid of the Picture Object
if (FAILED(hResult) && pIPictureDisp)
{
(pIPictureDisp)->Release();
(pIPictureDisp)=NULL;
}
else
*ppUnk = pIPictureDisp;
return hResult;
}
STDMETHODIMP CPictureDecoder::LoadResPicture(int ResourceID, IUnknown ** ppUnk)
{
HRESULT hResult=S_OK;
PLWinBmp * pWinBmp=new PLWinBmp;
IPictureDisp * pIPictureDisp = NULL;
// Big try..catch wrapper to catch any exceptions before they reach COM.
try {
try {
m_AnyPicDecoder.MakeBmpFromResource(GetModuleHandle(NULL), ResourceID, pWinBmp);
} catch (PLTextException e) {
// try again, this time with resource type "PAINTX"
m_AnyPicDecoder.MakeBmpFromResource(GetModuleHandle(NULL), ResourceID, pWinBmp, PLPixelFormat::DONTCARE, "PAINTX");
}
pIPictureDisp = BitmapToPicture(pWinBmp);
} catch (PLTextException e) {
// Handle PaintLib exceptions
hResult = this->CreateErrorInfo(e, IID_IPictureDecoder);
}
// Get rid of the bitmap, if it exists
if (pWinBmp) {
delete pWinBmp;
pWinBmp = NULL;
}
// if we have an error condition
// AND a (allocated) picture, we must
// get rid of the picture object
if (FAILED(hResult) && pIPictureDisp)
{
pIPictureDisp->Release();
pIPictureDisp=NULL;
} else {
*ppUnk = pIPictureDisp;
}
return hResult;
}
STDMETHODIMP CPictureDecoder::LoadMemPicture(VARIANT* vByteArray, IUnknown ** ppUnk)
{
HRESULT hResult=S_OK;
PLWinBmp * pWinBmp=new PLWinBmp;
IPictureDisp * pIPictureDisp = NULL;
SAFEARRAY FAR* psa = NULL;
BYTE* ppvData = NULL;
// Big try..catch wrapper to catch any exceptions before they reach COM.
try
{
USES_CONVERSION;
// Type check VARIANT parameter. It should contain a byte array
if (V_VT(vByteArray) != (VT_ARRAY | VT_UI1))
throw PLTextException (PL_ERRINTERNAL,"Type Mismatch in Parameter. Pass a byte array by reference");
psa = V_ARRAY(vByteArray);
// Check dimensions of the array.
if (SafeArrayGetDim(psa) != 1)
throw PLTextException (PL_ERRINTERNAL,"Type Mismatch in Parameter. Pass a one-dimensional array");
long lBound, uBound;
hResult = SafeArrayGetLBound(psa, 1, &lBound);
if (FAILED(hResult)) {
throw PLTextException (PL_ERRINTERNAL,"Could not get lbound of array");
}
hResult = SafeArrayGetUBound(psa, 1, &uBound);
if (FAILED(hResult)) {
throw PLTextException (PL_ERRINTERNAL,"Could not get ubound of array");
}
hResult = SafeArrayAccessData(psa, reinterpret_cast<void HUGEP* FAR*>(&ppvData));
if (FAILED(hResult)) {
throw PLTextException (hResult,"Could not read array");
}
m_AnyPicDecoder.MakeBmpFromMemory(ppvData, uBound-lBound, pWinBmp);
SafeArrayUnaccessData(psa);
ppvData = NULL;
pIPictureDisp = BitmapToPicture(pWinBmp);
}
catch (PLTextException e)
{
// Handle PaintLib exceptions
hResult = this->CreateErrorInfo(e, IID_IPictureDecoder);
}
// unlock array if locked
if (ppvData)
{
SafeArrayUnaccessData(psa);
ppvData = NULL;
}
// Get rid of the bitmap, if it exists
if (pWinBmp)
{
delete pWinBmp;
pWinBmp = NULL;
}
// We have an error condition
// AND an (allocated) Picture, so we must
// get rid of the Picture Object
if (FAILED(hResult) && pIPictureDisp)
{
(pIPictureDisp)->Release();
(pIPictureDisp)=NULL;
}
else
*ppUnk = pIPictureDisp;
return hResult;
}
IPictureDisp * CPictureDecoder::BitmapToPicture(PLBmp* pBmp)
{
// 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->GetMemUsed();
bufsize = bufsize < 20000 ? bufsize + 4096 : int(1.2 * bufsize);
PLStreamSink MyStreamSink;
MyStreamSink.Open( bufsize );
// Now save BMP encoded to the stream
PLBmpEncoder MyBmpEncoder;
MyBmpEncoder.SaveBmp( pBmp, &MyStreamSink );
MyStreamSink.Close ();
// Create a COM Picture Object from this stream
IPictureDisp * pIPictureDisp = NULL;
HRESULT hResult = OleLoadPicture(MyStreamSink.GetIStream(),0,false, IID_IPictureDisp, (void**)&pIPictureDisp);
// If the picture is ok, then call IPicture::PictureChanged
if (SUCCEEDED(hResult))
hResult = SignalPictureChanged(pIPictureDisp);
else
throw PLTextException (PL_ERRINTERNAL,"OleLoadPicture failed.\n");
return pIPictureDisp;
}
HRESULT CPictureDecoder::SignalPictureChanged(IUnknown * pUnknown)
{
HRESULT hResult = S_OK;
// Get Picture Interface (only the Picture Interface has the PictureChanged method)
IPicture* pPicture;
if(SUCCEEDED(pUnknown->QueryInterface(IID_IPicture, (LPVOID *) &pPicture)))
{
hResult = pPicture->PictureChanged();
// release the reference
pPicture->Release();
};
return hResult;
}
HRESULT CPictureDecoder::CreateErrorInfo(PLTextException TextException,REFIID riidSource)
{
// create an error info object
ICreateErrorInfo * pcerrinfo;
HRESULT hr = ::CreateErrorInfo(&pcerrinfo);
// if we succeeded
if (SUCCEEDED(hr))
{
int ErrCode = TextException.GetCode();
switch(ErrCode)
{
// Map PaintLib Error Codes to Win32 Codes to COM HRESULT Codes
case PL_ERRPATH_NOT_FOUND:
hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_FILE_NOT_FOUND);
break;
case PL_ERRFILE_NOT_FOUND:
hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_FILE_NOT_FOUND);
break;
case PL_ERRACCESS_DENIED:
hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32, ERROR_ACCESS_DENIED);
break;
// Map from PaintLib Error Codes to COM Interface-Independent Error Codes
case PL_ERRNO_MEMORY:
hr = E_OUTOFMEMORY;
break;
// Map From PaintLib Error Codes to COM Interface-Dependent HRESULT Codes
case PL_ERRINTERNAL:
case PL_ERRFORMAT_NOT_SUPPORTED:
case PL_ERRWRONG_SIGNATURE:
case PL_ERRFORMAT_UNKNOWN:
case PL_ERRUNKNOWN_FILE_TYPE:
case PL_ERREND_OF_FILE:
default:
hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_ITF, ErrCode);
}
// Set up ErrInfo object
pcerrinfo->SetGUID(riidSource);
CComBSTR bstrSource(L"PaintX");
pcerrinfo->SetSource(bstrSource);
CComBSTR bstrDescription(TextException);
pcerrinfo->SetDescription(bstrDescription);
// QI for the IErrorInfo interface
IErrorInfo * perrinfo;
if(SUCCEEDED(pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID *) &perrinfo)))
{
// set the error info object
::SetErrorInfo(0, perrinfo);
// release the reference
perrinfo->Release();
}
// release the reference
pcerrinfo->Release();
}
// return the error value
return hr;
}
/*
/--------------------------------------------------------------------
|
| $Log: PictureDecoder.cpp,v $
| Revision 1.14 2004/09/11 12:41:36 uzadow
| removed plstdpch.h
|
| Revision 1.13 2003/03/18 22:44:29 mskinner
| added LoadMemPicture\nadded IPictureDecoder2
|
| Revision 1.12 2002/07/11 19:47:11 mskinner
| replaced _bstr_t with CComBSTR
|
| Revision 1.11 2002/03/03 16:29:56 uzadow
| Re-added BPPWanted.
|
| Revision 1.10 2001/10/21 17:12:40 uzadow
| Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
|
| Revision 1.9 2001/09/16 19:03:23 uzadow
| Added global name prefix PL, changed most filenames.
|
| Revision 1.8 2000/09/29 20:00:51 Administrator
| VBPaintX added
|
| Revision 1.8 2000/09/26 14:13:49 Martin Skinner
| Added ResType "PAINTX" to LoadResPicture.
|
| Revision 1.7 2000/09/01 14:13:49 Administrator
| Removed MFC from paintX, added MSCV paintX sample.
|
| Revision 1.6 2000/01/16 20:43:17 anonymous
| Removed MFC dependencies
|
| Revision 1.5 2000/01/10 23:53:00 Ulrich von Zadow
| Changed formatting & removed tabs.
|
| Revision 1.4 1999/12/31 18:13:47 Ulrich von Zadow
| Fixed error handling.
|
|
\--------------------------------------------------------------------
*/
|