-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Everywhere: Make loading pcx format possible
- Loading branch information
Showing
5 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include "pcx.h" | ||
#include "d1gfx.h" | ||
#include <QFile> | ||
#include <QBuffer> | ||
#include <QByteArray> | ||
#include <QDataStream> | ||
|
||
bool Pcx::load(D1Gfx &gfx, QString filePath, const OpenAsParam ¶ms) | ||
{ | ||
if (!QFile::exists(filePath)) | ||
return false; | ||
|
||
QFile file = QFile(filePath); | ||
|
||
if (!file.open(QIODevice::ReadOnly)) | ||
return false; | ||
|
||
QByteArray fileData = file.readAll(); | ||
QBuffer fileBuffer(&fileData); | ||
|
||
if (!fileBuffer.open(QIODevice::ReadOnly)) | ||
return false; | ||
|
||
QDataStream in(&fileBuffer); | ||
in.setByteOrder(QDataStream::BigEndian); | ||
|
||
PCXHeader header; | ||
|
||
char* headerBuf = reinterpret_cast<char*>(&header); | ||
|
||
D1GfxFrame frame; | ||
|
||
in.readRawData(headerBuf, PcxHeaderSize); | ||
|
||
frame.width = header.Xmax; | ||
frame.height = header.Ymax; | ||
|
||
gfx.gfxFilePath = filePath; | ||
|
||
constexpr uint16_t PaletteSize = 768; | ||
const uint32_t imgDataSize = fileData.size() - PcxHeaderSize - PaletteSize - 1; | ||
unsigned char imgData[imgDataSize]; | ||
in.readRawData(reinterpret_cast<char *>(imgData), imgDataSize); | ||
|
||
QList<D1GfxPixel> pixelLine; | ||
for (int i = 0; i < imgDataSize; i++) { | ||
|
||
if (pixelLine.size() == header.BytesPerLine) { | ||
frame.pixels.append(pixelLine); | ||
pixelLine.clear(); | ||
} | ||
|
||
constexpr uint8_t PcxMaxSinglePixel = 0xBF; | ||
|
||
const uint8_t byte = imgData[i]; | ||
if (byte <= PcxMaxSinglePixel) { | ||
pixelLine.append(D1GfxPixel::colorPixel(byte)); | ||
continue; | ||
} | ||
|
||
constexpr uint8_t PcxRunLengthMask = 0x3F; | ||
const uint8_t runLength = (byte & PcxRunLengthMask); | ||
for (unsigned int repeatedByte = 0; repeatedByte < runLength; repeatedByte++) | ||
{ | ||
if (pixelLine.size() == header.BytesPerLine) { | ||
frame.pixels.append(pixelLine); | ||
pixelLine.clear(); | ||
} | ||
pixelLine.append(D1GfxPixel::colorPixel(imgData[i+1])); | ||
} | ||
i++; | ||
} | ||
|
||
gfx.frames.append(frame); | ||
|
||
// TBD how to actually handle loading palette, since it is being | ||
// from the image directly | ||
|
||
// unsigned char paletteBuf[768] = { 0 }; | ||
// in.skipRawData(1); | ||
// in.readRawData(reinterpret_cast<char *>(paletteBuf), 768); | ||
// QFile pcxFile("pcxtest.pal"); | ||
// if (!pcxFile.open(QIODevice::WriteOnly)) { | ||
// } | ||
// | ||
// QByteArray byteArray(reinterpret_cast<const char*>(paletteBuf), 768); | ||
// pcxFile.write(byteArray); | ||
// pcxFile.close(); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <QString> | ||
#include "d1gfx.h" | ||
|
||
struct PCXHeader { | ||
uint8_t Manufacturer; | ||
uint8_t Version; | ||
uint8_t Encoding; | ||
uint8_t BitsPerPixel; | ||
uint16_t Xmin; | ||
uint16_t Ymin; | ||
uint16_t Xmax; | ||
uint16_t Ymax; | ||
uint16_t HDpi; | ||
uint16_t VDpi; | ||
uint8_t Colormap[48]; | ||
uint8_t Reserved; | ||
uint8_t NPlanes; | ||
uint16_t BytesPerLine; | ||
uint16_t PaletteInfo; | ||
uint16_t HscreenSize; | ||
uint16_t VscreenSize; | ||
uint8_t Filler[54]; | ||
}; | ||
|
||
class Pcx { | ||
public: | ||
static constexpr size_t PcxHeaderSize = 128; | ||
|
||
static bool load(D1Gfx &gfx, QString filePath, const OpenAsParam ¶ms); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters