Adding upstream version 4.00~pre54+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / sys / vesa / background.c
blob935774619cb75e0f482b3e3b12f28c1dd27f4523
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2006-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
28 #include <stdio.h>
29 #include <png.h>
30 #include <tinyjpeg.h>
31 #include <com32.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <sys/stat.h>
35 #include <minmax.h>
36 #include <stdbool.h>
37 #include <ilog2.h>
38 #include <syslinux/loadfile.h>
39 #include "vesa.h"
40 #include "video.h"
42 /*** FIX: This really should be alpha-blended with color index 0 ***/
44 /* For best performance, "start" should be a multiple of 4, to assure
45 aligned dwords. */
46 static void draw_background_line(int line, int start, int npixels)
48 uint32_t *bgptr = &__vesacon_background[line*__vesa_info.mi.h_res+start];
49 unsigned int bytes_per_pixel = __vesacon_bytes_per_pixel;
50 size_t fbptr = line * __vesa_info.mi.logical_scan + start*bytes_per_pixel;
52 __vesacon_copy_to_screen(fbptr, bgptr, npixels);
55 /* This draws the border, then redraws the text area */
56 static void draw_background(void)
58 int i;
59 const int bottom_border = VIDEO_BORDER +
60 (TEXT_PIXEL_ROWS % __vesacon_font_height);
61 const int right_border = VIDEO_BORDER + (TEXT_PIXEL_COLS % FONT_WIDTH);
63 for (i = 0; i < VIDEO_BORDER; i++)
64 draw_background_line(i, 0, __vesa_info.mi.h_res);
66 for (i = VIDEO_BORDER; i < __vesa_info.mi.v_res - bottom_border; i++) {
67 draw_background_line(i, 0, VIDEO_BORDER);
68 draw_background_line(i, __vesa_info.mi.h_res - right_border,
69 right_border);
72 for (i = __vesa_info.mi.v_res - bottom_border;
73 i < __vesa_info.mi.v_res; i++)
74 draw_background_line(i, 0, __vesa_info.mi.h_res);
76 __vesacon_redraw_text();
80 * Tile an image in the UL corner across the entire screen
82 static void tile_image(int width, int height)
84 int xsize = __vesa_info.mi.h_res;
85 int ysize = __vesa_info.mi.v_res;
86 int x, y, yr;
87 int xl, yl;
88 uint32_t *sp, *dp, *drp, *dtp;
90 drp = __vesacon_background;
91 for (y = 0 ; y < ysize ; y += height) {
92 yl = min(height, ysize-y);
93 dtp = drp;
94 for (x = 0 ; x < xsize ; x += width) {
95 xl = min(width, xsize-x);
96 if (x || y) {
97 sp = __vesacon_background;
98 dp = dtp;
99 for (yr = 0 ; yr < yl ; yr++) {
100 memcpy(dp, sp, xl*sizeof(uint32_t));
101 dp += xsize;
102 sp += xsize;
105 dtp += xl;
107 drp += xsize*height;
111 static int read_png_file(FILE * fp)
113 png_structp png_ptr = NULL;
114 png_infop info_ptr = NULL;
115 #if 0
116 png_color_16p image_background;
117 static const png_color_16 my_background = { 0, 0, 0, 0, 0 };
118 #endif
119 png_bytep row_pointers[__vesa_info.mi.v_res], rp;
120 int i;
121 int rv = -1;
123 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
124 info_ptr = png_create_info_struct(png_ptr);
126 if (!png_ptr || !info_ptr || setjmp(png_jmpbuf(png_ptr)))
127 goto err;
129 png_init_io(png_ptr, fp);
130 png_set_sig_bytes(png_ptr, 8);
132 png_set_user_limits(png_ptr, __vesa_info.mi.h_res, __vesa_info.mi.v_res);
134 png_read_info(png_ptr, info_ptr);
136 /* Set the appropriate set of transformations. We need to end up
137 with 32-bit BGRA format, no more, no less. */
139 /* Expand to RGB first... */
140 if (info_ptr->color_type & PNG_COLOR_MASK_PALETTE)
141 png_set_palette_to_rgb(png_ptr);
142 else if (!(info_ptr->color_type & PNG_COLOR_MASK_COLOR))
143 png_set_gray_to_rgb(png_ptr);
145 /* Add alpha channel, if need be */
146 if (!(png_ptr->color_type & PNG_COLOR_MASK_ALPHA)) {
147 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
148 png_set_tRNS_to_alpha(png_ptr);
149 else
150 png_set_add_alpha(png_ptr, ~0, PNG_FILLER_AFTER);
153 /* Adjust the byte order, if necessary */
154 png_set_bgr(png_ptr);
156 /* Make sure we end up with 8-bit data */
157 if (info_ptr->bit_depth == 16)
158 png_set_strip_16(png_ptr);
159 else if (info_ptr->bit_depth < 8)
160 png_set_packing(png_ptr);
162 #if 0
163 if (png_get_bKGD(png_ptr, info_ptr, &image_background))
164 png_set_background(png_ptr, image_background,
165 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
166 else
167 png_set_background(png_ptr, &my_background,
168 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
169 #endif
171 /* Whew! Now we should get the stuff we want... */
172 rp = (png_bytep)__vesacon_background;
173 for (i = 0; i < (int)info_ptr->height; i++) {
174 row_pointers[i] = rp;
175 rp += __vesa_info.mi.h_res << 2;
178 png_read_image(png_ptr, row_pointers);
180 tile_image(info_ptr->width, info_ptr->height);
182 rv = 0;
184 err:
185 if (png_ptr)
186 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
187 return rv;
190 static int jpeg_sig_cmp(uint8_t * bytes, int len)
192 (void)len;
194 return (bytes[0] == 0xff && bytes[1] == 0xd8) ? 0 : -1;
197 static int read_jpeg_file(FILE * fp, uint8_t * header, int len)
199 struct jdec_private *jdec = NULL;
200 void *jpeg_file = NULL;
201 size_t length_of_file;
202 unsigned int width, height;
203 int rv = -1;
204 unsigned char *components[1];
205 unsigned int bytes_per_row[1];
207 rv = floadfile(fp, &jpeg_file, &length_of_file, header, len);
208 fclose(fp);
209 if (rv)
210 goto err;
212 jdec = tinyjpeg_init();
213 if (!jdec)
214 goto err;
216 if (tinyjpeg_parse_header(jdec, jpeg_file, length_of_file) < 0)
217 goto err;
219 tinyjpeg_get_size(jdec, &width, &height);
220 if (width > __vesa_info.mi.h_res || height > __vesa_info.mi.v_res)
221 goto err;
223 components[0] = (void *)__vesacon_background;
224 tinyjpeg_set_components(jdec, components, 1);
225 bytes_per_row[0] = __vesa_info.mi.h_res << 2;
226 tinyjpeg_set_bytes_per_row(jdec, bytes_per_row, 1);
228 tinyjpeg_decode(jdec, TINYJPEG_FMT_BGRA32);
229 tile_image(width, height);
231 rv = 0;
233 err:
234 /* Don't use tinyjpeg_free() here, since we didn't allow tinyjpeg
235 to allocate the frame buffer */
236 if (jdec)
237 free(jdec);
239 if (jpeg_file)
240 free(jpeg_file);
242 return rv;
245 /* Simple grey Gaussian hole, enough to look interesting */
246 int vesacon_default_background(void)
248 int x, y, dx, dy, dy2;
249 int z;
250 unsigned int shft;
251 uint8_t *bgptr = (uint8_t *)__vesacon_background;
252 uint8_t k;
254 if (__vesacon_pixel_format == PXF_NONE)
255 return 0; /* Not in graphics mode */
257 z = max(__vesa_info.mi.v_res, __vesa_info.mi.h_res) >> 1;
258 z = ((z*z) >> 11) - 1;
259 shft = ilog2(z) + 1;
261 for (y = 0, dy = -(__vesa_info.mi.v_res >> 1);
262 y < __vesa_info.mi.v_res; y++, dy++) {
263 dy2 = dy * dy;
264 for (x = 0, dx = -(__vesa_info.mi.h_res >> 1);
265 x < __vesa_info.mi.h_res; x++, dx++) {
266 k = __vesacon_linear_to_srgb[500 + ((dx*dx + dy2) >> shft)];
267 bgptr[0] = k; /* Blue */
268 bgptr[1] = k; /* Green */
269 bgptr[2] = k; /* Red */
270 bgptr += 4; /* Dummy alpha */
274 draw_background();
275 return 0;
278 /* Set the background to a single flat color */
279 int vesacon_set_background(unsigned int rgb)
281 void *bgptr = __vesacon_background;
282 unsigned int count = __vesa_info.mi.h_res * __vesa_info.mi.v_res;
284 if (__vesacon_pixel_format == PXF_NONE)
285 return 0; /* Not in graphics mode */
287 asm volatile ("rep; stosl":"+D" (bgptr), "+c"(count)
288 :"a"(rgb)
289 :"memory");
291 draw_background();
292 return 0;
295 struct lss16_header {
296 uint32_t magic;
297 uint16_t xsize;
298 uint16_t ysize;
301 #define LSS16_MAGIC 0x1413f33d
303 static inline int lss16_sig_cmp(const void *header, int len)
305 const struct lss16_header *h = header;
307 if (len != 8)
308 return 1;
310 return !(h->magic == LSS16_MAGIC &&
311 h->xsize <= __vesa_info.mi.h_res &&
312 h->ysize <= __vesa_info.mi.v_res);
315 static int read_lss16_file(FILE * fp, const void *header, int header_len)
317 const struct lss16_header *h = header;
318 uint32_t colors[16], color;
319 bool has_nybble;
320 uint8_t byte;
321 int count;
322 int nybble, prev;
323 enum state {
324 st_start,
325 st_c0,
326 st_c1,
327 st_c2,
328 } state;
329 int i, x, y;
330 uint32_t *bgptr = __vesacon_background;
332 /* Assume the header, 8 bytes, has already been loaded. */
333 if (header_len != 8)
334 return -1;
336 for (i = 0; i < 16; i++) {
337 uint8_t rgb[3];
338 if (fread(rgb, 1, 3, fp) != 3)
339 return -1;
341 colors[i] = (((rgb[0] & 63) * 255 / 63) << 16) +
342 (((rgb[1] & 63) * 255 / 63) << 8) +
343 ((rgb[2] & 63) * 255 / 63);
346 /* By spec, the state machine is per row */
347 for (y = 0; y < h->ysize; y++) {
348 state = st_start;
349 has_nybble = false;
350 color = colors[prev = 0]; /* By specification */
351 count = 0;
353 x = 0;
354 while (x < h->xsize) {
355 if (!has_nybble) {
356 if (fread(&byte, 1, 1, fp) != 1)
357 return -1;
358 nybble = byte & 0xf;
359 has_nybble = true;
360 } else {
361 nybble = byte >> 4;
362 has_nybble = false;
365 switch (state) {
366 case st_start:
367 if (nybble != prev) {
368 *bgptr++ = color = colors[prev = nybble];
369 x++;
370 } else {
371 state = st_c0;
373 break;
375 case st_c0:
376 if (nybble == 0) {
377 state = st_c1;
378 } else {
379 count = nybble;
380 goto do_run;
382 break;
384 case st_c1:
385 count = nybble + 16;
386 state = st_c2;
387 break;
389 case st_c2:
390 count += nybble << 4;
391 goto do_run;
393 do_run:
394 count = min(count, h->xsize - x);
395 x += count;
396 asm volatile ("rep; stosl":"+D" (bgptr),
397 "+c"(count):"a"(color));
398 state = st_start;
399 break;
403 /* Zero-fill rest of row */
404 i = __vesa_info.mi.h_res - x;
405 asm volatile ("rep; stosl":"+D" (bgptr), "+c"(i):"a"(0):"memory");
408 /* Zero-fill rest of screen */
409 i = (__vesa_info.mi.v_res - y) * __vesa_info.mi.h_res;
410 asm volatile ("rep; stosl":"+D" (bgptr), "+c"(i):"a"(0):"memory");
412 return 0;
415 int vesacon_load_background(const char *filename)
417 FILE *fp = NULL;
418 uint8_t header[8];
419 int rv = 1;
421 if (__vesacon_pixel_format == PXF_NONE)
422 return 0; /* Not in graphics mode */
424 fp = fopen(filename, "r");
426 if (!fp)
427 goto err;
429 if (fread(header, 1, 8, fp) != 8)
430 goto err;
432 if (!png_sig_cmp(header, 0, 8)) {
433 rv = read_png_file(fp);
434 } else if (!jpeg_sig_cmp(header, 8)) {
435 rv = read_jpeg_file(fp, header, 8);
436 } else if (!lss16_sig_cmp(header, 8)) {
437 rv = read_lss16_file(fp, header, 8);
440 /* This actually displays the stuff */
441 draw_background();
443 err:
444 if (fp)
445 fclose(fp);
447 return rv;
450 int __vesacon_init_background(void)
452 /* __vesacon_background was cleared by calloc() */
454 /* The VESA BIOS has already cleared the screen */
455 return 0;