Worldstone
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
dcc.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <stdint.h>
7 #include <FileStream.h>
8 #include <Vector.h>
9 #include <memory>
10 #include <type_traits>
11 #include "AABB.h"
12 #include "ImageView.h"
13 #include "Palette.h"
14 
15 namespace WorldStone
16 {
17 // clang-format off
45 // clang-format on
46 class DCC
47 {
48  using Extents = AABB<int32_t>;
49 
50 public:
52  struct Header
53  {
54  uint8_t signature;
55  uint8_t version;
56  uint8_t directions;
57  uint32_t framesPerDir;
58  uint32_t tag;
59 
62  uint32_t finalDc6Size;
63  };
64 
74  {
75  // clang-format off
76  uint32_t outsizeCoded;
78  bool compressEqualCells : 1;
79  uint32_t variable0Bits : 4;
80  uint32_t widthBits : 4;
81  uint32_t heightBits : 4;
82  uint32_t xOffsetBits : 4;
83  uint32_t yOffsetBits : 4;
84  uint32_t optionalBytesBits : 4;
85  uint32_t codedBytesBits : 4;
86  // clang-format on
87  };
88 
89  struct FrameHeader
90  {
93  uint32_t variable0;
94  uint32_t width;
95  uint32_t height;
96  int32_t xOffset;
97  int32_t yOffset;
98  uint32_t optionalBytes;
99  uint32_t codedBytes;
100  bool frameBottomUp;
102 
107  };
108 
109  // clang-format off
137  // clang-format on
138  struct Direction
139  {
142  DirectionHeader header;
143  Vector<FrameHeader> frameHeaders;
145 
146  Extents extents;
147 
150  {
151  extents.initializeForExtension();
152  for (const FrameHeader& frame : frameHeaders)
153  extents.extend(frame.extents);
154  }
155  };
156 
160  static constexpr unsigned bitsWidthTable[16] = {0, 1, 2, 4, 6, 8, 10, 12,
161  14, 16, 20, 24, 26, 28, 30, 32};
162 
163 protected:
164  StreamPtr stream = nullptr;
165  Header header;
171  Vector<uint32_t> directionsOffsets;
172  Vector<uint32_t> framePointers;
173 
174  size_t getDirectionSize(uint32_t dirIndex);
175 
176  bool extractHeaderAndOffsets();
177 
178 public:
185  bool initDecoder(StreamPtr&& streamPtr);
186 
188  void reset() { *this = DCC{}; }
189 
200  bool readDirection(Direction& outDir, uint32_t dirIndex, IImageProvider<uint8_t>& imgProvider);
201 
203  const Header& getHeader() const { return header; }
204 };
205 } // namespace WorldStone
uint32_t variable0Bits
Endcoded size in bits of FrameHeader::variable0.
Definition: dcc.h:79
Extents extents
Extent of this frame image in the pixel buffer.
Definition: dcc.h:106
uint32_t codedBytesBits
Endcoded size in bits of FrameHeader::codedBytes.
Definition: dcc.h:85
uint32_t heightBits
Endcoded size in bits of FrameHeader::height.
Definition: dcc.h:81
uint32_t yOffsetBits
Endcoded size in bits of FrameHeader::yoffset.
Definition: dcc.h:83
void reset()
Resets the decoder and frees resources.
Definition: dcc.h:188
static constexpr unsigned bitsWidthTable[16]
An array that maps an encoded 4-bit size to the real size in bits.
Definition: dcc.h:160
bool readDirection(Direction &outDir, uint32_t dirIndex, IImageProvider< uint8_t > &imgProvider)
Decodes a direction of the file into memory.
Definition: dcc.cpp:618
uint32_t framesPerDir
Frames number for each direction.
Definition: dcc.h:57
Decoder for the DCC image format.
Definition: dcc.h:46
Vector< uint32_t > directionsOffsets
Offset of each direction header in the file, follows the Header.
Definition: dcc.h:171
const Header & getHeader() const
Returns the header of the file read by extractHeaderAndOffsets.
Definition: dcc.h:203
uint8_t version
DCC major version, usually 6.
Definition: dcc.h:55
Represents a direction header.
Definition: dcc.h:73
void computeDirExtents()
Definition: dcc.h:149
uint32_t optionalBytesBits
Endcoded size in bits of FrameHeader::optionalBytes.
Definition: dcc.h:84
bool hasRawPixelEncoding
Do we have a direction supporting the raw pixel encoding ?
Definition: dcc.h:77
uint8_t signature
Magic number for DCC files, must be 0x74.
Definition: dcc.h:54
Just a placeholder to use if we ever want to implement our own vector type.
uint8_t directions
Number of directions in this file, max 32.
Definition: dcc.h:56
uint32_t widthBits
Endcoded size in bits of FrameHeader::width.
Definition: dcc.h:80
The header of the file, contains global information about the encoded image.
Definition: dcc.h:52
Implements image manipulation helpers.
An interface of a class that can provide images views.
Definition: ImageView.h:130
uint32_t xOffsetBits
Endcoded size in bits of FrameHeader::xoffset.
Definition: dcc.h:82
bool initDecoder(StreamPtr &&streamPtr)
Start decoding the stream and preparing data.
Definition: dcc.cpp:44
bool compressEqualCells
Do we have a stream for the equal cells optimization ?
Definition: dcc.h:78
uint32_t tag
Seems to be always 1 ?
Definition: dcc.h:58
uint32_t finalDc6Size
Size of the decoded image in bytes (outsizeCoded for all directions) + directions*framesPerDir*4 + 24...
Definition: dcc.h:62