Updates to the DPX class to figure out header packing. Not working yet.

This commit is contained in:
Matt McWilliams 2024-11-07 18:16:24 -05:00
parent 095ed397b2
commit d906731fa7
1 changed files with 70 additions and 34 deletions

View File

@ -15,61 +15,97 @@ uint16_t DPX::swapEndian(uint16_t value) {
}
bool DPX::readHeader(ifstream& file) {
// Read magic number
// DPX Header Map for reference:
// 0x0000-0x0003: Magic number
// 0x0004-0x0007: Image data offset
// 0x0008-0x000B: Version number of header format
// 0x000C-0x000F: Total image file size
// 0x0010-0x0013: Ditto Key
// 0x0014-0x0017: Generic section header length
// 0x0018-0x001B: Industry specific header length
// 0x001C-0x001F: User defined header length
// Read and check magic number
file.read(reinterpret_cast<char*>(&header.magic), sizeof(header.magic));
// Check for both possible magic numbers (big and little endian)
const uint32_t MAGIC_BE = 0x53445058; // "SDPX" in big endian
const uint32_t MAGIC_LE = 0x58504453; // "XPDS" in little endian
const uint32_t MAGIC_BE = 0x53445058; // "SDPX"
const uint32_t MAGIC_LE = 0x58504453; // "XPDS"
cout << "{ \"dpx_magic_number\" : \"" << hex << header.magic << "\" }" << endl;
cout << "{ \"dpx_magic_number\" : \"0x" << hex << header.magic << dec << "\" }" << endl;
if (header.magic == MAGIC_BE) {
header.isBigEndian = true;
} else if (header.magic == MAGIC_LE) {
header.isBigEndian = false;
} else {
uint32_t swapped_magic = swapEndian(header.magic);
cout << "{ \"dpx_swapped_magic_number\" : \"0x" << hex << swapped_magic << "\" }" << endl;
if (swapped_magic == MAGIC_BE) {
header.isBigEndian = true;
header.magic = swapped_magic;
} else if (swapped_magic == MAGIC_LE) {
header.isBigEndian = false;
header.magic = swapped_magic;
} else {
throw runtime_error("Not a valid DPX file - Invalid magic number");
}
throw runtime_error("Not a valid DPX file - Invalid magic number");
}
// Read image data offset
file.seekg(4);
file.read(reinterpret_cast<char*>(&header.imageOffset), sizeof(header.imageOffset));
if (header.isBigEndian) {
header.imageOffset = swapEndian(header.imageOffset);
// Read Core Header
uint32_t genericHeaderLength;
file.seekg(0x14); // Go to generic header length
file.read(reinterpret_cast<char*>(&genericHeaderLength), sizeof(genericHeaderLength));
if (!header.isBigEndian) {
genericHeaderLength = swapEndian(genericHeaderLength);
}
cout << "{ \"dpx_header_length\" : " << genericHeaderLength << " }" << endl;
// Seek to image information offset (768 bytes from start)
file.seekg(768);
// Image Information Header starts at 0x0200 (512)
file.seekg(0x0200);
// Read orientation
uint32_t orientation;
file.read(reinterpret_cast<char*>(&orientation), sizeof(orientation));
if (!header.isBigEndian) {
orientation = swapEndian(orientation);
}
// Read number of image elements
uint16_t numberOfElements;
file.read(reinterpret_cast<char*>(&numberOfElements), sizeof(numberOfElements));
if (!header.isBigEndian) {
numberOfElements = swapEndian(numberOfElements);
}
// Read image dimensions
file.read(reinterpret_cast<char*>(&header.width), sizeof(header.width));
file.read(reinterpret_cast<char*>(&header.height), sizeof(header.height));
if (header.isBigEndian) {
if (!header.isBigEndian) {
header.width = swapEndian(header.width);
header.height = swapEndian(header.height);
}
cout << "{ \"dpx_width\" : " << header.width << ", \"dpx_height\" :" << header.height << " }" << endl;
cout << "{ \"dimensions\" : { \"width\" : " << header.width << ", \"height\" :" << header.height << " }, ";
// Read image element information (1556 bytes from start)
file.seekg(1556);
file.read(reinterpret_cast<char*>(&header.descriptor), sizeof(header.descriptor));
// Image Element Information starts at 0x0604 (1540)
file.seekg(0x0604);
// Read data sign (0 = unsigned)
uint32_t dataSign;
file.read(reinterpret_cast<char*>(&dataSign), sizeof(dataSign));
if (!header.isBigEndian) {
dataSign = swapEndian(dataSign);
}
// Read bit depth
file.read(reinterpret_cast<char*>(&header.bitDepth), sizeof(header.bitDepth));
file.read(reinterpret_cast<char*>(&header.imageElements), sizeof(header.imageElements));
cout << "{ \"dpx_bit_depth\" : " << static_cast<int>(header.bitDepth) << " }"<< endl;
// Read packing method
uint16_t packing;
file.read(reinterpret_cast<char*>(&packing), sizeof(packing));
if (!header.isBigEndian) {
packing = swapEndian(packing);
}
cout << "{ \"dpx_packing\" : " << packing << " }" << endl;
cout << " \"bit_depth\" : " << static_cast<int>(header.bitDepth) << ", ";
cout << " \"offset\": " << header.imageOffset << " }" << endl;
// Read image offset (from start of file)
file.seekg(0x04);
file.read(reinterpret_cast<char*>(&header.imageOffset), sizeof(header.imageOffset));
if (!header.isBigEndian) {
header.imageOffset = swapEndian(header.imageOffset);
}
cout << "{ \"dpx_offset\" : " << header.imageOffset << " }" << endl;
return true;
}