|
8 | 8 |
|
9 | 9 | /// -------------- FUNCTIONS IMPLEMENTATION -------------- |
10 | 10 |
|
11 | | -/* Nearest neighboor optimized with possible out of screen coordinates (for cropping) */ |
12 | | -void flip_NNOptimized_AllowOutOfScreen(SDL_Surface *src_surface, SDL_Surface *dst_surface, int new_w, int new_h){ |
13 | | - int w1=src_surface->w; |
14 | | - //int h1=src_surface->h; |
15 | | - int w2=new_w; |
16 | | - int h2=new_h; |
17 | | - int x_ratio = (int)((src_surface->w<<16)/w2); |
18 | | - int y_ratio = (int)((src_surface->h<<16)/h2); |
| 11 | +/// Nearest neighboor optimized with possible out of screen coordinates (for cropping) |
| 12 | +SDL_Surface * zoomSurface(SDL_Surface *src_surface, int dst_width, int dst_height){ |
| 13 | + |
| 14 | + /* Declare vars */ |
| 15 | + int x_ratio; |
| 16 | + int y_ratio; |
19 | 17 | int x2, y2; |
20 | 18 | int i, j; |
| 19 | + int rat; |
21 | 20 |
|
22 | | - /* Compute padding for centering when out of bounds */ |
23 | | - int x_padding = 0, y_padding = 0; |
24 | | - if(h2>SCREEN_HORIZONTAL_SIZE){ |
25 | | - y_padding = (SCREEN_VERTICAL_SIZE-new_h)/2; |
| 21 | + /* Sanity checks */ |
| 22 | + if(src_surface == NULL){ |
| 23 | + printf("ERROR in %s, sanity check\n", __func__); |
| 24 | + return NULL; |
26 | 25 | } |
27 | | - if(w2>SCREEN_HORIZONTAL_SIZE){ |
28 | | - x_padding = (w2-SCREEN_HORIZONTAL_SIZE)/2 + 1; |
| 26 | + |
| 27 | + /* Compute zoom ratio */ |
| 28 | + x_ratio = (int)((src_surface->w << 16) / dst_width); |
| 29 | + y_ratio = (int)((src_surface->h << 16) / dst_height); |
| 30 | + |
| 31 | + /* Create dst surface */ |
| 32 | + SDL_Surface *dst_surface = SDL_CreateRGBSurface(src_surface->flags, |
| 33 | + dst_width, dst_height, |
| 34 | + src_surface->format->BitsPerPixel, |
| 35 | + src_surface->format->Rmask, src_surface->format->Gmask, |
| 36 | + src_surface->format->Bmask, src_surface->format->Amask); |
| 37 | + if(dst_surface == NULL){ |
| 38 | + printf("ERROR in %s, cannot create dst_surface: %s\n", __func__, SDL_GetError()); |
29 | 39 | } |
30 | | - int x_padding_ratio = x_padding*w1/w2; |
31 | 40 |
|
32 | | - /* Copy pixels NN */ |
33 | | - for (i=0;i<h2;i++) |
| 41 | + /* Columns iterations */ |
| 42 | + for (i = 0; i < dst_surface->h; i++) |
34 | 43 | { |
35 | | - if(i>=SCREEN_VERTICAL_SIZE){ |
36 | | - continue; |
37 | | - } |
38 | 44 |
|
39 | | - uint16_t* t = (uint16_t*)(dst_surface->pixels+((i+y_padding)* ((w2>SCREEN_HORIZONTAL_SIZE)?SCREEN_HORIZONTAL_SIZE:w2) )*sizeof(uint16_t)); |
| 45 | + /* Get current lines in src and dst surfaces */ |
| 46 | + uint8_t* t = ( (uint8_t*) dst_surface->pixels + (i*dst_surface->w)*dst_surface->format->BytesPerPixel ); |
40 | 47 | y2 = ((i*y_ratio)>>16); |
41 | | - uint16_t* p = (uint16_t*)(src_surface->pixels + (y2*w1 + x_padding_ratio) *sizeof(uint16_t)); |
42 | | - int rat = 0; |
43 | | - for (j=0;j<w2;j++) |
| 48 | + uint8_t* p = ( (uint8_t*) src_surface->pixels + (y2*src_surface->w)*src_surface->format->BytesPerPixel ); |
| 49 | + rat = 0; |
| 50 | + |
| 51 | + /* Lines iterations */ |
| 52 | + for (j = 0; j < dst_surface->w; j++) |
44 | 53 | { |
45 | | - if(j>=SCREEN_HORIZONTAL_SIZE){ |
46 | | - continue; |
47 | | - } |
| 54 | + |
| 55 | + /* Get current pixel in src surface */ |
48 | 56 | x2 = (rat>>16); |
49 | | - *t++ = p[x2]; |
| 57 | + |
| 58 | + /* Copy src pixel in dst surface */ |
| 59 | + memcpy(t, p+x2*src_surface->format->BytesPerPixel, dst_surface->format->BytesPerPixel); |
| 60 | + t += dst_surface->format->BytesPerPixel; |
| 61 | + |
| 62 | + /* Update x position in source surface */ |
50 | 63 | rat += x_ratio; |
51 | | - } |
| 64 | + } |
52 | 65 | } |
| 66 | + |
| 67 | + /* Return new zoomed surface */ |
| 68 | + return dst_surface; |
53 | 69 | } |
54 | 70 |
|
55 | 71 |
|
| 72 | + |
| 73 | + |
56 | 74 | int launch_prod_screen_showImage(int argc, char *argv[]){ |
57 | 75 | SDL_Event event; |
58 | 76 | SDL_Surface *text_surface = NULL; |
59 | 77 | SDL_Rect text_pos; |
60 | 78 | int res = EXIT_FAILURE; |
61 | 79 | int stop_menu_loop = 0; |
62 | 80 |
|
63 | | - /* Load Img */ |
64 | | - char *img_path = argv[0]; |
65 | | - SDL_Surface *image=IMG_Load(img_path); |
66 | | - if(!image) { |
67 | | - printf("ERROR IMG_Load: %s\n", IMG_GetError()); |
68 | | - printf("IMG path is: %s\n", img_path); |
69 | | - exit(1); |
70 | | - } |
71 | | - |
72 | 81 | /* Fill screen white */ |
73 | | - SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b)); |
| 82 | + SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) ); |
74 | 83 |
|
75 | 84 | /* Write Title */ |
76 | 85 | text_surface = TTF_RenderText_Shaded(font_info, "SCAN & PRINT", text_color, bg_color); |
@@ -101,22 +110,34 @@ int launch_prod_screen_showImage(int argc, char *argv[]){ |
101 | 110 | SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos); |
102 | 111 | SDL_FreeSurface(text_surface); |
103 | 112 |
|
104 | | - /* Convert img to RGB565 */ |
105 | | - SDL_Surface *image_rgb565 = SDL_CreateRGBSurface(SDL_SWSURFACE, image->w, image->h, 16, 0, 0, 0, 0); |
106 | | - SDL_BlitSurface(image, NULL, image_rgb565, NULL); |
| 113 | + /* Load Img */ |
| 114 | + char *img_path = argv[0]; |
| 115 | + SDL_Surface *image=IMG_Load(img_path); |
| 116 | + if(!image) { |
| 117 | + printf("ERROR IMG_Load: %s\n", IMG_GetError()); |
| 118 | + printf("IMG path is: %s\n", img_path); |
| 119 | + exit(1); |
| 120 | + } |
| 121 | + SDL_SetAlpha( image, 0, SDL_ALPHA_OPAQUE ); |
| 122 | + |
| 123 | + /* Convert to RGBA 32bits*/ |
| 124 | + SDL_Surface *image_rgb_RGBA32b = SDL_CreateRGBSurface(SDL_SWSURFACE, image->w, image->h, 32, |
| 125 | + image->format->Rmask, image->format->Gmask, |
| 126 | + image->format->Bmask, image->format->Amask); |
| 127 | + SDL_BlitSurface(image, NULL, image_rgb_RGBA32b, NULL); |
107 | 128 | SDL_FreeSurface(image); |
108 | 129 |
|
109 | | - /* Resize img */ |
| 130 | + /* Resize image */ |
110 | 131 | int new_img_height = hw_surface->h - height_buttons - height_title; |
111 | 132 | int new_img_width = image->w *new_img_height / image->h; |
112 | | - SDL_Surface *image_rgb565_resized = SDL_CreateRGBSurface(SDL_SWSURFACE, new_img_width, new_img_height, 16, 0, 0, 0, 0); |
113 | | - flip_NNOptimized_AllowOutOfScreen(image_rgb565, image_rgb565_resized, image_rgb565_resized->w, image_rgb565_resized->h); |
114 | | - SDL_FreeSurface(image_rgb565); |
| 133 | + SDL_Surface *image_RGBA32b_resized = zoomSurface(image_rgb_RGBA32b, new_img_width, new_img_height); |
| 134 | + //SDL_SaveBMP(image_RGBA32b_resized,"./image_RGBA32b_resized.bmp"); |
| 135 | + SDL_FreeSurface(image_rgb_RGBA32b); |
115 | 136 |
|
116 | 137 | /* Blit image */ |
117 | | - SDL_Rect pos_img = {(hw_surface->w-image_rgb565_resized->w)/2, height_title, image_rgb565_resized->w, image_rgb565_resized->h}; |
118 | | - SDL_BlitSurface(image_rgb565_resized, NULL, hw_surface, &pos_img); |
119 | | - SDL_FreeSurface(image_rgb565_resized); |
| 138 | + SDL_Rect pos_img = {(hw_surface->w-image_RGBA32b_resized->w)/2, height_title, image_RGBA32b_resized->w, image_RGBA32b_resized->h}; |
| 139 | + SDL_BlitSurface(image_RGBA32b_resized, NULL, hw_surface, &pos_img); |
| 140 | + SDL_FreeSurface(image_RGBA32b_resized); |
120 | 141 |
|
121 | 142 | /// -------- Main loop --------- |
122 | 143 | while (!stop_menu_loop) |
|
0 commit comments