Skip to content

Commit 696414b

Browse files
committed
So close, yet so far
1 parent 54be89f commit 696414b

File tree

6 files changed

+226
-13
lines changed

6 files changed

+226
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ project(ImageLoading)
33

44
set(CMAKE_CXX_STANDARD 11)
55

6-
set(SOURCE_FILES main.cpp image.cpp image.h)
6+
set(SOURCE_FILES main.cpp image.cpp image.h bitarray.cpp bitarray.h)
77
add_executable(ImageLoading ${SOURCE_FILES})

bitarray.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
//
2+
// Created by Ole on 10.09.2017.
3+
//
4+
5+
#include "bitarray.h"
6+
#include <string>
7+
8+
BitArray::BitArray()
9+
: localSize(0), totalSize(0) {
10+
current = new std::bitset<BITSET_SIZE>();
11+
bitsets.push_back(current);
12+
}
13+
14+
BitArray::BitArray(const int& value, unsigned int length)
15+
: localSize(0), totalSize(0) {
16+
current = new std::bitset<BITSET_SIZE>();
17+
bitsets.push_back(current);
18+
19+
if (length > sizeof(int) * 8)
20+
length = sizeof(int) * 8;
21+
22+
for (unsigned int i = 0; i < length; i++) {
23+
pushBack((bool) ((value >> i) & 0x1));
24+
}
25+
}
26+
27+
BitArray::BitArray(std::string& bits)
28+
: localSize(0), totalSize(0) {
29+
current = new std::bitset<BITSET_SIZE>();
30+
bitsets.push_back(current);
31+
32+
pushBack(bits);
33+
}
34+
35+
BitArray::BitArray(const bool* const bits, const unsigned int& size)
36+
: localSize(0), totalSize(0) {
37+
current = new std::bitset<BITSET_SIZE>();
38+
bitsets.push_back(current);
39+
40+
for (int i = 0; i < size; i++)
41+
pushBack(bits[i]);
42+
}
43+
44+
BitArray::~BitArray() {
45+
for (auto bitset : bitsets)
46+
delete bitset;
47+
}
48+
49+
void BitArray::pushBack(const bool& one) {
50+
if (localSize == BITSET_SIZE) {
51+
current = new std::bitset<BITSET_SIZE>();
52+
localSize = 0;
53+
}
54+
(*current)[localSize] = one;
55+
localSize++;
56+
updateSize();
57+
}
58+
59+
void BitArray::pushBack(const unsigned char& byte) {
60+
for (unsigned int i = 0; i < sizeof(byte) * 8; i++) {
61+
pushBack((bool) ((byte >> i) & 1));
62+
}
63+
}
64+
65+
void BitArray::pushBack(const std::string& bits) {
66+
for (char c : bits) {
67+
if (c == '0')
68+
pushBack((bool) 0);
69+
else if (c == '1')
70+
pushBack((bool) 1);
71+
}
72+
}
73+
74+
void BitArray::setBit(const unsigned int& pos, bool one) {
75+
if (pos >= getSize())
76+
return;
77+
bitsets[pos / BITSET_SIZE]->set(pos % BITSET_SIZE, one);
78+
}
79+
80+
bool BitArray::getBit(const unsigned int& position) const {
81+
if (position >= getSize())
82+
return false;
83+
return (*(bitsets[position / BITSET_SIZE]))[position % BITSET_SIZE];
84+
}
85+
86+
void BitArray::addBit(const unsigned int& position) {
87+
if (position >= getSize())
88+
return;
89+
if (getBit(position)) {
90+
setBit(position, false);
91+
addBit(position + 1);
92+
} else {
93+
setBit(position, true);
94+
}
95+
}
96+
97+
void BitArray::add(const unsigned int& value) {
98+
for (unsigned int i = 0; i < sizeof(int) * 8; i++) {
99+
if ((value >> i) & 0x1)
100+
addBit(i);
101+
}
102+
}
103+
104+
void BitArray::add(const BitArray& value) {
105+
const unsigned int count = (value.getSize() < getSize() ? value.getSize() : getSize());
106+
107+
for (unsigned int i = 0; i < count; i++)
108+
if (value.getBit(i))
109+
addBit(i);
110+
}
111+
112+
unsigned char BitArray::read(const unsigned int& position, const unsigned int& length, const bool& reverse) {
113+
unsigned char result = 0;
114+
for(unsigned int i = 0; i < length; i++) {
115+
if(getBit(position + (reverse ? length - 1 - i : i)))
116+
result |= 1 << i;
117+
}
118+
return result;
119+
}
120+
121+
void BitArray::updateSize() {
122+
totalSize = (unsigned int) (BITSET_SIZE * (bitsets.size() - 1) + localSize);
123+
}
124+
125+
const std::string BitArray::toString() const {
126+
std::string out;
127+
for (unsigned int i = 0; i < getSize(); i++) {
128+
out = (getBit(i) ? "1" : "0") + out;
129+
}
130+
return out;
131+
}
132+
133+
std::ostream& operator<<(std::ostream& stream, const BitArray& array) {
134+
return stream << array.toString();
135+
}
136+
137+
std::ostream& operator<<(std::ostream& stream, const BitArray* const array) {
138+
return stream << array->toString();
139+
}

bitarray.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Created by Ole on 10.09.2017.
3+
//
4+
5+
#pragma once
6+
7+
#include <bitset>
8+
#include <vector>
9+
#include <iostream>
10+
11+
#define BITSET_SIZE 128
12+
13+
class BitArray {
14+
private:
15+
std::vector<std::bitset<BITSET_SIZE>*> bitsets;
16+
std::bitset<BITSET_SIZE>* current;
17+
unsigned short localSize;
18+
unsigned int totalSize;
19+
public:
20+
BitArray();
21+
BitArray(const int& value, unsigned int length);
22+
explicit BitArray(std::string& bits);
23+
BitArray(const bool* const bits, const unsigned int& size);
24+
25+
~BitArray();
26+
27+
void pushBack(const bool& one);
28+
void pushBack(const unsigned char& byte);
29+
void pushBack(const std::string& one);
30+
void setBit(const unsigned int& pos, bool one);
31+
bool getBit(const unsigned int& pos) const;
32+
33+
void addBit(const unsigned int& position);
34+
void add(const unsigned int& value);
35+
void add(const BitArray& value);
36+
37+
unsigned char read(const unsigned int& position, const unsigned int& length, const bool& reverse = false);
38+
39+
inline const unsigned int& getSize() const {
40+
return totalSize;
41+
}
42+
const std::string toString() const;
43+
private:
44+
void updateSize();
45+
};
46+
47+
std::ostream& operator<< (std::ostream& stream, const BitArray& array);
48+
std::ostream& operator<<(std::ostream& stream, const BitArray* const array);

image.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ void Image::formatIHDR() {
5656
std::vector<unsigned char> Image::extractCompressedPixelData(const Chunk& chunk) {
5757
std::vector<unsigned char> compressedData;
5858
compressedData.reserve(chunk.length);
59-
for(int i = 0; i < chunk.length; i++) {
59+
for(int i = 0; i < chunk.length; i++)
6060
compressedData.push_back(chunk.data[i]);
61-
}
61+
6262
return compressedData;
6363
}
6464

image.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
unsigned char reversed(unsigned char value);
1515

1616
#define LOG(x) (std::cout << x << std::endl)
17-
#define LOGB(x) (std::cout << (std::bitset<8>)reversed(x) << std::endl)
17+
#define LOGB(x) (std::cout << (std::bitset<8>)(x) << std::endl)
1818

1919
//unsigned int crc_table[256];
2020
//

main.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#include <cstring>
33
#include "image.h"
4+
#include "bitarray.h"
45

56
static const unsigned short order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; /* permutation of code lengths */
67

@@ -34,17 +35,42 @@ int main() {
3435
LOG(pixelChunk.length);
3536
LOG("");
3637

37-
std::vector<unsigned char> compressedData = image.extractCompressedPixelData(*image.chunks[index]);
38+
std::vector<unsigned char> compressedData = image.extractCompressedPixelData(pixelChunk);
3839
LOG("CMF:");
39-
LOGB(compressedData[0] + (unsigned char) (8 <<4));
40-
LOG("FLG:");
41-
LOGB(compressedData[1]); // 16-bit sum is 14415 % 31 == 0
40+
BitArray CMFbits(compressedData[0], 8);
41+
LOG("\tHopefully this is 8: " << (unsigned int)CMFbits.read(0, 4, false));
42+
LOG("\tCINFO: " << (unsigned int)CMFbits.read(4, 4, false));
43+
LOG(CMFbits);
44+
45+
BitArray flagBits(compressedData[1], 8);
46+
LOG("FLG: " << flagBits);
47+
LOG("\tFDICT:" << (unsigned int) flagBits.read(4, 1));
48+
LOG("");
49+
LOG("Checks out: " << (((CMFbits.read(0, 8) << 8) + (flagBits.read(0, 8))) % 31 == 0));
4250
LOG("");
43-
LOGB(compressedData[2]);
44-
LOGB(compressedData[3]);
45-
LOGB(compressedData[4]);
46-
LOGB(compressedData[5]);
47-
LOGB(compressedData[6]);
51+
52+
BitArray nextBits(compressedData[2], 8);
53+
LOG("Last block?: " << (unsigned int) nextBits.read(0, 1));
54+
switch(nextBits.read(1, 2)) {
55+
case 0:
56+
LOG("Compression Method: uncompressed");
57+
break;
58+
case 1:
59+
LOG("Compression Method: fixed");
60+
break;
61+
case 2:
62+
LOG("Compression Method: dynamic");
63+
break;
64+
default:
65+
LOG("Compression Method: ERROR");
66+
}
67+
LOG(nextBits);
68+
BitArray actualData(nextBits.read(3, 5), 5);
69+
actualData.pushBack(compressedData[3]);
70+
actualData.pushBack(compressedData[4]);
71+
actualData.pushBack(compressedData[5]);
72+
actualData.pushBack(compressedData[6]);
73+
LOG(actualData);
4874

4975
return 0;
5076
}

0 commit comments

Comments
 (0)