-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmp-data-view.cpp
More file actions
188 lines (171 loc) · 6.37 KB
/
bmp-data-view.cpp
File metadata and controls
188 lines (171 loc) · 6.37 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<vector>
#include<cstdint>
#include<iostream>
#include<fstream>
#include<assert.h>
#include<SDL2/SDL.h>
using namespace std;
void update_display(SDL_Renderer * rend, vector<uint8_t>& img, int winw, int winh, int skipx, int skipy) {
uint32_t rmask = 0x000000ff;
uint32_t gmask = 0x0000ff00;
uint32_t bmask = 0x00ff0000;
uint32_t amask = 0xff000000;
SDL_Surface * surf = SDL_CreateRGBSurface(0, winw, winh, 32 /*bit depth*/, 0,0,0,0 /*color masks*/);
if(!surf) {
cerr<<"Failed to create surface.\t"<<SDL_GetError()<<endl;
return;
}
printf("Pitch: %d\n", surf->pitch);
// imgp < max_winp
for(int pix = 0; pix < winw * winh * 3 && pix + skipx + 3 * winw * skipy < img.size(); pix+=3) {
int x = (pix/3) % winw;
int y = (pix/3) / winw;
int pitch = surf->pitch;
int addr = (y * pitch) + (4*x);
((uint8_t *)(surf->pixels))[addr+2] = img[pix + skipx + (3*skipy*winw)+2 ];
((uint8_t *)(surf->pixels))[addr+1] = img[pix + skipx + (3*skipy*winw)+1 ];
((uint8_t *)(surf->pixels))[addr+0] = img[pix + skipx + (3*skipy*winw)+0 ];
((uint8_t *)(surf->pixels))[addr+3] = 0xff;
}
SDL_Texture * tex = SDL_CreateTextureFromSurface(rend, surf);
SDL_FreeSurface(surf);
surf = nullptr;
SDL_RenderClear(rend);
SDL_RenderCopy(rend, tex, NULL, NULL);
SDL_RenderPresent(rend);
}
int main(int argc, char *argv[]) {
if(argc != 2) {
cerr<<"Provide a data file."<<endl;
return 1;
}
ifstream img_file(argv[1]);
if(!img_file.is_open()) {
cerr<<"Couldn't open the file. Check paths."<<endl;
return 1;
}
size_t img_size = 0;
img_file.seekg(0,ios::end);
img_size = img_file.tellg();
img_file.seekg(0);
vector<uint8_t> decoded_img(img_size);
img_file.read(reinterpret_cast<char *>(&decoded_img[0]), img_size);
int sdl_status = SDL_Init(SDL_INIT_VIDEO);
if(sdl_status != 0) {
cerr<<"Got return value "<<sdl_status<<" while init'ing SDL. Aborting."<<endl;
cerr<<"Message: "<<SDL_GetError()<<endl;
return 1;
}
bool cont = true;
bool changed = true;
int winh = 512;
int winw = 256;
int old_winh = 512;
int old_winw = 256;
int skipx = 0;
int skipy = 0;
SDL_Window * win = SDL_CreateWindow("image viewer",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
winw, winh,
SDL_WINDOW_SHOWN);
if(!win) {
cerr<<"Failed to create window!\t"<<SDL_GetError()<<endl;
return 1;
}
SDL_Renderer *rend = SDL_CreateRenderer(win, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(!rend) {
cerr<<"Failed to create renderer!\t"<<SDL_GetError()<<endl;
return 1;
}
int ticks = 0;
int decode_mode = 0; // 0 = no decode, 1 = Solar Winds RLE decode #1, 2 = Solar Winds RLE decode #2
while(cont) {
//Read inputs
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_WINDOWEVENT) {
cout<<"Window event"<<endl;
}
else if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
//cout<<"Key event (scancode: "<<event.key.keysym.scancode<<", keycode: "<<event.key.keysym.sym<<", name: "<<SDL_GetKeyName(event.key.keysym.sym)<<": ";
if(event.key.state == SDL_PRESSED) {
//cout<<"Keydown event"<<endl;
switch(event.key.keysym.scancode) {
case SDL_SCANCODE_Q:
cont = false;
break;
case SDL_SCANCODE_A:
if(winw > 8) {
winw--;
changed = true;
}
break;
case SDL_SCANCODE_D:
if(winw < 4096) {
winw++;
changed = true;
}
break;
case SDL_SCANCODE_W:
if(winh > 8) {
winh--;
changed = true;
}
break;
case SDL_SCANCODE_S:
if(winh < 4096) {
winh++;
changed = true;
}
break;
case SDL_SCANCODE_J:
if(skipx > 0) {
skipx--;
changed = true;
}
break;
case SDL_SCANCODE_L:
if(skipx + skipy * winw < decoded_img.size()) {
skipx++;
changed = true;
}
break;
case SDL_SCANCODE_I:
if(skipy > 0) {
skipy--;
changed = true;
}
break;
case SDL_SCANCODE_K:
if(skipx + skipy * winw < decoded_img.size()) {
skipy++;
changed = true;
}
break;
}
}
else if(event.key.state == SDL_RELEASED) {
//cout<<"Keyup event"<<endl;
}
}
}
if(changed) {
//Apply changes
if(winw != old_winw || winh != old_winh) {
SDL_SetWindowSize(win, winw, winh);
old_winw = winw;
old_winh = winh;
cout<<winw<<" x "<<winh<<endl;
}
update_display(rend, decoded_img, winw, winh, skipx, skipy);
changed = false;
}
SDL_Delay(10);
ticks++;
}
SDL_DestroyWindow(win);
win = NULL;
SDL_Quit();
return 0;
}