David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 7 | #include <errno.h> |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 8 | #include <fcntl.h> |
| 9 | #include <math.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <unistd.h> |
| 15 | |
| 16 | #include "image.h" |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 17 | #include "util.h" |
| 18 | |
| 19 | typedef union { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 20 | uint32_t* as_pixels; |
| 21 | png_byte* as_png_bytes; |
| 22 | char* address; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 23 | } layout_t; |
| 24 | |
| 25 | struct _image_t { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 26 | char* filename; |
| 27 | bool use_offset; |
| 28 | bool use_location; |
| 29 | int32_t offset_x; |
| 30 | int32_t offset_y; |
| 31 | uint32_t location_x; |
| 32 | uint32_t location_y; |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 33 | uint32_t scale; |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 34 | uint32_t duration; |
| 35 | layout_t layout; |
| 36 | png_uint_32 width; |
| 37 | png_uint_32 height; |
| 38 | png_uint_32 pitch; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | image_t* image_create() |
| 42 | { |
| 43 | image_t* image; |
| 44 | |
| 45 | image = (image_t*)calloc(1, sizeof(image_t)); |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 46 | image->scale = 1; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 47 | return image; |
| 48 | } |
| 49 | |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 50 | static void image_rgb(png_struct* png, png_row_info* row_info, png_byte* data) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 51 | { |
Stéphane Marchesin | ac14d29 | 2015-12-14 15:27:18 -0800 | [diff] [blame] | 52 | for (unsigned int i = 0; i < row_info->rowbytes; i+= 4) { |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 53 | uint8_t r, g, b, a; |
| 54 | uint32_t pixel; |
| 55 | |
| 56 | r = data[i + 0]; |
| 57 | g = data[i + 1]; |
| 58 | b = data[i + 2]; |
| 59 | a = data[i + 3]; |
| 60 | pixel = (a << 24) | (r << 16) | (g << 8) | b; |
| 61 | memcpy(data + i, &pixel, sizeof(pixel)); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int image_load_image_from_file(image_t* image) |
| 66 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 67 | FILE* fp; |
| 68 | png_struct* png; |
| 69 | png_info* info; |
| 70 | png_uint_32 width, height, pitch, row; |
| 71 | int bpp, color_type, interlace_mthd; |
| 72 | png_byte** rows; |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 73 | int ret = 0; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 74 | |
| 75 | if (image->layout.address != NULL) |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 76 | return EADDRINUSE; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 77 | |
| 78 | fp = fopen(image->filename, "rb"); |
| 79 | if (fp == NULL) |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 80 | return errno; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 81 | |
| 82 | png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| 83 | info = png_create_info_struct(png); |
| 84 | |
| 85 | if (info == NULL) |
| 86 | return 1; |
| 87 | |
| 88 | png_init_io(png, fp); |
| 89 | |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 90 | ret = setjmp(png_jmpbuf(png)); |
| 91 | if (ret != 0) |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 92 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 93 | |
| 94 | png_read_info(png, info); |
| 95 | png_get_IHDR(png, info, &width, &height, &bpp, &color_type, |
| 96 | &interlace_mthd, NULL, NULL); |
| 97 | |
| 98 | pitch = 4 * width; |
| 99 | |
| 100 | switch (color_type) |
| 101 | { |
| 102 | case PNG_COLOR_TYPE_PALETTE: |
| 103 | png_set_palette_to_rgb(png); |
| 104 | break; |
| 105 | |
| 106 | case PNG_COLOR_TYPE_GRAY: |
| 107 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 108 | png_set_gray_to_rgb(png); |
| 109 | } |
| 110 | |
| 111 | if (png_get_valid(png, info, PNG_INFO_tRNS)) |
| 112 | png_set_tRNS_to_alpha(png); |
| 113 | |
| 114 | switch (bpp) |
| 115 | { |
| 116 | default: |
| 117 | if (bpp < 8) |
| 118 | png_set_packing(png); |
| 119 | break; |
| 120 | case 16: |
| 121 | png_set_strip_16(png); |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | if (interlace_mthd != PNG_INTERLACE_NONE) |
| 126 | png_set_interlace_handling(png); |
| 127 | |
| 128 | png_set_filler(png, 0xff, PNG_FILLER_AFTER); |
| 129 | |
| 130 | png_set_read_user_transform_fn(png, image_rgb); |
| 131 | png_read_update_info(png, info); |
| 132 | |
| 133 | rows = malloc(height * sizeof(*rows)); |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 134 | if (!rows) { |
| 135 | ret = -ENOMEM; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 136 | goto fail; |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 137 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 138 | |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 139 | image->layout.address = malloc(height * pitch); |
| 140 | if (!image->layout.address) { |
| 141 | free(rows); |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 142 | ret = -ENOMEM; |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 143 | goto fail; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 146 | for (row = 0; row < height; row++) |
| 147 | rows[row] = &image->layout.as_png_bytes[row * pitch]; |
| 148 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 149 | png_read_image(png, rows); |
| 150 | free(rows); |
| 151 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 152 | image->width = width; |
| 153 | image->height = height; |
| 154 | image->pitch = pitch; |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 155 | png_read_end(png, info); |
Stéphane Marchesin | c236e0b | 2015-12-14 15:29:15 -0800 | [diff] [blame] | 156 | |
| 157 | fail: |
| 158 | png_destroy_read_struct(&png, &info, NULL); |
| 159 | fclose(fp); |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 160 | fp = NULL; |
| 161 | return ret; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 164 | int image_show(image_t* image, fb_t* fb) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 165 | { |
Stéphane Marchesin | 00ff187 | 2015-12-14 13:40:09 -0800 | [diff] [blame] | 166 | uint32_t* buffer; |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 167 | int32_t startx, starty; |
| 168 | uint32_t pitch4; |
| 169 | int32_t x, y, w, h; |
| 170 | int32_t ox = 0, oy = 0; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 171 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 172 | buffer = fb_lock(fb); |
Stéphane Marchesin | d47f874 | 2016-01-06 16:44:00 -0800 | [diff] [blame] | 173 | if (buffer == NULL) |
| 174 | return -1; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 175 | |
| 176 | if (image->use_offset && image->use_location) { |
| 177 | LOG(WARNING, "offset and location set, using location"); |
| 178 | image->use_offset = false; |
| 179 | } |
| 180 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 181 | w = (int32_t)(image->width * image->scale); |
| 182 | h = (int32_t)(image->height * image->scale); |
| 183 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 184 | if (image->use_location) { |
| 185 | startx = image->location_x; |
| 186 | starty = image->location_y; |
| 187 | } else { |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 188 | startx = (fb_getwidth(fb) - w)/2; |
| 189 | starty = (fb_getheight(fb) - h)/2; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if (image->use_offset) { |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 193 | startx += image->offset_x * (int32_t)image->scale; |
| 194 | starty += image->offset_y * (int32_t)image->scale; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 197 | pitch4 = fb_getpitch(fb) / 4; |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 198 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 199 | if (startx >= fb_getwidth(fb) || startx + w <= 0) |
| 200 | return 0; |
| 201 | |
| 202 | if (starty >= fb_getheight(fb) || starty + h <= 0) |
| 203 | return 0; |
| 204 | |
| 205 | if (startx < 0) { |
| 206 | ox = -startx; |
| 207 | w += startx; |
| 208 | startx = 0; |
| 209 | } |
| 210 | |
| 211 | if (startx + w > fb_getwidth(fb)) |
| 212 | w = fb_getwidth(fb) - startx; |
| 213 | |
| 214 | if (starty < 0) { |
| 215 | oy = -starty; |
| 216 | h += starty; |
| 217 | starty = 0; |
| 218 | } |
| 219 | |
| 220 | if (starty + h > fb_getheight(fb)) |
| 221 | h = fb_getheight(fb) - starty; |
| 222 | |
| 223 | for (y = 0; y < h; y++) { |
| 224 | uint32_t *o = buffer + (starty + y) * pitch4 + startx; |
| 225 | int32_t iy = (oy + y) / image->scale; |
| 226 | uint32_t *i = image->layout.as_pixels + iy * (image->pitch >> 2); |
| 227 | |
| 228 | for (x = 0; x < w; x++) { |
| 229 | int32_t ix = (ox + x) / image->scale; |
| 230 | o[x] = i[ix]; |
| 231 | } |
| 232 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 233 | |
Dominik Behr | 83010f8 | 2016-03-18 18:43:08 -0700 | [diff] [blame] | 234 | fb_unlock(fb); |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | void image_release(image_t* image) |
| 239 | { |
| 240 | if (image->layout.address != NULL) { |
| 241 | free(image->layout.address); |
| 242 | image->layout.address = NULL; |
| 243 | } |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void image_destroy(image_t* image) |
| 247 | { |
| 248 | image_release(image); |
| 249 | |
| 250 | if (image->filename != NULL) { |
| 251 | free(image->filename); |
| 252 | image->filename = NULL; |
| 253 | } |
| 254 | |
| 255 | free(image); |
| 256 | } |
| 257 | |
| 258 | void image_set_filename(image_t* image, char* filename) |
| 259 | { |
| 260 | if (image->filename != NULL) |
| 261 | free(image->filename); |
| 262 | |
| 263 | image->filename = strdup(filename); |
| 264 | } |
| 265 | |
Dominik Behr | 46755a4 | 2016-04-21 18:08:33 -0700 | [diff] [blame] | 266 | char* image_get_filename(image_t* image) |
| 267 | { |
| 268 | return image->filename; |
| 269 | } |
| 270 | |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 271 | void image_set_offset(image_t* image, int32_t offset_x, int32_t offset_y) |
| 272 | { |
| 273 | image->offset_x = offset_x; |
| 274 | image->offset_y = offset_y; |
| 275 | |
| 276 | image->use_offset = true; |
| 277 | } |
| 278 | |
| 279 | void image_set_location(image_t* image, |
Stéphane Marchesin | edece33 | 2015-12-14 15:10:58 -0800 | [diff] [blame] | 280 | uint32_t location_x, uint32_t location_y) |
David Sodman | 8ef2006 | 2015-01-06 09:23:40 -0800 | [diff] [blame] | 281 | { |
| 282 | image->location_x = location_x; |
| 283 | image->location_y = location_y; |
| 284 | |
| 285 | image->use_location = true; |
| 286 | } |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 287 | |
| 288 | void image_set_scale(image_t* image, uint32_t scale) |
| 289 | { |
| 290 | if (scale > MAX_SCALE_FACTOR) |
| 291 | scale = MAX_SCALE_FACTOR; |
| 292 | if (scale == 0) |
| 293 | image->scale = 1; |
| 294 | else |
| 295 | image->scale = scale; |
| 296 | } |
| 297 | |
Brian Norris | b86eb58 | 2017-12-04 12:31:06 -0800 | [diff] [blame] | 298 | int image_is_hires(fb_t* fb) |
| 299 | { |
| 300 | return fb_getwidth(fb) > HIRES_THRESHOLD_HR || |
| 301 | fb_getheight(fb) > HIRES_THRESHOLD_VR; |
| 302 | } |
| 303 | |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 304 | int32_t image_get_auto_scale(fb_t* fb) |
| 305 | { |
Brian Norris | b86eb58 | 2017-12-04 12:31:06 -0800 | [diff] [blame] | 306 | if (image_is_hires(fb)) |
Dominik Behr | 92d9e31 | 2016-05-04 20:10:48 -0700 | [diff] [blame] | 307 | return 2; |
| 308 | else |
| 309 | return 1; |
| 310 | } |