gifimage.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 966 行 · 第 1/3 页

CPP
966
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: gifimage.cpp,v 1.2.24.1 2004/07/09 01:54:29 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 ***** */

// system
#include <hlxclib/memory.h>
#include <string.h>

// include
#include "hxtypes.h"
#include "hxcom.h"

// pnmisc
#include "baseobj.h"
#include "unkimp.h"

// pxcomlib
#include "pxtransp.h"

// pxgiflib
#include "gifimage.h"
#include "gifcodec.h"

// pndebug
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE		
static char HX_THIS_FILE[] = __FILE__;
#endif

CGIFImage::CGIFImage()
{
    Reset();
};

CGIFImage::~CGIFImage()
{
    TermDecompress();
};

void CGIFImage::Reset()
{
    m_cID.m_ulImageLeft                 = 0;
    m_cID.m_ulImageTop                  = 0;
    m_cID.m_ulImageWidth                = 0;
    m_cID.m_ulImageHeight               = 0;
    m_cID.m_bLocalColorTablePresent     = FALSE;
    m_cID.m_bInterlaced                 = FALSE;
    m_cID.m_bLocalColorsSorted          = FALSE;
    m_cID.m_ulLocalColorTableBits       = 0;
    m_cID.m_ulLocalColorTableNumEntries = 0;
    m_cGCE.m_ulDisposalMethod           = 0;
    m_cGCE.m_bUserInputExpected         = FALSE;
    m_cGCE.m_bTransparentIndexGiven     = FALSE;
    m_cGCE.m_ulDelayTime                = 0;
    m_cGCE.m_ulTransparentColorIndex    = 0;
    m_bGCEPresent                       = FALSE;
    m_pucLocalColorMap                  = NULL;
    m_bGlobalColorMapPresent            = FALSE;
    m_ulNumGlobalColors                 = 0;
    m_pucGlobalColorMap                 = NULL;
    m_pOutputBuffer                     = NULL;
    m_ulOutputBufferSize                = 0;
    m_ulCurX                            = 0;
    m_ulCurY                            = 0;
    m_ulPass                            = 0;
    m_pOutPtr                           = NULL;
    m_ulState                           = kStateConstructed;
    m_pLZWCodec                         = NULL;
    m_bValid                            = TRUE;
}

void CGIFImage::TermDecompress()
{
    /* Free the local color map */
    if (m_pucLocalColorMap)
    {
        delete [] m_pucLocalColorMap;
        m_pucLocalColorMap = NULL;
    }
    /* Free the output buffer */
    if (m_pOutputBuffer)
    {
        delete [] m_pOutputBuffer;
        m_pOutputBuffer = NULL;
    }
    /* Delete the LZW Codec */
    HX_DELETE(m_pLZWCodec);
    /* Reset all member variables */
    Reset();
}

void CGIFImage::SetGlobalColorMap(UINT32 ulNumColors, BYTE *pMap)
{
    if (ulNumColors > 0 && pMap != NULL)
    {
        m_bGlobalColorMapPresent = TRUE;
        m_ulNumGlobalColors      = ulNumColors;
        m_pucGlobalColorMap      = pMap;
    }
    else
    {
        m_bGlobalColorMapPresent = FALSE;
        m_ulNumGlobalColors      = 0;
        m_pucGlobalColorMap      = NULL;
    }
}

HX_RESULT CGIFImage::SetCompressedBufferSize(UINT32 ulSize)
{
    // Check for input error
    if (m_pLZWCodec == NULL)
    {
        return HXR_INVALID_PARAMETER;
    }

    return m_pLZWCodec->SetCompressedBufferSize((INT32) ulSize);
}

void CGIFImage::ParseImageDescriptor(BYTE *pBuffer, ImageDescriptor &cID)
{
    cID.m_ulImageLeft                 = (pBuffer[1] << 8) | pBuffer[0];
    cID.m_ulImageTop                  = (pBuffer[3] << 8) | pBuffer[2];
    cID.m_ulImageWidth                = (pBuffer[5] << 8) | pBuffer[4];
    cID.m_ulImageHeight               = (pBuffer[7] << 8) | pBuffer[6];
    cID.m_bLocalColorTablePresent     = (pBuffer[8] & 0x80 ? TRUE : FALSE);
    cID.m_bInterlaced                 = (pBuffer[8] & 0x40 ? TRUE : FALSE);
    cID.m_bLocalColorsSorted          = (pBuffer[8] & 0x20 ? TRUE : FALSE);
    cID.m_ulLocalColorTableBits       = (pBuffer[8] & 0x07) + 1;
    cID.m_ulLocalColorTableNumEntries = 1 << cID.m_ulLocalColorTableBits;
}

void CGIFImage::ParseGraphicControlExtension(BYTE *pBuffer, GraphicControlExtension &cGCE)
{
    cGCE.m_ulDisposalMethod        =  (pBuffer[0] & 0x1C) >> 2;
    cGCE.m_bUserInputExpected      = ((pBuffer[0] & 0x02) ? TRUE : FALSE);
    cGCE.m_bTransparentIndexGiven  = ((pBuffer[0] & 0x01) ? TRUE : FALSE);
    cGCE.m_ulDelayTime             =  (pBuffer[2] << 8) | pBuffer[1];
    cGCE.m_ulTransparentColorIndex =   pBuffer[3];
}

HX_RESULT CGIFImage::InitDecompress(BYTE *pBuffer, UINT32 ulLen)
{
    /* Check for input error conditions */
    if (pBuffer == NULL || ulLen == 0)
    {
        return HXR_INVALID_PARAMETER;
    }

    /* Check the state */
    if (m_ulState != kStateConstructed)
    {
        return HXR_UNEXPECTED;
    }

    /*
     * We're either right at an Image Descriptor or a Graphic Control Extension.
     * If we're at an 
     */
    BYTE *pBufPtr = pBuffer;
    if (*pBufPtr == CGIFCodec::kExtension)
    {
        /* Advance the pointer to the extension type */
        pBufPtr++;

        /* Verify the extension is a GCE */
        UINT32 ulExtension = *pBufPtr++;
        if (ulExtension != CGIFCodec::kGraphicControlExtension)
        {
            return HXR_UNEXPECTED;
        }

        /* Note the presence of the GCE - this means we're GIF89a */
        m_bGCEPresent = TRUE;

        UINT32 ulBlockSize;
        do
        {
            /* Get the block size */
            ulBlockSize = *pBufPtr++;

            /* Initialize the GCE */
            if (ulBlockSize >= 4)
            {
                ParseGraphicControlExtension(pBufPtr, m_cGCE);
            }

            /* Advance by the block size */
            pBufPtr += ulBlockSize;
        }
        while (ulBlockSize > 0);
    }

    // Skip forward to the image descriptor
//    if (*pBuffer != CGIFCodec::kImageDescriptor)
//    {
//        return HXR_UNEXPECTED;
//    }
    while (*pBufPtr != CGIFCodec::kImageDescriptor && pBufPtr < pBuffer + ulLen)
    {
        // We assume here that these can only be extensions
        pBufPtr += 2;
        CGIFCodec::SkipBlocks(pBufPtr);
    }
    if (pBufPtr >= pBuffer + ulLen)
    {
        return HXR_FAIL;
    }

    /* Advance the pointer */
    pBufPtr++;

    /* Parse the Image Descriptor */
    ParseImageDescriptor(pBufPtr, m_cID);
    pBufPtr += 9;

    /* Get the local colormap (if present) */
    if (m_cID.m_bLocalColorTablePresent == TRUE)
    {
        /* Allocate memory for the local color table */
        if (m_pucLocalColorMap)
        {
            delete [] m_pucLocalColorMap;
            m_pucLocalColorMap = NULL;
        }
        UINT32 ulNumBytes  = m_cID.m_ulLocalColorTableNumEntries * 3;
        m_pucLocalColorMap = new BYTE [ulNumBytes];
        if (!m_pucLocalColorMap)
        {
            return HXR_OUTOFMEMORY;
        }

        /* Read in the local color table */
        memcpy(m_pucLocalColorMap, pBufPtr, ulNumBytes); /* Flawfinder: ignore */

        /* Advance the pointer */
        pBufPtr += ulNumBytes;
    }

    /* Allocate an output buffer */
    if (m_pOutputBuffer)
    {
        delete [] m_pOutputBuffer;
        m_pOutputBuffer = NULL;
    }
    m_ulOutputBufferSize = m_cID.m_ulImageWidth * m_cID.m_ulImageHeight;
    m_pOutputBuffer      = new BYTE [m_ulOutputBufferSize];
    if (!m_pOutputBuffer)
    {
        if (m_pucLocalColorMap)
        {
            delete [] m_pucLocalColorMap;
            m_pucLocalColorMap = NULL;
        }
        return HXR_OUTOFMEMORY;
    }

    /* Clear the output buffer */
    memset(m_pOutputBuffer, 0, m_ulOutputBufferSize);

    /* Allocate an LZW Codec */
    HX_DELETE(m_pLZWCodec);
    m_pLZWCodec = new LZWCodec();
    if (!m_pLZWCodec)
    {
        if (m_pucLocalColorMap)
        {
            delete [] m_pucLocalColorMap;
            m_pucLocalColorMap = NULL;
        }
        if (m_pOutputBuffer)
        {
            delete [] m_pOutputBuffer;
            m_pOutputBuffer = NULL;
        }
        return HXR_OUTOFMEMORY;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?