Add ability to edit game name from UI
[jpcrr.git] / streamtools / testresizer.cpp
blobeea4a178592496db2cec5f591d81254889b31965
1 #include "SDL_image.h"
2 #include "SDL.h"
3 #include "resize.hpp"
4 #include "rescalers/public.hpp"
5 #include <zlib.h>
6 #include <cstdlib>
7 #include <stdint.h>
8 #include <string>
9 #include <iostream>
10 #include <vector>
11 #include "newpacket.hpp"
12 #include <stdexcept>
14 namespace
16 bool do_quit_on(SDL_Event* e)
18 if(e->type == SDL_QUIT)
19 return true;
20 if(e->type == SDL_KEYUP && e->key.keysym.sym == 'q')
21 return true;
22 return false;
26 int real_main(int argc, char** argv)
28 if(argc < 5) {
29 std::cerr << "Syntax: testresizer.exe <picture> <algo> <width> <height>" << std::endl;
30 exit(1);
33 char* ptr1;
34 char* ptr2;
35 unsigned twidth = strtoul(argv[3], &ptr1, 10);
36 unsigned theight = strtoul(argv[4], &ptr2, 10);
37 if(!twidth || !theight || *ptr1 || *ptr2) {
38 std::cerr << "Error: Bad target size." << std::endl;
39 exit(1);
43 //Load the image.
44 SDL_Surface* img = IMG_Load(argv[1]);
45 if(!img) {
46 std::cerr << "Can't load image '" << argv[1] << "':" << IMG_GetError() << std::endl;
47 exit(2);
50 //Convert the SDL surface into raw image.
51 unsigned width = img->w;
52 unsigned height = img->h;
53 image_frame_rgbx src(width, height);
54 unsigned char* pixels = src.get_pixels();
55 SDL_LockSurface(img);
56 for(uint32_t y = 0; y < height; y++)
57 for(uint32_t x = 0; x < width; x++) {
58 Uint8 r, g, b;
59 size_t addr = y * img->pitch + x * img->format->BytesPerPixel;
60 if(img->format->BytesPerPixel == 4)
61 SDL_GetRGB(*(Uint32*)((unsigned char*)img->pixels + addr), img->format, &r, &g, &b);
62 else if(img->format->BytesPerPixel == 3)
63 SDL_GetRGB(*(Uint32*)((unsigned char*)img->pixels + addr) & 0xFFFFFF, img->format,
64 &r, &g, &b);
65 else if(img->format->BytesPerPixel == 2)
66 SDL_GetRGB(*(Uint16*)((unsigned char*)img->pixels + addr), img->format, &r, &g, &b);
67 else
68 SDL_GetRGB(*(Uint8*)((unsigned char*)img->pixels + addr), img->format, &r, &g, &b);
69 pixels[4 * width * y + 4 * x + 0] = r;
70 pixels[4 * width * y + 4 * x + 1] = g;
71 pixels[4 * width * y + 4 * x + 2] = b;
72 pixels[4 * width * y + 4 * x + 3] = 0;
74 SDL_UnlockSurface(img);
75 SDL_FreeSurface(img);
77 rescaler_group grp(*(parse_rescaler_expression(argv[2]).use_rescaler));
78 image_frame_rgbx& dest = src.resize(twidth, theight, grp);
80 //Now, display the image.
81 SDL_Surface* swsurf = NULL;
82 SDL_Surface* hwsurf = NULL;
84 if(SDL_Init(SDL_INIT_VIDEO) < 0) {
85 std::cerr << "Can't Initialize SDL." << std::endl;
86 exit(2);
89 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
90 uint32_t rmask = 0xFF000000;
91 uint32_t gmask = 0x00FF0000;
92 uint32_t bmask = 0x0000FF00;
93 #else
94 uint32_t rmask = 0x000000FF;
95 uint32_t gmask = 0x0000FF00;
96 uint32_t bmask = 0x00FF0000;
97 #endif
99 hwsurf = SDL_SetVideoMode(dest.get_width(), dest.get_height(), 0, SDL_SWSURFACE |
100 SDL_DOUBLEBUF | SDL_ANYFORMAT);
101 swsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, dest.get_width(), dest.get_height(), 32,
102 rmask, gmask, bmask, 0);
103 if(!swsurf || !hwsurf) {
104 std::cerr << "Can't Set video mode." << std::endl;
105 exit(2);
108 //Copy the picture to surface.
109 SDL_LockSurface(swsurf);
110 memcpy((unsigned char*)swsurf->pixels, dest.get_pixels(), 4 * twidth * theight);
111 SDL_UnlockSurface(swsurf);
113 //Render and wait.
114 SDL_BlitSurface(swsurf, NULL, hwsurf, NULL);
115 SDL_Flip(hwsurf);
116 SDL_Event e;
117 while(SDL_PollEvent(&e) != 1 || !do_quit_on(&e));
119 if(&dest != &src)
120 delete &dest;
121 return 0;