File: ui_imagelib.c

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (273 lines) | stat: -rw-r--r-- 7,216 bytes parent folder | download
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#ifndef NO_IMAGE

#include "../ui_imagelib.h"

#include <stdio.h> /* sprintf */
#include <math.h>  /* pow */
#include <strings.h> /* strcasecmp */

#include <pobl/bl_util.h> /* DIGIT_STR_LEN */
#include <pobl/bl_debug.h>
#include <pobl/bl_mem.h>
#include <pobl/bl_conf_io.h> /* bl_get_user_rc_path */

#include "beos.h"

#if 1
#define BUILTIN_SIXEL
#endif

/* --- static functions --- */

#define CARD_HEAD_SIZE 0
#include "../../common/c_sixel.c"
#include "../../common/c_regis.c"
#include "../../common/c_animgif.c"

static void value_table_refresh(u_char *value_table, /* 256 bytes */
                                ui_picture_modifier_t *mod) {
  int i, tmp;
  double real_gamma, real_brightness, real_contrast;

  real_gamma = (double)(mod->gamma) / 100;
  real_contrast = (double)(mod->contrast) / 100;
  real_brightness = (double)(mod->brightness) / 100;

  for (i = 0; i < 256; i++) {
    tmp = real_contrast * (255 * pow(((double)i + 0.5) / 255, real_gamma) - 128) +
          128 * real_brightness;
    if (tmp >= 255) {
      break;
    } else if (tmp < 0) {
      value_table[i] = 0;
    } else {
      value_table[i] = tmp;
    }
  }

  for (; i < 256; i++) {
    value_table[i] = 255;
  }
}

static void adjust_pixmap(u_char *image, u_int width, u_int height,
                          ui_picture_modifier_t *pic_mod) {
  u_char *value_table;
  u_int y;
  u_int x;
  u_char r, g, b, a;
  u_int32_t pixel;

  if (!ui_picture_modifier_is_normal(pic_mod) && (value_table = alloca(256))) {
    value_table_refresh(value_table, pic_mod);
  } else {
    return;
  }

  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      pixel = *(((u_int32_t *)image) + (y * width + x));

      a = (pixel >> 24) & 0xff;
      r = (pixel >> 16) & 0xff;
      g = (pixel >> 8) & 0xff;
      b = pixel & 0xff;

      r = (value_table[r] * (255 - pic_mod->alpha) + pic_mod->blend_red * pic_mod->alpha) / 255;
      g = (value_table[g] * (255 - pic_mod->alpha) + pic_mod->blend_green * pic_mod->alpha) / 255;
      b = (value_table[b] * (255 - pic_mod->alpha) + pic_mod->blend_blue * pic_mod->alpha) / 255;

      pixel = (a << 24) | (r << 16) | (g << 8) | b;
      *(((u_int32_t *)image) + (y * width + x)) = pixel;
    }
  }
}

static void adjust_pixmap2(u_char *image, u_int width, u_int height,
                           ui_picture_modifier_t *pic_mod) {
  u_char *value_table;
  u_int y;
  u_int x;
  u_char r, g, b;

  if (!ui_picture_modifier_is_normal(pic_mod) && (value_table = alloca(256))) {
    value_table_refresh(value_table, pic_mod);
  } else {
    return;
  }

  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++) {
      r = image[0];
      g = image[1];
      b = image[2];

      image[0] =
          (value_table[r] * (255 - pic_mod->alpha) + pic_mod->blend_red * pic_mod->alpha) / 255;
      image[1] =
          (value_table[g] * (255 - pic_mod->alpha) + pic_mod->blend_green * pic_mod->alpha) / 255;
      image[2] =
          (value_table[b] * (255 - pic_mod->alpha) + pic_mod->blend_blue * pic_mod->alpha) / 255;

      image += 4;
    }
  }
}

static int check_has_alpha(u_char *image, u_int width, u_int height) {
  u_int x;
  u_int y;
  u_char *p = image + 3;

  for (y = 0; y < height; y++) {
    for (x = 0; x < width; x++, p += 4) {
      if (*p != 0xff) {
        return 1;
      }
    }
  }

  return 0;
}

static int load_file(char *path, /* must be UTF-8 */
                     u_int *width, u_int *height, ui_picture_modifier_t *pic_mod,
                     Pixmap *pixmap, PixmapMask *mask, int *transparent) {
  char *suffix;
  u_char *image;

  suffix = path + strlen(path) - 4;
#ifdef BUILTIN_SIXEL
  if (strcasecmp(suffix, ".six") == 0 && *width == 0 && *height == 0 &&
      (image = load_sixel_from_file(path, width, height, transparent))) {
    adjust_pixmap(image, *width, *height, pic_mod);
    *pixmap = beos_create_image(image, (*width) * (*height) * 4, *width, *height);
  } else
#endif
  {
    if (strcmp(suffix, ".gif") == 0) {
      char *dir;

      if (!strstr(path, "mlterm/anim") && /* is animation gif */
          (dir = bl_get_user_rc_path("mlterm/"))) {
        split_animation_gif(path, dir, hash_path(path));
        free(dir);
      }
    } else if (strcasecmp(suffix, ".rgs") == 0) {
      convert_regis_to_bmp(path);
    }

    /* XXX pic_mod is ignored */
    if (!(*pixmap = beos_load_image(path, width, height))) {
      return 0;
    }

    if (transparent) {
      *transparent = 0;
    }

    if (!ui_picture_modifier_is_normal(pic_mod)) {
      Pixmap new_pixmap;
      u_char *image2;

      image = beos_get_bits(*pixmap);
      if ((image2 = malloc((*width) * (*height) * 4))) {
        image = memcpy(image2, image, (*width) * (*height) * 4);
        beos_destroy_image(*pixmap);
        adjust_pixmap2(image, *width, *height, pic_mod);
        new_pixmap = beos_create_image(image, (*width) * (*height) * 4, *width, *height);
        free(image);
        *pixmap = new_pixmap;
      }
    }
  }

  if (transparent && *transparent) {
  dummy_mask:
    if (mask) {
      /* dummy (If cur_pic->mask is non-zero, need_clear = 1 in draw_picture() in ui_draw_str.c) */
      *mask = 1;
    }
  } else {
    int x, y;
    u_int32_t *p = (u_int32_t*)image;

    for (y = 0; y < *height; y++) {
      for (x = 0; x < *width; x++) {
        if ((((*p) >> 24) & 0xff) <= 0x7f) { /* alpha */
          goto dummy_mask;
        }
        p ++;
      }
    }

    if (mask) {
      *mask = None;
    }
  }

  return 1;
}

/* --- global functions --- */

void ui_imagelib_display_opened(Display *display) {}

void ui_imagelib_display_closed(Display *display) {}

Pixmap ui_imagelib_load_file_for_background(ui_window_t *win, char *path,
                                            ui_picture_modifier_t *pic_mod) {
  Pixmap pixmap;
  u_int width = 0;
  u_int height = 0;

  if (!load_file(path, &width, &height, pic_mod, &pixmap, NULL, NULL)) {
    return None;
  }

  if (ACTUAL_WIDTH(win) != width || ACTUAL_HEIGHT(win) != height) {
    pixmap = beos_resize_image(pixmap, ACTUAL_WIDTH(win), ACTUAL_HEIGHT(win));
  }

  return pixmap;
}

int ui_imagelib_root_pixmap_available(Display *display) { return 0; }

Pixmap ui_imagelib_get_transparent_background(ui_window_t *win, ui_picture_modifier_t *pic_mod) {
  return None;
}

int ui_imagelib_load_file(ui_display_t *disp, char *path, int keep_aspect, u_int32_t **cardinal,
                          Pixmap *pixmap, PixmapMask *mask, u_int *width, u_int *height,
                          int *transparent) {
  u_int pix_width = 0;
  u_int pix_height = 0;

  if (cardinal) {
    return 0;
  }

  if (!load_file(path, &pix_width, &pix_height, NULL, pixmap, mask, transparent)) {
    return 0;
  }

  if ((*width > 0 && pix_width != *width) || (*height > 0 && pix_height != *height)) {
    *pixmap = beos_resize_image(*pixmap, *width, *height);
  } else {
    *width = pix_width;
    *height = pix_height;
  }

  return 1;
}

void ui_destroy_image(Display *display, Pixmap pixmap) {
  beos_destroy_image(pixmap);
}

void ui_destroy_mask(Display *display, PixmapMask mask /* can be NULL */) {}

#endif /* NO_IMAGE */