Skip to content

Commit e40b04b

Browse files
committed
2.0: custom image size
1 parent 79dd43c commit e40b04b

File tree

4 files changed

+59
-62
lines changed

4 files changed

+59
-62
lines changed

src/main.c

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,15 @@
1111

1212
#define BLACK_THRESHOLD 100
1313
#define DEGREE_STRIDE 0.1
14-
#define SCREEN_WIDTH 2000
15-
#define SCREEN_HEIGHT 2000
1614

17-
struct point surface_point_rotate(const struct point *p, double degree)
15+
struct point surface_point_rotate(const struct point *p, double degree, int height, int width)
1816
{
1917
// since point is rotated around the center (0,0), we substract
2018
// half of the screen WIDTH and HEIGHT from X and Y so as to move
2119
// it to the center (image coordinates are always positive and the
2220
// (0, 0) position is in the top left corner
23-
int x_offset = SCREEN_WIDTH / 2;
24-
int y_offset = SCREEN_HEIGHT / 2;
21+
int x_offset = width / 2;
22+
int y_offset = height / 2;
2523

2624
struct point aligned_pt = mkpoint(p->x - x_offset, p->y - y_offset);
2725
struct point rotated_pt = point_rotate(&aligned_pt, degree);
@@ -38,15 +36,15 @@ struct point surface_point_rotate(const struct point *p, double degree)
3836
// If we do not encounter any tracks on the way, we assume that we
3937
// have reached the end of the plate (last track before large black
4038
// circle in the middle of the plate)
41-
bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg)
39+
bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg, int height, int width)
4240
{
4341
const int dead_end_pixel_count = 30;
4442
int black_pixels = 0;
4543
struct point pt;
4644
struct pixel pix;
4745
while (black_pixels < dead_end_pixel_count)
4846
{
49-
pt = surface_point_rotate(&start, deg);
47+
pt = surface_point_rotate(&start, deg, height, width);
5048
pix = get_pixel(png_surface, &pt);
5149
if (pix.r > BLACK_THRESHOLD)
5250
{
@@ -61,18 +59,18 @@ bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg)
6159
// crawl from the image top center (SCREEN_WIDTH / 2, 0), down along
6260
// the Y axis till we spot the first white track coloured lighter than
6361
// BLACK_THRESHOLD
64-
struct point find_start(SDL_Surface* png_surface)
62+
struct point find_start(SDL_Surface* png_surface, int height, int width)
6563
{
6664
int x_start, y_start;
6765
struct point start;
6866
int i;
69-
for (i = 0; i < SCREEN_HEIGHT / 2; i++)
67+
for (i = 0; i < height / 2; i++)
7068
{
71-
struct point pt = mkpoint(SCREEN_WIDTH / 2, i);
69+
struct point pt = mkpoint(width / 2, i);
7270
struct pixel pix = get_pixel(png_surface, &pt);
7371
if (pix.r > BLACK_THRESHOLD)
7472
{
75-
x_start = SCREEN_WIDTH / 2;
73+
x_start = width / 2;
7674
y_start = i;
7775
start = mkpoint(x_start, y_start);
7876

@@ -83,14 +81,14 @@ struct point find_start(SDL_Surface* png_surface)
8381
return start;
8482
}
8583

86-
void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surface* png_surface, char* decoded_sound_file)
84+
void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surface* png_surface, char* decoded_sound_file, int height, int width)
8785
{
8886
bool quit = false;
8987
int samples_written = 0;
9088
struct wav_hdr hdr;
9189
SDL_Event e;
9290
FILE *decoded_sound = fopen(decoded_sound_file, "wb");
93-
struct point start = find_start(png_surface);
91+
struct point start = find_start(png_surface, height, width);
9492

9593
if (decoded_sound == NULL)
9694
{
@@ -122,7 +120,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
122120
struct point pnew;
123121
struct pixel pix;
124122

125-
pnew = surface_point_rotate(&start, deg);
123+
pnew = surface_point_rotate(&start, deg, height, width);
126124
pix = get_pixel(png_surface, &pnew);
127125
// check if we are still on the track
128126
if (pix.r > BLACK_THRESHOLD)
@@ -136,7 +134,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
136134

137135
// probe pixels above our point
138136
struct point yinc = mkpoint(start.x, start.y + 2);
139-
pnew = surface_point_rotate(&yinc, deg);
137+
pnew = surface_point_rotate(&yinc, deg, height, width);
140138
pix = get_pixel(png_surface, &pnew);
141139
if (pix.r > BLACK_THRESHOLD)
142140
{
@@ -150,7 +148,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
150148

151149
// probe pixels below our point
152150
struct point ydec = mkpoint(start.x, start.y - 2);
153-
pnew = surface_point_rotate(&ydec, deg);
151+
pnew = surface_point_rotate(&ydec, deg, height, width);
154152
pix = get_pixel(png_surface, &pnew);
155153
if (pix.r > BLACK_THRESHOLD)
156154
{
@@ -164,15 +162,15 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa
164162
// awkward case when we are on the dark spot and pixels
165163
// above and below are also dark - this means that we have
166164
// either reached the end of the plate...
167-
if (looks_like_end(png_surface, start, deg))
165+
if (looks_like_end(png_surface, start, deg, height, width))
168166
{
169167
quit = true;
170168
break;
171169
}
172170

173171
// ...or we have stumbled upon the 'bump' which is lower than
174172
// BLACK_THRESHOLD value but is still on the track
175-
pnew = surface_point_rotate(&start, deg);
173+
pnew = surface_point_rotate(&start, deg, height, width);
176174
pix = get_pixel(png_surface, &pnew);
177175
fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound);
178176
put_red_pixel(png_surface, &pnew);
@@ -194,9 +192,9 @@ int main(int argc, char* args[])
194192
{
195193
SDL_Window* sdl_window = NULL;
196194
//The surface contained by the window
195+
SDL_Surface* loaded_surface = NULL;
197196
SDL_Surface* screen_surface = NULL;
198197
SDL_Surface* png_surface = NULL;
199-
const char *plate_name = args[1];
200198

201199
if (argc < 3)
202200
{
@@ -205,38 +203,50 @@ int main(int argc, char* args[])
205203
return 0;
206204
}
207205

208-
//Start up SDL and create window
209-
if (vinyl_init())
206+
207+
loaded_surface = IMG_Load(args[1]);
208+
if (loaded_surface == NULL)
210209
{
211-
sdl_window = SDL_CreateWindow( "SDL Tutorial", 500, 500, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
212-
if (sdl_window == NULL )
213-
{
214-
printf( "Failed to create SDL Window: %s\n", SDL_GetError() );
215-
}
216-
else
210+
printf("Unable to load image %s: %s\n", args[1], IMG_GetError());
211+
}
212+
else
213+
{
214+
int height = loaded_surface->h;
215+
int widht = loaded_surface->w;
216+
//Start up SDL and create window
217+
if (vinyl_init())
217218
{
218-
screen_surface = SDL_GetWindowSurface(sdl_window);
219-
if (screen_surface == NULL )
219+
sdl_window = SDL_CreateWindow( "SDL Tutorial", 0, 0, widht, height, SDL_WINDOW_SHOWN);
220+
if (sdl_window == NULL )
220221
{
221-
printf("Failed to get window surface: %s\n", SDL_GetError());
222+
printf( "Failed to create SDL Window: %s\n", SDL_GetError() );
222223
}
223224
else
224225
{
225-
png_surface = load_surface(screen_surface, plate_name);
226-
SDL_FreeSurface(screen_surface);
227-
if (png_surface == NULL)
226+
screen_surface = SDL_GetWindowSurface(sdl_window);
227+
if (screen_surface == NULL )
228228
{
229-
printf("Failed to load PNG image: %s\n", SDL_GetError());
229+
printf("Failed to get window surface: %s\n", SDL_GetError());
230230
}
231231
else
232232
{
233-
vinyl_decode(sdl_window, screen_surface, png_surface, args[2]);
234-
SDL_FreeSurface(png_surface);
233+
png_surface = load_surface(screen_surface, loaded_surface);
234+
SDL_FreeSurface(screen_surface);
235+
SDL_FreeSurface(loaded_surface);
236+
if (png_surface == NULL)
237+
{
238+
printf("Failed to load PNG image: %s\n", SDL_GetError());
239+
}
240+
else
241+
{
242+
vinyl_decode(sdl_window, screen_surface, png_surface, args[2], height, widht);
243+
SDL_FreeSurface(png_surface);
244+
}
235245
}
246+
SDL_DestroyWindow(sdl_window);
236247
}
237-
SDL_DestroyWindow(sdl_window);
248+
vinyl_exit();
238249
}
239-
vinyl_exit();
240250
}
241251
return 0;
242252
}

src/surface.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,18 @@
44
#include "surface.h"
55
#include "point.h"
66

7-
SDL_Surface* load_surface(SDL_Surface* screen_surface, const char* path)
7+
SDL_Surface* load_surface(SDL_Surface* screen_surface, SDL_Surface* loadedSurface)
88
{
99
//The final optimized image
1010
SDL_Surface* optimizedSurface = NULL;
1111

12-
//Load image at specified path
13-
SDL_Surface* loadedSurface = IMG_Load(path);
14-
if (loadedSurface == NULL)
15-
{
16-
printf("Unable to load image %s: %s\n", path, IMG_GetError());
17-
goto out;
18-
}
19-
2012
//Convert surface to screen format
2113
optimizedSurface = SDL_ConvertSurface(loadedSurface, screen_surface->format, 0);
2214
if (optimizedSurface == NULL)
2315
{
24-
printf("Unable to optimize image %s: %s\n", path, SDL_GetError());
16+
printf("Unable to optimize image: %s\n", SDL_GetError());
2517
}
2618

27-
out:
2819
return optimizedSurface;
2920
}
3021

src/surface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ struct pixel
88
Uint8 b;
99
};
1010

11-
SDL_Surface* load_surface(SDL_Surface *screen, const char* path);
11+
SDL_Surface* load_surface(SDL_Surface *screen, SDL_Surface *loadedSurface);
1212
void put_red_pixel(SDL_Surface *surface, const struct point *pt);
1313
struct pixel get_pixel(SDL_Surface *surface, const struct point *pt);

src/vinyl.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,17 @@ bool vinyl_init()
1111
{
1212
printf( "Failed to initialize SDL: %s\n", SDL_GetError());
1313
success = false;
14-
goto out;
1514
}
16-
17-
int imgFlags = IMG_INIT_PNG;
18-
if (!(IMG_Init(imgFlags) & imgFlags))
15+
else
1916
{
20-
printf( "Failed to initialize SDL_image: %s\n", IMG_GetError() );
21-
success = false;
22-
goto img_init_fail;
17+
int imgFlags = IMG_INIT_PNG;
18+
if (!(IMG_Init(imgFlags) & imgFlags))
19+
{
20+
printf( "Failed to initialize SDL_image: %s\n", IMG_GetError() );
21+
success = false;
22+
SDL_Quit();
23+
}
2324
}
24-
25-
out:
26-
return success;
27-
img_init_fail:
28-
SDL_Quit();
2925
return success;
3026
}
3127

0 commit comments

Comments
 (0)