From d906731fa75b912e0be83603e5de336fbec6d2a4 Mon Sep 17 00:00:00 2001 From: mattmcw Date: Thu, 7 Nov 2024 18:16:24 -0500 Subject: [PATCH] Updates to the DPX class to figure out header packing. Not working yet. --- src/dpx.cpp | 104 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/src/dpx.cpp b/src/dpx.cpp index 58ba324..e8a7318 100644 --- a/src/dpx.cpp +++ b/src/dpx.cpp @@ -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(&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(&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(&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(&orientation), sizeof(orientation)); + if (!header.isBigEndian) { + orientation = swapEndian(orientation); + } + + // Read number of image elements + uint16_t numberOfElements; + file.read(reinterpret_cast(&numberOfElements), sizeof(numberOfElements)); + if (!header.isBigEndian) { + numberOfElements = swapEndian(numberOfElements); + } + + // Read image dimensions file.read(reinterpret_cast(&header.width), sizeof(header.width)); file.read(reinterpret_cast(&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(&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(&dataSign), sizeof(dataSign)); + if (!header.isBigEndian) { + dataSign = swapEndian(dataSign); + } + + // Read bit depth file.read(reinterpret_cast(&header.bitDepth), sizeof(header.bitDepth)); - file.read(reinterpret_cast(&header.imageElements), sizeof(header.imageElements)); + cout << "{ \"dpx_bit_depth\" : " << static_cast(header.bitDepth) << " }"<< endl; + + // Read packing method + uint16_t packing; + file.read(reinterpret_cast(&packing), sizeof(packing)); + if (!header.isBigEndian) { + packing = swapEndian(packing); + } + cout << "{ \"dpx_packing\" : " << packing << " }" << endl; - cout << " \"bit_depth\" : " << static_cast(header.bitDepth) << ", "; - cout << " \"offset\": " << header.imageOffset << " }" << endl; + // Read image offset (from start of file) + file.seekg(0x04); + file.read(reinterpret_cast(&header.imageOffset), sizeof(header.imageOffset)); + if (!header.isBigEndian) { + header.imageOffset = swapEndian(header.imageOffset); + } + cout << "{ \"dpx_offset\" : " << header.imageOffset << " }" << endl; return true; }