TexConv/CMP_CompressonatorLib/Buffer/CodecBuffer_R16.cpp

249 lines
7.9 KiB
C++
Raw Normal View History

2020-07-31 11:31:32 +08:00
//===============================================================================
// Copyright (c) 2007-2016 Advanced Micro Devices, Inc. All rights reserved.
// Copyright (c) 2004-2006 ATI Technologies Inc.
//===============================================================================
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
2020-07-31 11:31:32 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
2020-07-31 11:31:32 +08:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//
// File Name: CodecBuffer_R16.cpp
// Description: implementation of the CCodecBuffer_R16 class
//
//////////////////////////////////////////////////////////////////////////////
#include "common.h"
#include "codecbuffer_r16.h"
2020-07-31 11:31:32 +08:00
//////////////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////////////
const int nChannelCount = 1;
const int nPixelSize = nChannelCount * sizeof(CMP_WORD);
CCodecBuffer_R16::CCodecBuffer_R16(
CMP_BYTE nBlockWidth, CMP_BYTE nBlockHeight, CMP_BYTE nBlockDepth,
CMP_DWORD dwWidth, CMP_DWORD dwHeight, CMP_DWORD dwPitch, CMP_BYTE* pData,CMP_DWORD dwDataSize)
: CCodecBuffer(nBlockWidth, nBlockHeight, nBlockDepth, dwWidth, dwHeight, dwPitch, pData,dwDataSize) {
2020-07-31 11:31:32 +08:00
assert((m_dwPitch == 0) || (m_dwPitch >= GetWidth() * nPixelSize));
if(m_dwPitch <= GetWidth() * nPixelSize)
m_dwPitch = GetWidth() * nPixelSize;
if(m_pData == NULL) {
2020-07-31 11:31:32 +08:00
CMP_DWORD dwSize = m_dwPitch * GetHeight();
m_pData = (CMP_BYTE*) malloc(dwSize);
}
}
CCodecBuffer_R16::~CCodecBuffer_R16() {
2020-07-31 11:31:32 +08:00
}
void CCodecBuffer_R16::Copy(CCodecBuffer& srcBuffer) {
2020-07-31 11:31:32 +08:00
if(GetWidth() != srcBuffer.GetWidth() || GetHeight() != srcBuffer.GetHeight())
return;
const CMP_DWORD dwBlocksX = ((GetWidth() + 3) >> 2);
const CMP_DWORD dwBlocksY = ((GetHeight() + 3) >> 2);
for(CMP_DWORD j = 0; j < dwBlocksY; j++) {
for(CMP_DWORD i = 0; i < dwBlocksX; i++) {
2020-07-31 11:31:32 +08:00
CMP_WORD block[BLOCK_SIZE_4X4X4];
srcBuffer.ReadBlockRGBA(i*4, j*4, 4, 4, block);
WriteBlockRGBA(i*4, j*4, 4, 4, block);
}
}
}
bool CCodecBuffer_R16::ReadBlock(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
CMP_DWORD dwWidth = cmp_minT(w, (GetWidth() - x));
2020-07-31 11:31:32 +08:00
CMP_DWORD i,j;
for(j = 0; j < h && (y + j) < GetHeight(); j++) {
2020-07-31 11:31:32 +08:00
CMP_WORD* pData = (CMP_WORD*) (GetData() + ((y + j) * m_dwPitch) + (x * nPixelSize));
for(i = 0; i < dwWidth; i++)
wBlock[(j * w) + i] = *pData++;
// Pad line with previous values if necessary
if(i < w)
PadLine(i, w, 1, &wBlock[j * w]);
}
// Pad block with previous values if necessary
if(j < h)
PadBlock(j, w, h, 1, wBlock);
return true;
}
bool CCodecBuffer_R16::WriteBlock(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
CMP_DWORD dwWidth = cmp_minT(w, (GetWidth() - x));
2020-07-31 11:31:32 +08:00
for(CMP_DWORD j = 0; j < h && (y + j) < GetHeight(); j++) {
2020-07-31 11:31:32 +08:00
CMP_WORD* pData = (CMP_WORD*) (GetData() + ((y + j) * m_dwPitch) + (x * nChannelCount * sizeof(CMP_WORD)));
for(CMP_DWORD i = 0; i < dwWidth; i++)
*pData++ = wBlock[(j * w) + i];
2020-07-31 11:31:32 +08:00
}
return true;
}
bool CCodecBuffer_R16::ReadBlockA(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
memset(wBlock, 0, w*h*nPixelSize);
return true;
}
bool CCodecBuffer_R16::ReadBlockR(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
return ReadBlock(x, y, w, h, wBlock);
}
bool CCodecBuffer_R16::ReadBlockG(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
memset(wBlock, 0, w*h*nPixelSize);
return true;
}
bool CCodecBuffer_R16::ReadBlockB(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
memset(wBlock, 0, w*h*nPixelSize);
return true;
}
bool CCodecBuffer_R16::WriteBlockA(CMP_DWORD x, CMP_DWORD y, CMP_BYTE /*w*/, CMP_BYTE /*h*/, CMP_WORD /*wBlock*/[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
return true;
}
bool CCodecBuffer_R16::WriteBlockR(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
return WriteBlock(x, y, w, h, wBlock);
}
bool CCodecBuffer_R16::WriteBlockG(CMP_DWORD x, CMP_DWORD y, CMP_BYTE /*w*/, CMP_BYTE /*h*/, CMP_WORD /*wBlock*/[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
return true;
}
bool CCodecBuffer_R16::WriteBlockB(CMP_DWORD x, CMP_DWORD y, CMP_BYTE /*w*/, CMP_BYTE /*h*/, CMP_WORD /*wBlock*/[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
if(x >= GetWidth() || y >= GetHeight())
return false;
2020-07-31 11:31:32 +08:00
return true;
}
#define GET_PIXEL(i, j) &wBlock[(((j * w) + i) * 4)]
bool CCodecBuffer_R16::ReadBlockRGBA(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
assert(x % w == 0);
assert(y % h == 0);
if(x >= GetWidth() || y >= GetHeight())
return false;
CMP_DWORD dwWidth = cmp_minT(w, (GetWidth() - x));
2020-07-31 11:31:32 +08:00
CMP_DWORD i, j;
for(j = 0; j < h && (y + j) < GetHeight(); j++) {
2020-07-31 11:31:32 +08:00
CMP_WORD* pData = (CMP_WORD*) (GetData() + ((y + j) * m_dwPitch) + (x * nPixelSize));
for(i = 0; i < dwWidth; i++) {
2020-07-31 11:31:32 +08:00
CMP_WORD* pDest = GET_PIXEL(i, j);
*pDest++ = *pData++;
2020-07-31 11:31:32 +08:00
*pDest++ = 0;
*pDest++ = 0;
*pDest++ = 0xffff;
}
// Pad line with previous values if necessary
if(i < w)
PadLine(i, w, 4, &wBlock[j * w * 4]);
}
// Pad block with previous values if necessary
if(j < h)
PadBlock(j, w, h, 4, wBlock);
return true;
}
bool CCodecBuffer_R16::WriteBlockRGBA(CMP_DWORD x, CMP_DWORD y, CMP_BYTE w, CMP_BYTE h, CMP_WORD wBlock[]) {
2020-07-31 11:31:32 +08:00
assert(x < GetWidth());
assert(y < GetHeight());
assert(x % w == 0);
assert(y % h == 0);
if(x >= GetWidth() || y >= GetHeight())
return false;
CMP_DWORD dwWidth = cmp_minT(w, (GetWidth() - x));
2020-07-31 11:31:32 +08:00
for(CMP_DWORD j = 0; j < h && (y + j) < GetHeight(); j++) {
2020-07-31 11:31:32 +08:00
CMP_WORD* pData = (CMP_WORD*) (GetData() + ((y + j) * m_dwPitch) + (x * nPixelSize));
for(CMP_DWORD i = 0; i < dwWidth; i++) {
2020-07-31 11:31:32 +08:00
memcpy(pData, GET_PIXEL(i, j), nPixelSize);
pData += nChannelCount;
}
}
return true;
}