Resize algorithm tester
[jpcrr.git] / streamtools / testresizer.cpp
blob2e260fae1f273f005986234e7e84b82248b93332
1 #include "SDL_image.h"
2 #include "SDL.h"
3 #include "resize.hpp"
4 #include <zlib.h>
5 #include <cstdlib>
6 #include <stdint.h>
7 #include <string>
8 #include <iostream>
9 #include <vector>
10 #include "newpacket.hpp"
11 #include <stdexcept>
13 int main(int argc, char** argv)
15 if(argc < 5) {
16 std::cerr << "Syntax: testresizer.exe <picture> <algo> <width> <height>" << std::endl;
17 exit(1);
20 char* ptr1;
21 char* ptr2;
22 unsigned twidth = strtoul(argv[3], &ptr1, 10);
23 unsigned theight = strtoul(argv[4], &ptr2, 10);
24 if(!twidth || !theight || *ptr1 || *ptr2) {
25 std::cerr << "Error: Bad target size." << std::endl;
26 exit(1);
30 //Load the image.
31 SDL_Surface* img = IMG_Load(argv[1]);
32 if(!img) {
33 std::cerr << "Can't load image '" << argv[1] << "':" << IMG_GetError() << std::endl;
34 exit(2);
37 //Convert the SDL surface into raw image.
38 unsigned width = img->w;
39 unsigned height = img->h;
40 image_frame_rgbx src(width, height);
41 unsigned char* pixels = src.get_pixels();
42 SDL_LockSurface(img);
43 for(uint32_t y = 0; y < height; y++)
44 for(uint32_t x = 0; x < width; x++) {
45 Uint8 r, g, b;
46 size_t addr = y * img->pitch + x * img->format->BytesPerPixel;
47 SDL_GetRGB(*(Uint32*)((unsigned char*)img->pixels + addr), img->format, &r, &g, &b);
48 pixels[4 * width * y + 4 * x + 0] = r;
49 pixels[4 * width * y + 4 * x + 1] = g;
50 pixels[4 * width * y + 4 * x + 2] = b;
51 pixels[4 * width * y + 4 * x + 3] = 0;
53 SDL_UnlockSurface(img);
54 SDL_FreeSurface(img);
56 resizer& r = resizer_factory::make_by_type(argv[2]);
57 image_frame_rgbx& dest = src.resize(twidth, theight, r);
59 //Now, display the image.
60 SDL_Surface* swsurf = NULL;
61 SDL_Surface* hwsurf = NULL;
63 if(SDL_Init(SDL_INIT_VIDEO) < 0) {
64 std::cerr << "Can't Initialize SDL." << std::endl;
65 exit(2);
68 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
69 uint32_t rmask = 0xFF000000;
70 uint32_t gmask = 0x00FF0000;
71 uint32_t bmask = 0x0000FF00;
72 #else
73 uint32_t rmask = 0x000000FF;
74 uint32_t gmask = 0x0000FF00;
75 uint32_t bmask = 0x00FF0000;
76 #endif
78 hwsurf = SDL_SetVideoMode(dest.get_width(), dest.get_height(), 0, SDL_SWSURFACE |
79 SDL_DOUBLEBUF | SDL_ANYFORMAT);
80 swsurf = SDL_CreateRGBSurface(SDL_SWSURFACE, dest.get_width(), dest.get_height(), 32,
81 rmask, gmask, bmask, 0);
82 if(!swsurf || !hwsurf) {
83 std::cerr << "Can't Set video mode." << std::endl;
84 exit(2);
87 //Copy the picture to surface.
88 SDL_LockSurface(swsurf);
89 memcpy((unsigned char*)swsurf->pixels, dest.get_pixels(), 4 * twidth * theight);
90 SDL_UnlockSurface(swsurf);
92 //Render and wait.
93 SDL_BlitSurface(swsurf, NULL, hwsurf, NULL);
94 SDL_Flip(hwsurf);
95 SDL_Event e;
96 while(SDL_PollEvent(&e) != 1 || e.type != SDL_QUIT);
98 if(&dest != &src)
99 delete &dest;
100 return 0;