-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
137 lines (106 loc) · 3.78 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS // disable fopen/fread warnings on windows
#endif
#include <iostream>
#include <stdio.h>
#include <stdint.h>
#include "RgbBitmap.h"
#include "RgbaBitmap.h"
#include "PvrTcEncoder.h"
#include "PvrTcDecoder.h"
/*
Test program for the PvrTcEncoder, it compresses and then decompresses an
image, ensuring the end result is something reasonable.
*/
using namespace Javelin;
Bitmap *readTga(const char *filename) {
FILE *fp = fopen(filename, "rb");
fseek(fp, 0, SEEK_END);
int fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
unsigned char header[18];
fread(header, 18, 1, fp);
int bpp = header[16];
int w = header[12] | (header[13] << 8);
int h = header[14] | (header[15] << 8);
Bitmap *bitmap = NULL;
if (bpp == 24) {
RgbBitmap *rgb = new RgbBitmap(w, h);
fread((void *) rgb->data, w * h * 3, 1, fp);
bitmap = rgb;
}
else if (bpp == 32) {
RgbaBitmap *rgba = new RgbaBitmap(w, h);
fread((void *) rgba->data, w * h * 4, 1, fp);
bitmap = rgba;
}
fclose(fp);
return bitmap;
}
void writeTga(const char *filename, Bitmap *bitmap) {
FILE *fp = fopen(filename, "wb");
bool isRgb = dynamic_cast<RgbBitmap *>(bitmap) != NULL;
unsigned char header[18];
memset(header, 0, 18);
header[2] = 2;
header[12] = bitmap->width & 0xff;
header[13] = (bitmap->width >> 8) & 0xff;
header[14] = bitmap->height & 0xff;
header[15] = (bitmap->height >> 8) & 0xff;
header[16] = isRgb ? 24 : 32;
fwrite(header, 18, 1, fp);
int bytesPerPixel = isRgb ? 3 : 4;
fwrite(bitmap->data, bitmap->width * bitmap->height * bytesPerPixel, 1, fp);
fclose(fp);
}
int main(int argc, char **argv) {
{
Bitmap *bitmap = readTga("globe.tga");
bool isRgb = dynamic_cast<RgbBitmap *>(bitmap) != NULL;
const int size = bitmap->GetArea() / 2;
// Write the texture prior to compression
writeTga("globe_before.tga", bitmap);
unsigned char *pvrtc = new unsigned char[size];
memset(pvrtc, 0, size);
if (isRgb) {
RgbBitmap *rgb = static_cast<RgbBitmap *>(bitmap);
ColorRgb<unsigned char> *data = rgb->GetData();
PvrTcEncoder::EncodeRgb4Bpp(pvrtc, *rgb);
PvrTcDecoder::DecodeRgb4Bpp(data, bitmap->GetSize(), pvrtc);
}
else {
RgbaBitmap *rgb = static_cast<RgbaBitmap *>(bitmap);
ColorRgba<unsigned char> *data = rgb->GetData();
PvrTcEncoder::EncodeRgba4Bpp(pvrtc, *rgb);
PvrTcDecoder::DecodeRgba4Bpp(data, bitmap->GetSize(), pvrtc);
}
// Write the texture post compression
writeTga("globe_after.tga", bitmap);
delete bitmap;
}
{
Bitmap *bitmap = readTga("alpha.tga");
bool isRgb = dynamic_cast<RgbBitmap *>(bitmap) != NULL;
const int size = bitmap->GetArea() / 2;
// Write the texture prior to compression
writeTga("alpha_before.tga", bitmap);
unsigned char *pvrtc = new unsigned char[size];
memset(pvrtc, 0, size);
if (isRgb) {
RgbBitmap *rgb = static_cast<RgbBitmap *>(bitmap);
ColorRgb<unsigned char> *data = rgb->GetData();
PvrTcEncoder::EncodeRgb4Bpp(pvrtc, *rgb);
PvrTcDecoder::DecodeRgb4Bpp(data, bitmap->GetSize(), pvrtc);
}
else {
RgbaBitmap *rgb = static_cast<RgbaBitmap *>(bitmap);
ColorRgba<unsigned char> *data = rgb->GetData();
PvrTcEncoder::EncodeRgba4Bpp(pvrtc, *rgb);
PvrTcDecoder::DecodeRgba4Bpp(data, bitmap->GetSize(), pvrtc);
}
// Write the texture post compression
writeTga("alpha_after.tga", bitmap);
delete bitmap;
}
return 0;
}