Skip to content
Snippets Groups Projects
Forked from Alchemy Viewer / Alchemy Viewer
9051 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
llimagej2coj.cpp 19.46 KiB
/** 
 * @file llimagej2coj.cpp
 * @brief This is an implementation of JPEG2000 encode/decode using OpenJPEG.
 *
 * $LicenseInfo:firstyear=2006&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"
#include "llimagej2coj.h"

// this is defined so that we get static linking.
#include "openjpeg.h"

#include "lltimer.h"

struct LLJp2StreamReader 
{
	LLJp2StreamReader(LLImageJ2C* pImage) : m_pImage(pImage), m_Position(0) { }

	static OPJ_SIZE_T readStream(void* pBufferOut, OPJ_SIZE_T szBufferOut, void* pUserData)
	{
		LLJp2StreamReader* pStream = static_cast<LLJp2StreamReader*>(pUserData);
		if (!pBufferOut || !pStream || !pStream->m_pImage)
			return (OPJ_SIZE_T)-1;

		OPJ_SIZE_T szBufferRead = llmin(szBufferOut, pStream->m_pImage->getDataSize() - pStream->m_Position);
		if (!szBufferRead)
			return (OPJ_SIZE_T)-1;

		memcpy(pBufferOut, pStream->m_pImage->getData() + pStream->m_Position, szBufferRead);
		pStream->m_Position += szBufferRead;
		return szBufferRead;
	}

	static OPJ_OFF_T skipStream(OPJ_OFF_T bufferOffset, void* pUserData)
	{
		LLJp2StreamReader* pStream = static_cast<LLJp2StreamReader*>(pUserData);
		if (!pStream || !pStream->m_pImage)
			return (OPJ_OFF_T)-1;

		if (bufferOffset < 0)
		{
			// Skipping backward
			if (pStream->m_Position == 0)
				return (OPJ_OFF_T)-1;              // Already at the start of the stream
			else if (pStream->m_Position + bufferOffset < 0)
				bufferOffset = -(OPJ_OFF_T)pStream->m_Position; // Don't underflow
		}
		else
		{
			// Skipping forward