-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmuncher.c
146 lines (141 loc) · 3.72 KB
/
bitmuncher.c
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
/*
* bitmuncher - play your files bit by bit
* Copyright (C) 2015 Raphael Sousa Santos, http://www.raphaelss.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
struct mmap_data {
unsigned char *data;
size_t size;
int fd;
};
static int
mmap_data_init(struct mmap_data *md, const char *path) {
md->fd = open(path, O_RDONLY);
if (md->fd == -1) {
fprintf(stderr, "Error opening %s\n", path);
return 1;
}
struct stat s;
fstat(md->fd, &s);
md->data = mmap(0, s.st_size, PROT_READ, MAP_SHARED, md->fd, 0);
if (md->data == MAP_FAILED) {
close(md->fd);
fputs("Error on mmap\n", stderr);
return 1;
}
md->size = s.st_size;
return 0;
}
static void
mmap_data_release(struct mmap_data *md) {
munmap(md->data, md->size);
close(md->fd);
}
int
main(int argc, char **argv) {
const char *file_path;
int width, height, fullscreen = 0;
switch (argc) {
case 2:
file_path = argv[1];
fullscreen = 1;
break;
case 4:
width = atoi(argv[1]);
height = atoi(argv[2]);
file_path = argv[3];
if (width > 0 && height > 0) {
break;
}
puts("width and height must be positive integers");
default:
puts("usage:\n"
"- fullscreen:\tbitmuncher file\n"
"- window:\tbitmuncher width height file");
return 0;
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
fprintf(stderr, "Error initializing SDL: %s\n", SDL_GetError());
return 1;
}
struct mmap_data md;
if (mmap_data_init(&md, file_path)) {
SDL_Quit();
return 1;
}
SDL_Window *window;
SDL_DisplayMode dm;
if (SDL_GetCurrentDisplayMode(0, &dm)) {
fprintf(stderr, "SDL_GetDisplayMode failed: %s", SDL_GetError());
mmap_data_release(&md);
SDL_Quit();
return 1;
}
int bytes_per_pixel = SDL_BYTESPERPIXEL(dm.format);
if (fullscreen) {
window = SDL_CreateWindow("Bit muncher",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
dm.w, dm.h, SDL_WINDOW_FULLSCREEN);
} else {
window = SDL_CreateWindow("Bit muncher",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height, SDL_WINDOW_SHOWN);
}
if (!window) {
fputs("Error initializing window\n", stderr);
mmap_data_release(&md);
SDL_Quit();
return 1;
}
size_t byte_i = 0, bit_i = 0;
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
unsigned total_pixels = window_surface->w * window_surface->h * 4;
SDL_LockSurface(window_surface);
SDL_UnlockSurface(window_surface);
int run = 1;
SDL_Event event;
while (run) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
run = 0;
break;
default:
break;
}
}
SDL_LockSurface(window_surface);
unsigned char *pixels = window_surface->pixels;
for (size_t i = 0; i < total_pixels; i += bytes_per_pixel) {
memset(pixels + i, ((md.data[byte_i] & (1 << bit_i++)) != 0) * 0xFF, bytes_per_pixel);
bit_i %= 8;
byte_i = (byte_i + !bit_i) % md.size;
}
SDL_UnlockSurface(window_surface);
SDL_UpdateWindowSurface(window);
}
mmap_data_release(&md);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}