📄 pxpngdec.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: pxpngdec.cpp,v 1.2.24.1 2004/07/09 01:52:12 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** */// include#include "hxtypes.h"#include "hxwintyp.h"#include "hxresult.h"#include "hxcom.h"#include "hxcomm.h"#include "ihxpckts.h"#include "hxvsurf.h"// pnmisc#include "baseobj.h"#include "netbyte.h"// pxcomlib// libpng#include "png.h"// pxpnglib#include "pxpngdec.h"// pndebug#include "hxheap.h"#ifdef _DEBUG#undef HX_THIS_FILE static char HX_THIS_FILE[] = __FILE__;#endif#include "errdbg.h"#include "hxperf.h"PXPNGDecode::PXPNGDecode(){ m_lRefCount = 0; Reset();}PXPNGDecode::~PXPNGDecode(){ Deallocate();}void PXPNGDecode::Reset(){ m_pPNGStruct = NULL; m_pPNGInfo = NULL; m_pPNGEndInfo = NULL; m_pOutputBuffer = NULL; m_ppImageRow = NULL; m_bSingleBufferIO = FALSE; m_bDupFirstBuffer = FALSE; m_bFirstDecompress = TRUE; m_bDeferFirstBuffer = FALSE; m_pFirstBuffer = NULL; m_ulFirstBufferOffset = 0; m_bFinished = FALSE; m_ulLastSeqNum = 0; m_bValid = TRUE;}void PXPNGDecode::Deallocate(){ DeallocateErrorHandling(m_pPNGStruct); DeallocateIOHandling(m_pPNGStruct); if (m_pPNGStruct) { png_destroy_read_struct(&m_pPNGStruct, &m_pPNGInfo, &m_pPNGEndInfo); } HX_RELEASE(m_pOutputBuffer); HX_VECTOR_DELETE(m_ppImageRow); Reset();}STDMETHODIMP PXPNGDecode::QueryInterface(REFIID riid, void** ppvObj){ HX_RESULT retVal = HXR_OK; if (IsEqualIID(riid, IID_IUnknown)) { AddRef(); *ppvObj = (IUnknown*) this; } else { *ppvObj = NULL; retVal = HXR_NOINTERFACE; } return retVal;}STDMETHODIMP_(UINT32) PXPNGDecode::AddRef(){ return InterlockedIncrement(&m_lRefCount);}STDMETHODIMP_(UINT32) PXPNGDecode::Release(){ if (InterlockedDecrement(&m_lRefCount) > 0) return m_lRefCount; delete this; return 0;}BOOL PXPNGDecode::ValidInputData(IHXBuffer* pBuffer){ BOOL bRet = FALSE; if (pBuffer) { if (pBuffer->GetSize() >= 8) { BYTE* pBuf = pBuffer->GetBuffer(); if (pBuf) { UINT32 ulSig0 = (pBuf[0] << 24) | (pBuf[1] << 16) | (pBuf[2] << 8) | (pBuf[3]); UINT32 ulSig1 = (pBuf[4] << 24) | (pBuf[5] << 16) | (pBuf[6] << 8) | (pBuf[7]); if (ulSig0 == kSig0 && ulSig1 == kSig1) { bRet = TRUE; } } } } return bRet;}HX_RESULT PXPNGDecode::Init(IUnknown* pContext, IHXBuffer* pBuffer, BOOL bDupFirstBuffer){ HX_LOG_BLOCK( "PXPNGDecode::Init" ); HX_RESULT retVal = HXR_FAIL; if (pContext && pBuffer) { // Clear out and reset everything Deallocate(); // Save member m_bDupFirstBuffer = bDupFirstBuffer; // Init the first decompress flag m_bFirstDecompress = TRUE; // Init the finished flag m_bFinished = FALSE; // Create a PXUserError struct PXUserError* pUserError = new PXUserError; if (pUserError) { // Init the members of the struct pUserError->m_pContext = pContext; pUserError->m_pContext->AddRef(); pUserError->m_pErrorStr = NULL; // Create the read struct m_pPNGStruct = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp) pUserError, HandleError, HandleWarning); if (m_pPNGStruct) { // Set the error return jump if (setjmp(m_pPNGStruct->jmpbuf)) { // If we get here, then libpng threw an error and // we longjmp'd back here. return HXR_FAIL; } // Create the info struct m_pPNGInfo = png_create_info_struct(m_pPNGStruct); if (m_pPNGInfo) { // Create the end info struct m_pPNGEndInfo = png_create_info_struct(m_pPNGStruct); if (m_pPNGEndInfo) { // If we have a complete IEND chunk, then we have // the entire file. Therefore, we'll process this as a // single buffer read. If we dont' have a complete IEND // chunk, then we will attempt to process it as // a progressive read. if (IsCompleteChunkPresent(pBuffer, kIEND)) { // Set the single-buffer-IO flag m_bSingleBufferIO = TRUE; // First we need to create a single-buffer read struct PXUserIOSingle* pUserIO = new PXUserIOSingle; if (pUserIO) { // Set the members of the struct pUserIO->m_pBuffer = pBuffer; pUserIO->m_ulOffset = 0; pUserIO->m_pBuffer->AddRef(); // Set up the single-buffer callback png_set_read_fn(m_pPNGStruct, (png_voidp) pUserIO, (png_rw_ptr) SingleBufferRead); // Now we can call to read all the way up // to (but not including) the actual image data // After this call, then all the accessor methods // (GetWidth(), GetHeight(), etc.) should be valid png_read_info(m_pPNGStruct, m_pPNGInfo); // Clear the return value retVal = HXR_OK; } } else { // Clear the single-buffer-IO flag m_bSingleBufferIO = FALSE; // We require that at least the first kMinIDATBytes // of the IDAT chunk to be present (just the length and the // IDAT chunk type) in the first buffer. (This forces // libpng to make the info callback after the first // call to png_process_data(), which we need if we are // going to be able to provide width/height info to // the app). If we have more than the first kMinIDATBytes // in the first buffer, that's OK - we will only supply // up to the first 8 bytes to the first call to // png_process_data() and defer the processing of the rest // of the first buffer. UINT32 ulIDATBytes = 0; UINT32 ulIDATOffset = 0; BOOL bComplete = FALSE; BOOL bIDATPresent = IsChunkPresent(pBuffer, kIDAT, ulIDATOffset, ulIDATBytes, bComplete); if (bIDATPresent && ulIDATBytes >= kMinIDATBytes) { // Create a progressive read struct PXUserIOProgressive* pUserIO = new PXUserIOProgressive; if (pUserIO) { // Set the member pUserIO->m_ulAppState = kAppStateConstructed; pUserIO->m_ulDataState = kDataStateConstructed; pUserIO->m_ulNumRows = 0; pUserIO->m_ppImageRow = NULL; // Set up the progressive read callbacks png_set_progressive_read_fn(m_pPNGStruct, (png_voidp) pUserIO, InfoCallback, RowCallback, EndCallback); // Now if we have MORE than kMinIDATBytes in the // first buffer, then that's OK - we will defer // the processing of those bytes until later. // However, we must save the first buffer and // the offset and set a flag saying we need to // process the first buffer. UINT32 ulBytesToProcessNow = 0; if (ulIDATBytes > kMinIDATBytes) { // Save these members so we can process the // rest of the first buffer later m_bDeferFirstBuffer = TRUE; m_ulFirstBufferOffset = ulIDATOffset + kMinIDATBytes; HX_RELEASE(m_pFirstBuffer); m_pFirstBuffer = pBuffer; m_pFirstBuffer->AddRef(); // Compute the number of bytes to process // right now ulBytesToProcessNow = m_ulFirstBufferOffset; } else { // We had exactly kMinIDATBytes in the first buffer, // so there's no need to defer processing of // any part of the buffer m_bDeferFirstBuffer = FALSE; ulBytesToProcessNow = pBuffer->GetSize(); }#ifdef XXXMEH_DEBUG_LOG DEBUG_OUTF("c:\\pxpnglib.log", (s, "Init(): calling png_process_data()\n"));#endif // Process the first buffer png_process_data(m_pPNGStruct, m_pPNGInfo, (png_bytep) pBuffer->GetBuffer(), (png_size_t) ulBytesToProcessNow); // Set the app state pUserIO->m_ulAppState = kAppStateInit;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -