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