Skip to content

Commit 3c3fc40

Browse files
committed
show_image now working with QR codes that are 8 bits per pixels, all surfaces now 32b
1 parent db31a5c commit 3c3fc40

10 files changed

+79
-58
lines changed

funkey_prod_screens.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void init_libraries(){
6464

6565
/// Open HW screen and set video mode 240x240
6666
hw_surface = SDL_SetVideoMode(SCREEN_HORIZONTAL_SIZE, SCREEN_VERTICAL_SIZE,
67-
16, SDL_HWSURFACE | SDL_DOUBLEBUF);
67+
32, SDL_HWSURFACE | SDL_DOUBLEBUF);
6868
if (hw_surface == NULL)
6969
{
7070
fprintf(stderr, "ERROR SDL_SetVideoMode: %s\n", SDL_GetError());

prodScreen_buttonsTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int launch_prod_screen_buttons(int argc, char *argv[]){
5757
time_left--;
5858

5959
/* Fill screen white */
60-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
60+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
6161

6262
/* Background image */
6363
SDL_BlitSurface(img_console_layout, NULL, hw_surface, NULL);

prodScreen_displayTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int launch_prod_screen_display(int argc, char *argv[]){
2222
timeout--;
2323

2424
/* Fill screen white */
25-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
25+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
2626

2727
/* Write Title */
2828
text_surface = TTF_RenderText_Shaded(font_title, "FUNKEY PCBA TESTS", text_color, bg_color);

prodScreen_failScreen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int launch_prod_screen_fail(int argc, char *argv[]){
5353
SDL_Rect text_pos;
5454

5555
/* Fill screen white */
56-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
56+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
5757

5858
/* Write Title */
5959
SDL_Color red={255,0,0};

prodScreen_ledTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int launch_prod_screen_LED(int argc, char *argv[]){
5252
SDL_Rect text_pos;
5353

5454
/* Fill screen white */
55-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
55+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
5656

5757
/* Write Title */
5858
text_surface = TTF_RenderText_Shaded(font_title, "FunKey PCBA Tests", text_color, bg_color);

prodScreen_magnetTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int launch_prod_screen_magnet(int argc, char *argv[]){
6565
signal(SIGUSR1, handle_sigusr1);
6666

6767
/* Fill screen white */
68-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
68+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
6969

7070
/* Write Title */
7171
text_surface = TTF_RenderText_Shaded(font_title, "FunKey PCBA Tests", text_color, bg_color);

prodScreen_showImage.c

Lines changed: 70 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,78 @@
88

99
/// -------------- FUNCTIONS IMPLEMENTATION --------------
1010

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;
1917
int x2, y2;
2018
int i, j;
19+
int rat;
2120

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;
2625
}
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());
2939
}
30-
int x_padding_ratio = x_padding*w1/w2;
3140

32-
/* Copy pixels NN */
33-
for (i=0;i<h2;i++)
41+
/* Columns iterations */
42+
for (i = 0; i < dst_surface->h; i++)
3443
{
35-
if(i>=SCREEN_VERTICAL_SIZE){
36-
continue;
37-
}
3844

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 );
4047
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++)
4453
{
45-
if(j>=SCREEN_HORIZONTAL_SIZE){
46-
continue;
47-
}
54+
55+
/* Get current pixel in src surface */
4856
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 */
5063
rat += x_ratio;
51-
}
64+
}
5265
}
66+
67+
/* Return new zoomed surface */
68+
return dst_surface;
5369
}
5470

5571

72+
73+
5674
int launch_prod_screen_showImage(int argc, char *argv[]){
5775
SDL_Event event;
5876
SDL_Surface *text_surface = NULL;
5977
SDL_Rect text_pos;
6078
int res = EXIT_FAILURE;
6179
int stop_menu_loop = 0;
6280

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-
7281
/* 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) );
7483

7584
/* Write Title */
7685
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[]){
101110
SDL_BlitSurface(text_surface, NULL, hw_surface, &text_pos);
102111
SDL_FreeSurface(text_surface);
103112

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);
107128
SDL_FreeSurface(image);
108129

109-
/* Resize img */
130+
/* Resize image */
110131
int new_img_height = hw_surface->h - height_buttons - height_title;
111132
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);
115136

116137
/* 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);
120141

121142
/// -------- Main loop ---------
122143
while (!stop_menu_loop)

prodScreen_speakerTest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int launch_prod_screen_speaker(int argc, char *argv[]){
5353
SDL_Rect text_pos;
5454

5555
/* Fill screen white */
56-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
56+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
5757

5858
/* Write Title */
5959
text_surface = TTF_RenderText_Shaded(font_title, "FunKey PCBA Tests", text_color, bg_color);

prodScreen_validation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int launch_prod_screen_validation(int argc, char *argv[]){
5353
SDL_Rect text_pos;
5454

5555
/* Fill screen white */
56-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
56+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
5757

5858
/* Write Title */
5959
text_surface = TTF_RenderText_Shaded(font_title, "FUNKEY PCBA TESTS", text_color, bg_color);

prodScreen_waitBattery.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int launch_prod_screen_waitbattery(int argc, char *argv[]){
9393
SDL_Rect text_pos;
9494

9595
/* Fill screen white */
96-
SDL_FillRect(hw_surface, NULL, SDL_MapRGB(hw_surface->format, bg_color.r, bg_color.g, bg_color.b));
96+
SDL_FillRect(hw_surface, NULL, SDL_MapRGBA(hw_surface->format, bg_color.r, bg_color.g, bg_color.b, 0) );
9797

9898
/* Write Title */
9999
text_surface = TTF_RenderText_Shaded(font_title, "FUNKEY PCBA TESTS", text_color, bg_color);

0 commit comments

Comments
 (0)