|
| 1 | +#include <SDL2/SDL.h> |
| 2 | +#include <SDL2/SDL_image.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <string.h> |
| 5 | +#include <unistd.h> |
| 6 | + |
| 7 | +#include "wav.h" |
| 8 | +#include "vinyl.h" |
| 9 | +#include "point.h" |
| 10 | +#include "surface.h" |
| 11 | + |
| 12 | +#define DECODED_SOUND_FILE "vinyl_decoded.wav" |
| 13 | +#define BLACK_THRESHOLD 200 |
| 14 | +#define DEGREE_STRIDE 0.1 |
| 15 | +#define SCREEN_WIDTH 2000 |
| 16 | +#define SCREEN_HEIGHT 2000 |
| 17 | + |
| 18 | +struct point surface_point_rotate(const struct point *p, double degree) |
| 19 | +{ |
| 20 | + // since point is rotated around the center (0,0), we substract |
| 21 | + // half of the screen WIDTH and HEIGHT from X and Y so as to move |
| 22 | + // it to the center (image coordinates are always positive and the |
| 23 | + // (0, 0) position is in the top left corner |
| 24 | + int x_offset = SCREEN_WIDTH / 2; |
| 25 | + int y_offset = SCREEN_HEIGHT / 2; |
| 26 | + |
| 27 | + struct point aligned_pt = mkpoint(p->x - x_offset, p->y - y_offset); |
| 28 | + struct point rotated_pt = point_rotate(&aligned_pt, degree); |
| 29 | + |
| 30 | + return mkpoint(rotated_pt.x + x_offset, rotated_pt.y + y_offset); |
| 31 | +} |
| 32 | + |
| 33 | +// NOTE: since the plate is black & white, each pixel will have R == G == B |
| 34 | +// (shades of grey). We use red pixel component but using G or B will produce |
| 35 | +// the same result |
| 36 | + |
| 37 | +// crawl 30 pixels in the direction of the plate center from the point equal to |
| 38 | +// 'start', rotated 'deg' degree counterclockwise |
| 39 | +// If we do not encounter any tracks on the way, we assume that we |
| 40 | +// have reached the end of the plate (last track before large black |
| 41 | +// circle in the middle of the plate) |
| 42 | +bool looks_like_end(SDL_Surface *png_surface, struct point start, double deg) |
| 43 | +{ |
| 44 | + const int dead_end_pixel_count = 30; |
| 45 | + int black_pixels = 0; |
| 46 | + struct point pt; |
| 47 | + struct pixel pix; |
| 48 | + while (black_pixels < dead_end_pixel_count) { |
| 49 | + pt = surface_point_rotate(&start, deg); |
| 50 | + pix = get_pixel(png_surface, &pt); |
| 51 | + if (pix.r > BLACK_THRESHOLD) { |
| 52 | + break; |
| 53 | + } |
| 54 | + start.y++; |
| 55 | + black_pixels++; |
| 56 | + } |
| 57 | + return black_pixels == dead_end_pixel_count; |
| 58 | +} |
| 59 | + |
| 60 | +// crawl from the image top center (SCREEN_WIDTH / 2, 0), down along |
| 61 | +// the Y axis till we spot the first white track coloured lighter than |
| 62 | +// BLACK_THRESHOLD |
| 63 | +struct point find_start(SDL_Surface* png_surface) |
| 64 | +{ |
| 65 | + int x_start, y_start; |
| 66 | + struct point start; |
| 67 | + int i; |
| 68 | + for (i = 0; i < SCREEN_HEIGHT / 2; i++) { |
| 69 | + struct point pt = mkpoint(SCREEN_WIDTH / 2, i); |
| 70 | + struct pixel pix = get_pixel(png_surface, &pt); |
| 71 | + if (pix.r > BLACK_THRESHOLD) { |
| 72 | + x_start = SCREEN_WIDTH / 2; |
| 73 | + y_start = i; |
| 74 | + start = mkpoint(x_start, y_start); |
| 75 | + |
| 76 | + put_red_pixel(png_surface, &start); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + return start; |
| 81 | +} |
| 82 | + |
| 83 | +void vinyl_decode(SDL_Window* sdl_window, SDL_Surface* screen_surface, SDL_Surface* png_surface) |
| 84 | +{ |
| 85 | + bool quit = false; |
| 86 | + int samples_written = 0; |
| 87 | + struct wav_hdr hdr; |
| 88 | + SDL_Event e; |
| 89 | + FILE *decoded_sound = fopen(DECODED_SOUND_FILE, "w"); |
| 90 | + struct point start = find_start(png_surface); |
| 91 | + |
| 92 | + if (decoded_sound == NULL) { |
| 93 | + perror("Failed to write decoded data"); |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + // save the place for wav header, which we will use to write |
| 98 | + // the actual header |
| 99 | + // We do not write it in advance since we have to know |
| 100 | + // how much samples we have read to put that count into wav_hdr |
| 101 | + fseek(decoded_sound, sizeof(hdr), SEEK_SET); |
| 102 | + |
| 103 | + while (!quit) { |
| 104 | + while (SDL_PollEvent(&e) != 0) { |
| 105 | + if (e.type == SDL_QUIT) { |
| 106 | + quit = true; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + SDL_BlitSurface(png_surface, NULL, screen_surface, NULL); |
| 111 | + |
| 112 | + double deg; |
| 113 | + for (deg = 0; deg > -360.0; deg -= DEGREE_STRIDE) { |
| 114 | + struct point pnew; |
| 115 | + struct pixel pix; |
| 116 | + |
| 117 | + pnew = surface_point_rotate(&start, deg); |
| 118 | + pix = get_pixel(png_surface, &pnew); |
| 119 | + // check if we are still on the track |
| 120 | + if (pix.r > BLACK_THRESHOLD) { |
| 121 | + fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound); |
| 122 | + samples_written++; |
| 123 | + put_red_pixel(png_surface, &pnew); |
| 124 | + continue; |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + // probe pixels above our point |
| 129 | + struct point yinc = mkpoint(start.x, start.y + 2); |
| 130 | + pnew = surface_point_rotate(&yinc, deg); |
| 131 | + pix = get_pixel(png_surface, &pnew); |
| 132 | + if (pix.r > BLACK_THRESHOLD) { |
| 133 | + start = yinc; |
| 134 | + fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound); |
| 135 | + samples_written++; |
| 136 | + put_red_pixel(png_surface, &pnew); |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + // probe pixels below our point |
| 142 | + struct point ydec = mkpoint(start.x, start.y - 2); |
| 143 | + pnew = surface_point_rotate(&ydec, deg); |
| 144 | + pix = get_pixel(png_surface, &pnew); |
| 145 | + if (pix.r > BLACK_THRESHOLD) { |
| 146 | + start = ydec; |
| 147 | + fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound); |
| 148 | + samples_written++; |
| 149 | + put_red_pixel(png_surface, &pnew); |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + // awkward case when we are on the dark spot and pixels |
| 154 | + // above and below are also dark - this means that we have |
| 155 | + // either reached the end of the plate... |
| 156 | + if (looks_like_end(png_surface, start, deg)) { |
| 157 | + quit = true; |
| 158 | + break; |
| 159 | + } |
| 160 | + |
| 161 | + // ...or we have stumbled upon the 'bump' which is lower than |
| 162 | + // BLACK_THRESHOLD value but is still on the track |
| 163 | + pnew = surface_point_rotate(&start, deg); |
| 164 | + pix = get_pixel(png_surface, &pnew); |
| 165 | + fwrite(&pix.r, sizeof(pix.r), 1, decoded_sound); |
| 166 | + put_red_pixel(png_surface, &pnew); |
| 167 | + samples_written++; |
| 168 | + } |
| 169 | + |
| 170 | + SDL_UpdateWindowSurface(sdl_window); |
| 171 | + } |
| 172 | + |
| 173 | + // replace placeholder wav_hdr struct in the beginning of file with the actual one |
| 174 | + hdr = mk_wav_hdr(samples_written); |
| 175 | + rewind(decoded_sound); |
| 176 | + fwrite(&hdr, sizeof(hdr), 1, decoded_sound); |
| 177 | + fclose(decoded_sound); |
| 178 | + printf("wrote samples: %d\n", samples_written); |
| 179 | + usleep(1000000); |
| 180 | +} |
| 181 | +int main(int argc, char* args[]) |
| 182 | +{ |
| 183 | + SDL_Window* sdl_window = NULL; |
| 184 | + //The surface contained by the window |
| 185 | + SDL_Surface* screen_surface = NULL; |
| 186 | + SDL_Surface* png_surface = NULL; |
| 187 | + const char *plate_name = args[1]; |
| 188 | + |
| 189 | + if (argc != 2) { |
| 190 | + printf("Usage:\n" |
| 191 | + "%s <vinyl-image-to-decode>\n", args[0]); |
| 192 | + goto fail; |
| 193 | + } |
| 194 | + |
| 195 | + //Start up SDL and create window |
| 196 | + if (!vinyl_init()) { |
| 197 | + goto fail; |
| 198 | + } |
| 199 | + |
| 200 | + sdl_window = SDL_CreateWindow( "SDL Tutorial", 500, 500, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); |
| 201 | + if (sdl_window == NULL ) { |
| 202 | + printf( "Failed to create SDL Window: %s\n", SDL_GetError() ); |
| 203 | + goto window_creation_fail; |
| 204 | + } |
| 205 | + |
| 206 | + screen_surface = SDL_GetWindowSurface(sdl_window); |
| 207 | + if (screen_surface == NULL ) { |
| 208 | + printf("Failed to get window surface: %s\n", SDL_GetError()); |
| 209 | + goto surface_creation_fail; |
| 210 | + } |
| 211 | + |
| 212 | + png_surface = load_surface(screen_surface, plate_name); |
| 213 | + SDL_FreeSurface(screen_surface); |
| 214 | + if (png_surface == NULL) { |
| 215 | + printf("Failed to load PNG image: %s\n", SDL_GetError()); |
| 216 | + goto surface_creation_fail; |
| 217 | + } |
| 218 | + |
| 219 | + vinyl_decode(sdl_window, screen_surface, png_surface); |
| 220 | + |
| 221 | + SDL_FreeSurface(png_surface); |
| 222 | +surface_creation_fail: |
| 223 | + SDL_DestroyWindow(sdl_window); |
| 224 | +window_creation_fail: |
| 225 | + vinyl_exit(); |
| 226 | +fail: |
| 227 | + return 0; |
| 228 | +} |
| 229 | + |
0 commit comments