1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
* image.c - Functions to load & unpack PNGs and JPEGs
*
* TuxOnIce userui adaptations:
* Copyright (C) 2005 Bernard Blackham <bernard@blackham.com.au>
*
* Based on the original splashutils code:
* Copyright (C) 2004-2005, Michal Januszewski <spock@gentoo.org>
*
* This file is subject to the terms and conditions of the GNU General Public
* License v2. See the file COPYING in the main directory of this archive for
* more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <linux/fb.h>
#include "config.h"
#ifdef CONFIG_PNG
#ifdef TARGET_KERNEL
#include "libs/libpng-1.2.8/png.h"
#else
#include <png.h>
#endif
#endif
#ifdef TARGET_KERNEL
#include "libs/jpeg-6b/jpeglib.h"
#else
#include <jpeglib.h>
#endif
#include "../userui.h"
#include "splash.h"
struct fb_image verbose_img;
struct fb_image silent_img;
typedef struct {
u8 r, g, b;
} __attribute__ ((packed)) rgbcolor;
#if 0
#undef png_sig_cmp
static int (*png_sig_cmp) PNGARG((png_bytep sig, png_size_t start,
png_size_t num_to_check));
#endif
/* This function converts a truecolor image to whatever format the
* framebuffer uses */
void truecolor2fb (truecolor* data, u8* out, int len, int y, u8 alpha)
{
int i, add = 0;
rgbcolor* rgb = (rgbcolor*)data;
add ^= (0 ^ y) & 1 ? 1 : 3;
for (i = 0; i < len; i++) {
if (alpha) {
put_pixel(data->a, data->r, data->g, data->b, out, out, add);
data++;
} else {
put_pixel(255, rgb->r, rgb->g, rgb->b, out, out, add);
rgb++;
}
out += bytespp;
add ^= 3;
}
}
#ifdef CONFIG_PNG
#define PALETTE_COLORS 240
int load_png(char *filename, u8 **data, struct fb_cmap *cmap, unsigned int *width, unsigned int *height, u8 want_alpha)
{
png_structp png_ptr;
png_infop info_ptr, end_info;
png_bytep row_pointer;
png_colorp palette;
int rowbytes, num_palette;
int i, j, bytespp = (fb_var.bits_per_pixel + 7) >> 3;
u8 *buf = NULL;
u8 *t;
FILE *fp;
if (want_alpha)
bytespp = 4;
fp = fopen(filename,"r");
if (!fp)
{
printk("Could not open file %s.\n", filename);
return -1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
printk("Could not allocate PNG info struct. Out of memory?\n");
return -1;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info)
{
png_destroy_read_struct(&png_ptr, (png_infopp)&info_ptr, (png_infopp)NULL);
printk("Could not read file %s. Corrupt PNG header?\n", filename);
return -1;
}
if (setjmp(png_jmpbuf(png_ptr)))
goto failed;
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
if (cmap && png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_PALETTE) {
printk("Could not read file %s. Not a palette-based image.\n", filename);
goto failed;
}
if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY ||
png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
if (png_get_bit_depth(png_ptr, info_ptr) == 16)
png_set_strip_16(png_ptr);
if (!want_alpha && png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);
#ifndef TARGET_KERNEL
if (want_alpha && !(png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_ALPHA)) {
png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);
}
#endif
png_read_update_info(png_ptr, info_ptr);
if (!cmap && png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGB && png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_RGBA) {
printk("Could not read file %s. Not an RGB image.\n", filename);
goto failed;
}
if (cmap) {
png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
if (num_palette > cmap->len) {
printk("Could not read file %s. Too many colours in image (%d > %d).\n", filename, num_palette, cmap->len);
goto failed;
}
}
rowbytes = png_get_rowbytes(png_ptr, info_ptr);
if ((width && *width && png_get_image_width(png_ptr, info_ptr) != *width) || (height && *height && png_get_image_height(png_ptr, info_ptr) != *height)) {
printk("Image size mismatch: %s.\n", filename);
goto failed;
} else {
*width = png_get_image_width(png_ptr, info_ptr);
*height = png_get_image_height(png_ptr, info_ptr);
}
*data = malloc(fb_var.xres * fb_var.yres * bytespp);
if (!*data) {
printk("Failed to allocate memory for image: %s.\n", filename);
goto failed;
}
buf = malloc(rowbytes);
if (!buf) {
printk("Failed to allocate memory for image line buffer.\n");
free(*data);
goto failed;
}
for (i = 0; i < png_get_image_height(png_ptr, info_ptr); i++) {
if (cmap) {
row_pointer = *data + png_get_image_width(png_ptr, info_ptr) * i;
} else if (want_alpha) {
row_pointer = *data + png_get_image_width(png_ptr, info_ptr) * i * 4;
} else {
row_pointer = buf;
}
png_read_row(png_ptr, row_pointer, NULL);
if (cmap) {
int h = 256 - cmap->len;
t = *data + png_get_image_width(png_ptr, info_ptr) * i;
if (h) {
/* Move the colors up by 'h' offset. This is used because fbcon
* takes the first 16 colors. */
for (j = 0; j < rowbytes; j++) {
t[j] += h;
}
}
/* We only need to convert the image if we the alpha channel is not required */
} else if (!want_alpha) {
truecolor2fb((truecolor*)buf, *data + png_get_image_width(png_ptr, info_ptr) * bytespp * i, png_get_image_width(png_ptr, info_ptr), i, 0);
}
}
if (cmap) {
for (i = 0; i < cmap->len; i++) {
cmap->red[i] = palette[i].red * 257;
cmap->green[i] = palette[i].green * 257;
cmap->blue[i] = palette[i].blue * 257;
}
}
free(buf);
fclose(fp);
png_destroy_read_struct(&png_ptr, (png_infopp)&info_ptr, (png_infopp)&end_info);
return 0;
failed:
png_destroy_read_struct(&png_ptr, (png_infopp)&info_ptr, (png_infopp)&end_info);
return -1;
}
int is_png(char *filename)
{
unsigned char header[8];
FILE *fp = fopen(filename,"r");
if (!fp)
return -1;
fread(header, 1, 8, fp);
fclose(fp);
return !png_sig_cmp(header, 0, 8);
}
#endif /* PNG */
int load_jpeg(char *filename, u8 **data, __u32 *width, __u32 *height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE* injpeg;
u8 *buf = NULL;
int i, bytespp = (fb_var.bits_per_pixel+7) >> 3;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if ((injpeg = fopen(filename,"r")) == NULL) {
printk("Can't open file %s!\n", filename);
return -1;
}
jpeg_stdio_src(&cinfo, injpeg);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
if ((width && cinfo.output_width != *width) || (height && cinfo.output_height != *height)) {
printk("Image size mismatch: %s.\n", filename);
return -2;
} else {
*width = cinfo.output_width;
*height = cinfo.output_height;
}
buf = malloc(cinfo.output_width * cinfo.output_components * sizeof(char));
if (!buf) {
printk("Failed to allocate JPEG decompression buffer.\n");
return -1;
}
*data = malloc(cinfo.output_width * cinfo.output_height * bytespp);
if (!*data) {
printk("Failed to allocate memory for image: %s.\n", filename);
return -4;
}
for (i = 0; i < cinfo.output_height; i++) {
jpeg_read_scanlines(&cinfo, (JSAMPARRAY) &buf, 1);
truecolor2fb((truecolor*)buf, *data + cinfo.output_width * bytespp * i, cinfo.output_width, i, 0);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(injpeg);
free(buf);
return 0;
}
int load_bg_images(char mode)
{
struct fb_image *img = (mode == 'v') ? &verbose_img : &silent_img;
char *pic;
int i;
img->width = fb_var.xres;
img->height = fb_var.yres;
img->depth = fb_var.bits_per_pixel;
/* Deal with 8bpp modes. Only PNGs can be loaded, and pic256
* option has to be used to specify the filename of the image */
if (fb_var.bits_per_pixel == 8) {
pic = (mode == 'v') ? cf_pic256 : cf_silentpic256;
if (!pic) {
printk("No 8bpp %s picture specified in the theme config.\n", (mode == 'v') ? "verbose" : "silent" );
return -1;
}
#ifdef CONFIG_PNG
if (!is_png(pic)) {
printk("Unrecognized format of the verbose 8bpp background image.\n");
return -1;
}
/* We have a palette of 256 colors, but fbcon takes 16 of these for
* font colors in verbose mode, so we have 240 left for the picture */
if (mode != 's') {
i = PALETTE_COLORS;
img->cmap.start = 16;
} else {
i = 256;
img->cmap.start = 0;
}
img->cmap.transp = NULL;
img->cmap.red = malloc(i * 3 * 2);
if (!img->cmap.red) {
printk("Failed to allocate memory for the image palette.\n");
return -4;
}
img->cmap.green = img->cmap.red + i;
img->cmap.blue = img->cmap.green + i;
img->cmap.len = i;
if (load_png(pic, (u8**)&img->data, &img->cmap, &img->width, &img->height, 0)) {
printk("Failed to load PNG file %s.\n", pic);
return -1;
}
#else
printk("This version of splashutils has been compiled without support for 8bpp modes.\n");
return -1;
#endif
/* Deal with 15, 16, 24 and 32bpp modes */
} else {
pic = (mode == 'v') ? cf_pic : cf_silentpic;
if (!pic) {
if (mode == 's') /* only complain about the silent pic */
printk("No silent picture specified in the theme config.\n");
return -1;
}
#ifdef CONFIG_PNG
if (is_png(pic)) {
i = load_png(pic, (u8**)&img->data, NULL, &img->width, &img->height, 0);
} else
#endif
{
i = load_jpeg(pic, (u8**)&img->data, &img->width, &img->height);
}
if (i) {
printk("Failed to load image %s.\n", pic);
return -1;
}
}
return 0;
}
int load_images(char mode)
{
item *i;
if (!config_file) {
printk("No config file specified.\n");
return -1;
}
if (mode == 'v' || mode == 'a')
if (load_bg_images('v'))
return -2;
if (mode == 's' || mode == 'a') {
if (load_bg_images('s'))
return -2;
#ifdef CONFIG_PNG
for (i = icons.head; i != NULL; i = i->next) {
icon_img *ii = (icon_img*) i->p;
ii->w = ii->h = 0;
if (!is_png(ii->filename)) {
printk("Icon %s is not a PNG file.\n", ii->filename);
continue;
}
if (load_png(ii->filename, &ii->picbuf, NULL, &ii->w, &ii->h, 1)) {
printk("Failed to load icon %s.\n", ii->filename);
ii->picbuf = NULL;
ii->w = ii->h = 0;
continue;
}
}
#endif
}
return 0;
}
|