-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstb.c
163 lines (142 loc) · 5.16 KB
/
stb.c
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
/*
* QEmacs graphics mode for image files
*
* Copyright (c) 2017-2020 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "qe.h"
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ASSERT(x)
#undef malloc
#undef realloc
#undef free
/* git commit d90462ef3124a9d019f7759edb2ba9256209f257 */
/* merged from 9d7a768 and b42009b */
#include "stb_image.h"
/*----------------------------------------------------------------*/
static ModeDef stb_mode;
typedef struct ImageState {
QEModeData base;
QEPicture pic;
void *stb_image;
int stb_x, stb_y, stb_channels;
} ImageState;
/*----------------*/
static inline ImageState *image_get_state(EditState *e, int status)
{
return qe_get_buffer_mode_data(e->b, &stb_mode, status ? e : NULL);
}
static void image_display(EditState *s) {
ImageState *ms = image_get_state(s, 0);
QEColor col = qe_styles[QE_STYLE_GUTTER].bg_color;
if (s->display_invalid) {
if (ms && ms->stb_image) {
#if 0
int x0, y0, w, h;
/* No scaling */
w = min(s->width, ms->pic.width);
h = min(s->height, ms->pic.height / s->screen->dpy.yfactor);
x0 = (s->width - w) / 2;
y0 = (s->height - h) / 2;
qe_draw_picture(s->screen, s->xleft + x0, s->ytop + y0, w, h,
&ms->pic, 0, 0, w, h * s->screen->dpy.yfactor,
0, QERGB(128, 128, 128));
fill_border(s, x0, y0, w, h, col);
put_status(s, "%dx%dx%d",
ms->pic.width, ms->pic.height, ms->stb_channels * 8);
#else
int x0 = 0;
int y0 = 0;
int w = ms->pic.width;
int h = (ms->pic.height + s->screen->dpy.yfactor - 1) / s->screen->dpy.yfactor;
int factor = 1024;
if (w > 0 && h > 0) {
/* compute scaling factor for scale up or down */
factor = min(4 * 1024, min(s->width * 1024 / w, s->height * 1024 / h));
/* Do not scale up on text display */
if (factor < 1024 || s->width != s->cols) {
w = ((w * factor) + 512) / 1024;
h = ((h * factor) + 512) / 1024;
}
x0 = (s->width - w) / 2;
y0 = (s->height - h) / 2;
qe_draw_picture(s->screen, s->xleft + x0, s->ytop + y0, w, h,
&ms->pic, 0, 0, ms->pic.width, ms->pic.height,
0, QERGB(128, 128, 128));
}
fill_border(s, x0, y0, w, h, col);
put_status(s, "%dx%dx%d",
ms->pic.width, ms->pic.height, ms->stb_channels * 8);
#endif
} else {
fill_rectangle(s->screen, s->xleft, s->ytop, s->width, s->height, col);
}
s->display_invalid = 0;
}
if (s->qe_state->active_window == s) {
/* Update cursor */
int xc = s->xleft;
int yc = s->ytop;
int w = s->char_width;
int h = s->line_height;
if (s->screen->dpy.dpy_cursor_at) {
/* hardware cursor */
s->screen->dpy.dpy_cursor_at(s->screen, xc, yc, w, h);
} else {
xor_rectangle(s->screen, xc, yc, w, h, QERGB(0xFF, 0xFF, 0xFF));
}
}
}
static void image_display_hook(EditState *s) {
ImageState *ms = image_get_state(s, 0);
if (ms && !ms->stb_image) {
ms->stb_image = stbi_load(s->b->filename,
&ms->stb_x, &ms->stb_y, &ms->stb_channels, 4);
if (ms->stb_image) {
ms->pic.width = ms->stb_x;
ms->pic.height = ms->stb_y;
ms->pic.format = QEBITMAP_FORMAT_BGRA32;
ms->pic.data[0] = ms->stb_image;
ms->pic.linesize[0] = ms->stb_x * 4;
} else {
put_status(s, "stbi_load error");
}
}
edit_invalidate(s, 1);
}
static void image_mode_free(EditBuffer *b, void *state) {
ImageState *ms = state;
if (ms->stb_image) {
stbi_image_free(ms->stb_image);
ms->stb_image = NULL;
ms->pic.data[0] = NULL;
}
}
static ModeDef stb_mode = {
.name = "Image file",
.alt_name = "stb",
.extensions = "bmp|ico|jpg|jpeg|png|tga|psd|gif|hdr|pic|pnm|ppm|pgm",
.buffer_instance_size = sizeof(ImageState),
.mode_free = image_mode_free,
.display_hook = image_display_hook,
.display = image_display,
};
static int stb_init(void)
{
qe_register_mode(&stb_mode, MODEF_VIEW);
return 0;
}
qe_module_init(stb_init);