2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* DreamGrafix super-hi-res conversions.
|
|
|
|
*
|
|
|
|
* Based on code provided by Jason Andersen.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "SuperHiRes.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ==========================================================================
|
2014-11-04 00:26:53 +00:00
|
|
|
* ReformatDG256SHR
|
2007-03-27 17:47:10 +00:00
|
|
|
* ==========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decide whether or not we want to handle this file.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
void ReformatDG256SHR::Examine(ReformatHolder* pHolder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
ReformatHolder::ReformatApplies applies = ReformatHolder::kApplicNot;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fDG.ScanDreamGrafix(pHolder) && fDG.fNumColors == 256)
|
|
|
|
applies = ReformatHolder::kApplicYes;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
pHolder->SetApplic(ReformatHolder::kReformatSHR_DG256, applies,
|
|
|
|
ReformatHolder::kApplicNot, ReformatHolder::kApplicNot);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a 256-color DreamGrafix Super Hi-Res Image.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int ReformatDG256SHR::Process(const ReformatHolder* pHolder,
|
2014-11-04 00:26:53 +00:00
|
|
|
ReformatHolder::ReformatID id, ReformatHolder::ReformatPart part,
|
|
|
|
ReformatOutput* pOutput)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 02:10:18 +00:00
|
|
|
const uint8_t* srcBuf = pHolder->GetSourceBuf(part);
|
2014-11-04 00:26:53 +00:00
|
|
|
long srcLen = pHolder->GetSourceLen(part);
|
|
|
|
int retval = -1;
|
|
|
|
|
|
|
|
if (fDG.UnpackDG(srcBuf, srcLen, &fScreen, NULL)) {
|
|
|
|
MyDIBitmap* pDib = SHRScreenToBitmap8(&fScreen);
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pDib != NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
SetResultBuffer(pOutput, pDib);
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ==========================================================================
|
2014-11-04 00:26:53 +00:00
|
|
|
* ReformatDG3200SHR
|
2007-03-27 17:47:10 +00:00
|
|
|
* ==========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decide whether or not we want to handle this file.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
void ReformatDG3200SHR::Examine(ReformatHolder* pHolder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
ReformatHolder::ReformatApplies applies = ReformatHolder::kApplicNot;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fDG.ScanDreamGrafix(pHolder) && fDG.fNumColors == 3200)
|
|
|
|
applies = ReformatHolder::kApplicYes;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
pHolder->SetApplic(ReformatHolder::kReformatSHR_DG3200, applies,
|
|
|
|
ReformatHolder::kApplicNot, ReformatHolder::kApplicNot);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a 3200-color DreamGrafix Super Hi-Res Image.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
int ReformatDG3200SHR::Process(const ReformatHolder* pHolder,
|
2014-11-04 00:26:53 +00:00
|
|
|
ReformatHolder::ReformatID id, ReformatHolder::ReformatPart part,
|
|
|
|
ReformatOutput* pOutput)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 02:10:18 +00:00
|
|
|
const uint8_t* srcBuf = pHolder->GetSourceBuf(part);
|
2014-11-04 00:26:53 +00:00
|
|
|
long srcLen = pHolder->GetSourceLen(part);
|
|
|
|
int retval = -1;
|
|
|
|
|
|
|
|
if (fDG.UnpackDG(srcBuf, srcLen, &fScreen, fExtColorTable)) {
|
|
|
|
MyDIBitmap* pDib = SHR3200ToBitmap24();
|
2014-11-18 05:13:13 +00:00
|
|
|
if (pDib != NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
SetResultBuffer(pOutput, pDib);
|
|
|
|
retval = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ==========================================================================
|
2014-11-04 00:26:53 +00:00
|
|
|
* ReformatDreamGrafix
|
2007-03-27 17:47:10 +00:00
|
|
|
* ==========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Examine a DreamGrafix file. This figures out if its any of the
|
|
|
|
* DreamGrafix formats, i.e. 256-color, 3200-color, packed, or unpacked.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
bool DreamGrafix::ScanDreamGrafix(ReformatHolder* pHolder)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
long fileType = pHolder->GetFileType();
|
|
|
|
long auxType = pHolder->GetAuxType();
|
|
|
|
long fileLen = pHolder->GetSourceLen(ReformatHolder::kPartData);
|
|
|
|
bool relaxed, couldBe;
|
|
|
|
|
|
|
|
relaxed = pHolder->GetOption(ReformatHolder::kOptRelaxGfxTypeCheck) != 0;
|
|
|
|
couldBe = false;
|
|
|
|
|
|
|
|
if (fileType == Reformat::kTypePNT && auxType == 0x8005)
|
|
|
|
couldBe = true; // $c0/8005 DG packed
|
|
|
|
else if (fileType == Reformat::kTypePIC && auxType == 0x8003)
|
|
|
|
couldBe = false; // $c1/8003 DG unpacked -- not supported
|
|
|
|
else if (relaxed) {
|
|
|
|
if (fileType == Reformat::kTypeBIN && auxType == 0x8005)
|
|
|
|
couldBe = true;
|
|
|
|
}
|
|
|
|
if (fileLen < 256)
|
|
|
|
couldBe = false;
|
|
|
|
|
|
|
|
if (!couldBe)
|
|
|
|
return false;
|
|
|
|
|
2014-11-21 02:10:18 +00:00
|
|
|
const uint8_t* ptr;
|
2014-11-04 00:26:53 +00:00
|
|
|
ptr = pHolder->GetSourceBuf(ReformatHolder::kPartData)
|
|
|
|
+ fileLen - kHeaderOffset;
|
|
|
|
|
|
|
|
if (memcmp(ptr+6, "\x0a""DreamWorld", 11) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
fNumColors = (*ptr == 0) ? 256 : 3200;
|
|
|
|
fHeight = Reformat::Get16LE(ptr + 2);
|
|
|
|
fWidth = Reformat::Get16LE(ptr + 4);
|
|
|
|
if (fWidth != 320 || fHeight != 200) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("ODD: strange height %dx%x in DG", fWidth, fHeight);
|
2014-11-04 00:26:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unpack a DreamGrafix SHR image compressed with LZW.
|
|
|
|
*
|
|
|
|
* Returns true on success, false if the uncompress step failed to produce
|
|
|
|
* exactly 32768+32*200 bytes.
|
|
|
|
*/
|
2014-11-25 22:34:14 +00:00
|
|
|
bool DreamGrafix::UnpackDG(const uint8_t* srcBuf, long srcLen,
|
2014-11-21 02:10:18 +00:00
|
|
|
ReformatSHR::SHRScreen* pScreen, uint8_t* extColorTable)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
int expectedLen;
|
2014-11-21 02:10:18 +00:00
|
|
|
uint8_t* tmpBuf;
|
2014-11-04 00:26:53 +00:00
|
|
|
int actual;
|
|
|
|
|
|
|
|
if (extColorTable == NULL) {
|
|
|
|
/*
|
|
|
|
* 32000 for pixels (320*200*0.5 bytes)
|
|
|
|
* 256 for SCB (200 + 56 unused)
|
|
|
|
* 512 for basic palette (16 sets * 16 colors * 2 bytes)\
|
|
|
|
* 512 optional/unused?
|
|
|
|
*/
|
|
|
|
expectedLen = 33280;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* 32000 for pixels (320*200*0.5 bytes)
|
|
|
|
* 6400 for palette (200 lines * 16 entries * 2 bytes)
|
|
|
|
* 512 optional/unused?
|
|
|
|
*/
|
|
|
|
expectedLen = 38912;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* over-alloc -- our LZW decoder doesn't check often */
|
2014-11-21 02:10:18 +00:00
|
|
|
tmpBuf = new uint8_t[expectedLen + 1024];
|
2014-11-04 00:26:53 +00:00
|
|
|
if (tmpBuf == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
actual = UnpackLZW(srcBuf, srcLen, tmpBuf, expectedLen);
|
|
|
|
if (actual != expectedLen && actual != (expectedLen-512)) {
|
2014-11-25 22:34:14 +00:00
|
|
|
LOGW("UnpackLZW expected %d, got %d", expectedLen, actual);
|
2014-11-19 22:54:24 +00:00
|
|
|
delete[] tmpBuf;
|
2014-11-04 00:26:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(pScreen->pixels, tmpBuf, 32000);
|
|
|
|
if (extColorTable == NULL) {
|
|
|
|
memcpy(pScreen->scb, tmpBuf + 32000, 256);
|
|
|
|
memcpy(pScreen->colorTable, tmpBuf + 32256, 512);
|
|
|
|
} else {
|
2014-11-21 02:10:18 +00:00
|
|
|
const uint16_t* pSrcTable;
|
|
|
|
uint16_t* pDstTable;
|
2014-11-04 00:26:53 +00:00
|
|
|
|
2014-11-21 02:10:18 +00:00
|
|
|
pSrcTable = (const uint16_t*) (tmpBuf + 32000);
|
|
|
|
pDstTable = (uint16_t*) extColorTable;
|
2014-11-04 00:26:53 +00:00
|
|
|
int table;
|
|
|
|
for (table = 0; table < ReformatSHR::kNumLines; table++) {
|
|
|
|
int entry;
|
|
|
|
for (entry = 0; entry < ReformatSHR::kNumEntriesPerColorTable; entry++) {
|
|
|
|
pDstTable[(ReformatSHR::kNumEntriesPerColorTable-1) - entry] =
|
|
|
|
*pSrcTable++;
|
|
|
|
}
|
|
|
|
pDstTable += ReformatSHR::kNumEntriesPerColorTable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:54:24 +00:00
|
|
|
delete[] tmpBuf;
|
2014-11-04 00:26:53 +00:00
|
|
|
return true;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Constants for LZW.
|
|
|
|
*/
|
|
|
|
static const int kClearCode = 256;
|
|
|
|
static const int kEofCode = 257;
|
|
|
|
static const int kFirstFreeCode = 258;
|
|
|
|
|
|
|
|
static const unsigned int bitMasks[] = {
|
2014-11-04 00:26:53 +00:00
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1ff, 0x3ff, 0x7ff, 0xfff
|
2007-03-27 17:47:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* initialize the table */
|
2014-11-04 00:26:53 +00:00
|
|
|
#define INIT_TABLE() { \
|
|
|
|
nBitMod1 = 9; \
|
|
|
|
nBitMask = bitMasks[nBitMod1]; \
|
|
|
|
maxCode = 1 << nBitMod1; \
|
|
|
|
freeCode = kFirstFreeCode; \
|
|
|
|
};
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
/* add a new code to our data structure */
|
2014-11-04 00:26:53 +00:00
|
|
|
#define ADD_CODE() { \
|
|
|
|
hashChar[freeCode] = k; \
|
|
|
|
hashNext[freeCode] = oldCode; \
|
|
|
|
freeCode++; \
|
|
|
|
};
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
/* read the next code and put the value into iCode */
|
2014-11-04 00:26:53 +00:00
|
|
|
#define READ_CODE() { \
|
|
|
|
int bit_idx = bitOffset & 0x7; \
|
|
|
|
int byte_idx = bitOffset >> 3; \
|
|
|
|
\
|
|
|
|
iCode = srcBuf[ byte_idx ]; \
|
|
|
|
iCode &= 0xFF; \
|
|
|
|
iCode |= (srcBuf[ byte_idx+1 ]<<8); \
|
|
|
|
iCode &= 0xFFFF; \
|
|
|
|
iCode |= (srcBuf[ byte_idx+2 ]<<16); \
|
|
|
|
iCode >>= bit_idx; \
|
|
|
|
iCode &= nBitMask; \
|
|
|
|
bitOffset += nBitMod1; \
|
|
|
|
};
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-25 22:34:14 +00:00
|
|
|
/*static*/ int DreamGrafix::UnpackLZW(const uint8_t* srcBuf, long srcLen,
|
2014-11-21 02:10:18 +00:00
|
|
|
uint8_t* dstBuf, long dstLen)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-21 02:10:18 +00:00
|
|
|
uint16_t finChar, oldCode, inCode, freeCode, maxCode, k;
|
|
|
|
uint16_t nBitMod1, nBitMask;
|
2014-11-04 00:26:53 +00:00
|
|
|
int bitOffset;
|
2014-11-21 02:10:18 +00:00
|
|
|
uint16_t hashNext[4096];
|
|
|
|
uint16_t hashChar[4096];
|
2014-11-04 00:26:53 +00:00
|
|
|
|
2014-11-21 02:10:18 +00:00
|
|
|
uint8_t* pOrigDst = dstBuf;
|
2014-11-04 00:26:53 +00:00
|
|
|
int iCode;
|
2014-11-21 02:10:18 +00:00
|
|
|
uint16_t stack[32768];
|
2014-11-04 00:26:53 +00:00
|
|
|
int stackIdx = 0;
|
|
|
|
|
|
|
|
/* initialize table and code reader */
|
|
|
|
INIT_TABLE();
|
|
|
|
bitOffset = 0;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (dstBuf - pOrigDst > dstLen) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("LZW overrun");
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int A;
|
|
|
|
int Y;
|
|
|
|
READ_CODE();
|
|
|
|
if (iCode == kEofCode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iCode == kClearCode) {
|
|
|
|
// Got Clear
|
|
|
|
INIT_TABLE();
|
|
|
|
READ_CODE();
|
|
|
|
oldCode = iCode;
|
|
|
|
k = iCode;
|
|
|
|
finChar = iCode;
|
2014-11-21 02:10:18 +00:00
|
|
|
*dstBuf++ = (uint8_t) iCode;
|
2014-11-04 00:26:53 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
A = inCode = iCode;
|
|
|
|
|
|
|
|
if (iCode < freeCode) {
|
|
|
|
goto inTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
stack[stackIdx] = finChar;
|
|
|
|
stackIdx++;
|
|
|
|
A = oldCode;
|
2007-03-27 17:47:10 +00:00
|
|
|
inTable:
|
2014-11-04 00:26:53 +00:00
|
|
|
if (A < 256) {
|
|
|
|
goto gotChar;
|
|
|
|
}
|
|
|
|
while (A >= 256) {
|
|
|
|
Y = A;
|
|
|
|
A = hashChar[Y];
|
|
|
|
|
|
|
|
stack[stackIdx] = A;
|
|
|
|
stackIdx++;
|
|
|
|
|
|
|
|
A = hashNext[Y];
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
gotChar:
|
2014-11-04 00:26:53 +00:00
|
|
|
A &= 0xFF;
|
|
|
|
finChar = A;
|
|
|
|
k = A;
|
|
|
|
Y = 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
dstBuf[Y++] = A;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
while (stackIdx) {
|
|
|
|
stackIdx--;
|
|
|
|
A = stack[stackIdx];
|
|
|
|
dstBuf[Y++] = A;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
dstBuf += Y;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
ADD_CODE();
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
oldCode = inCode;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (freeCode < maxCode) {
|
|
|
|
continue; // goto nextCode;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (12 == nBitMod1) {
|
|
|
|
continue; // goto nextCode;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
nBitMod1++;
|
|
|
|
nBitMask = bitMasks[nBitMod1];
|
|
|
|
//printf("nBitMod1 = %d, nBitMask = %04x\n",
|
|
|
|
// nBitMod1, nBitMask);
|
|
|
|
maxCode <<= 1;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return dstBuf - pOrigDst;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|