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}
0 commit comments