diff --git a/src/main.c b/src/main.c index 4aef8a0..6ace4bd 100644 --- a/src/main.c +++ b/src/main.c @@ -11,17 +11,15 @@ #define BLACK_THRESHOLD 100 #define DEGREE_STRIDE 0.1 -#define SCREEN_WIDTH 2000 -#define SCREEN_HEIGHT 2000 -struct point surface_point_rotate(const struct point *p, double degree) +struct point surface_point_rotate(const struct point *p, double degree, int height, int width) { // since point is rotated around the center (0,0), we substract // half of the screen WIDTH and HEIGHT from X and Y so as to move // it to the center (image coordinates are always positive and the // (0, 0) position is in the top left corner - int x_offset = SCREEN_WIDTH / 2; - int y_offset = SCREEN_HEIGHT / 2; + int x_offset = width / 2; + int y_offset = height / 2; struct point aligned_pt = mkpoint(p->x - x_offset, p->y - y_offset); struct point rotated_pt = point_rotate(&aligned_pt, degree); @@ -38,7 +36,7 @@ struct point surface_point_rotate(const struct point *p, double degree) // If we do not encounter any tracks on the way, we assume that we // have reached the end of the plate (last track before large black // circle in the middle of the plate) -bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg) +bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg, int height, int width) { const int dead_end_pixel_count = 30; int black_pixels = 0; @@ -46,7 +44,7 @@ bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg) struct pixel pix; while (black_pixels < dead_end_pixel_count) { - pt = surface_point_rotate(&start, deg); + pt = surface_point_rotate(&start, deg, height, width); pix = get_pixel(png_surface, &pt); if (pix.r > BLACK_THRESHOLD) { @@ -61,18 +59,18 @@ bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg) // crawl from the image top center (SCREEN_WIDTH / 2, 0), down along // the Y axis till we spot the first white track coloured lighter than // BLACK_THRESHOLD -struct point find_start(SDL_Surface* png_surface) +struct point find_start(SDL_Surface* png_surface, int height, int width) { int x_start, y_start; struct point start; int i; - for (i = 0; i < SCREEN_HEIGHT / 2; i++) + for (i = 0; i < height / 2; i++) { - struct point pt = mkpoint(SCREEN_WIDTH / 2, i); + struct point pt = mkpoint(width / 2, i); struct pixel pix = get_pixel(png_surface, &pt); if (pix.r > BLACK_THRESHOLD) { - x_start = SCREEN_WIDTH / 2; + x_start = width / 2; y_start = i; start = mkpoint(x_start, y_start); @@ -83,14 +81,14 @@ struct point find_start(SDL_Surface* png_surface) return start; } -void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surface* png_surface, char* decoded_sound_file) +void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surface* png_surface, char* decoded_sound_file, int height, int width) { bool quit = false; int samples_written = 0; struct wav_hdr hdr; SDL_Event e; FILE *decoded_sound = fopen(decoded_sound_file, "wb"); - struct point start = find_start(png_surface); + struct point start = find_start(png_surface, height, width); if (decoded_sound == NULL) { @@ -122,7 +120,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa struct point pnew; struct pixel pix; - pnew = surface_point_rotate(&start, deg); + pnew = surface_point_rotate(&start, deg, height, width); pix = get_pixel(png_surface, &pnew); // check if we are still on the track if (pix.r > BLACK_THRESHOLD) @@ -136,7 +134,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa // probe pixels above our point struct point yinc = mkpoint(start.x, start.y + 2); - pnew = surface_point_rotate(&yinc, deg); + pnew = surface_point_rotate(&yinc, deg, height, width); pix = get_pixel(png_surface, &pnew); if (pix.r > BLACK_THRESHOLD) { @@ -150,7 +148,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa // probe pixels below our point struct point ydec = mkpoint(start.x, start.y - 2); - pnew = surface_point_rotate(&ydec, deg); + pnew = surface_point_rotate(&ydec, deg, height, width); pix = get_pixel(png_surface, &pnew); if (pix.r > BLACK_THRESHOLD) { @@ -164,7 +162,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa // awkward case when we are on the dark spot and pixels // above and below are also dark - this means that we have // either reached the end of the plate... - if (looks_like_end(png_surface, start, deg)) + if (looks_like_end(png_surface, start, deg, height, width)) { quit = true; break; @@ -172,7 +170,7 @@ void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surfa // ...or we have stumbled upon the 'bump' which is lower than // BLACK_THRESHOLD value but is still on the track - pnew = surface_point_rotate(&start, deg); + pnew = surface_point_rotate(&start, deg, height, width); pix = get_pixel(png_surface, &pnew); fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound); put_red_pixel(png_surface, &pnew); @@ -194,9 +192,9 @@ int main(int argc, char* args[]) { SDL_Window* sdl_window = NULL; //The surface contained by the window + SDL_Surface* loaded_surface = NULL; SDL_Surface* screen_surface = NULL; SDL_Surface* png_surface = NULL; - const char *plate_name = args[1]; if (argc < 3) { @@ -205,38 +203,50 @@ int main(int argc, char* args[]) return 0; } - //Start up SDL and create window - if (vinyl_init()) + + loaded_surface = IMG_Load(args[1]); + if (loaded_surface == NULL) { - sdl_window = SDL_CreateWindow( "SDL Tutorial", 500, 500, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); - if (sdl_window == NULL ) - { - printf( "Failed to create SDL Window: %s\n", SDL_GetError() ); - } - else + printf("Unable to load image %s: %s\n", args[1], IMG_GetError()); + } + else + { + int height = loaded_surface->h; + int widht = loaded_surface->w; + //Start up SDL and create window + if (vinyl_init()) { - screen_surface = SDL_GetWindowSurface(sdl_window); - if (screen_surface == NULL ) + sdl_window = SDL_CreateWindow( "SDL Tutorial", 0, 0, widht, height, SDL_WINDOW_SHOWN); + if (sdl_window == NULL ) { - printf("Failed to get window surface: %s\n", SDL_GetError()); + printf( "Failed to create SDL Window: %s\n", SDL_GetError() ); } else { - png_surface = load_surface(screen_surface, plate_name); - SDL_FreeSurface(screen_surface); - if (png_surface == NULL) + screen_surface = SDL_GetWindowSurface(sdl_window); + if (screen_surface == NULL ) { - printf("Failed to load PNG image: %s\n", SDL_GetError()); + printf("Failed to get window surface: %s\n", SDL_GetError()); } else { - vinyl_decode(sdl_window, screen_surface, png_surface, args[2]); - SDL_FreeSurface(png_surface); + png_surface = load_surface(screen_surface, loaded_surface); + SDL_FreeSurface(screen_surface); + SDL_FreeSurface(loaded_surface); + if (png_surface == NULL) + { + printf("Failed to load PNG image: %s\n", SDL_GetError()); + } + else + { + vinyl_decode(sdl_window, screen_surface, png_surface, args[2], height, widht); + SDL_FreeSurface(png_surface); + } } + SDL_DestroyWindow(sdl_window); } - SDL_DestroyWindow(sdl_window); + vinyl_exit(); } - vinyl_exit(); } return 0; } diff --git a/src/surface.c b/src/surface.c index 9d236f9..5fababf 100644 --- a/src/surface.c +++ b/src/surface.c @@ -4,27 +4,18 @@ #include "surface.h" #include "point.h" -SDL_Surface* load_surface(SDL_Surface* screen_surface, const char* path) +SDL_Surface* load_surface(SDL_Surface* screen_surface, SDL_Surface* loadedSurface) { //The final optimized image SDL_Surface* optimizedSurface = NULL; - //Load image at specified path - SDL_Surface* loadedSurface = IMG_Load(path); - if (loadedSurface == NULL) - { - printf("Unable to load image %s: %s\n", path, IMG_GetError()); - goto out; - } - //Convert surface to screen format optimizedSurface = SDL_ConvertSurface(loadedSurface, screen_surface->format, 0); if (optimizedSurface == NULL) { - printf("Unable to optimize image %s: %s\n", path, SDL_GetError()); + printf("Unable to optimize image: %s\n", SDL_GetError()); } -out: return optimizedSurface; } diff --git a/src/surface.h b/src/surface.h index 18b08b7..83d394c 100644 --- a/src/surface.h +++ b/src/surface.h @@ -8,6 +8,6 @@ struct pixel Uint8 b; }; -SDL_Surface* load_surface(SDL_Surface *screen, const char* path); +SDL_Surface* load_surface(SDL_Surface *screen, SDL_Surface *loadedSurface); void put_red_pixel(SDL_Surface *surface, const struct point *pt); struct pixel get_pixel(SDL_Surface *surface, const struct point *pt); diff --git a/src/vinyl.c b/src/vinyl.c index 835546e..34cb6a1 100644 --- a/src/vinyl.c +++ b/src/vinyl.c @@ -11,21 +11,17 @@ bool vinyl_init() { printf( "Failed to initialize SDL: %s\n", SDL_GetError()); success = false; - goto out; } - - int imgFlags = IMG_INIT_PNG; - if (!(IMG_Init(imgFlags) & imgFlags)) + else { - printf( "Failed to initialize SDL_image: %s\n", IMG_GetError() ); - success = false; - goto img_init_fail; + int imgFlags = IMG_INIT_PNG; + if (!(IMG_Init(imgFlags) & imgFlags)) + { + printf( "Failed to initialize SDL_image: %s\n", IMG_GetError() ); + success = false; + SDL_Quit(); + } } - -out: - return success; -img_init_fail: - SDL_Quit(); return success; }