Skip to content

Commit

Permalink
2.0: custom image size
Browse files Browse the repository at this point in the history
  • Loading branch information
zvezdochiot committed Nov 11, 2023
1 parent 79dd43c commit e40b04b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 62 deletions.
86 changes: 48 additions & 38 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -38,15 +36,15 @@ 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;
struct point pt;
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)
{
Expand All @@ -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);

Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -164,15 +162,15 @@ 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;
}

// ...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);
Expand All @@ -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)
{
Expand All @@ -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;
}
Expand Down
13 changes: 2 additions & 11 deletions src/surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
20 changes: 8 additions & 12 deletions src/vinyl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit e40b04b

Please sign in to comment.