Skip to content

Commit d500fad

Browse files
committed
HEIC instead of BPG
1 parent 53789de commit d500fad

File tree

5 files changed

+86
-90
lines changed

5 files changed

+86
-90
lines changed

.gitignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
bpg.so
1+
heic.so
22
*.d
33
*.o
44
*.mkv
55
*.ffmpeg
66
*.png
77
*.ts
8-
*.bpg
8+
*.heic
99
*.ppm
10+
*.debug

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
imlib2-bpg
1+
imlib2-heic
22
---
33

4-
Loader for [BPG image format][1] for Imlib2 (i.e. for [Feh][2]).
4+
Loader for [HEIC][1] for Imlib2 (i.e. for [Feh][2]). Based on libheif.
55

66
Usage
77
---
88

99
1. Ensure Imlib2 development things (like `libimlib2-dev`) are installed;
10-
2. Build libbpg (for example, v0.9.3);
11-
3. Adjust BPGDIR in `makefile`;
12-
4. Build and install `bpg.so`.
10+
2. Ensure libheif is installed.
11+
3. Build and install `heic.so`.
1312

1413

15-
[1]:http://bellard.org/bpg/
14+
[1]:https://nokiatech.github.io/heif/technical.html
1615
[2]:http://feh.finalrewind.org/

commands.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RM=rm -f
44
INSTALL=install
55
INSTALL_PROGRAM=$(INSTALL) -s -m 555
66
INSTALL_DIR=$(INSTALL) -d
7-
INSTALL_LIB=$(INSTALL) -s -m 444
7+
INSTALL_LIB=$(INSTALL) -m 444
88
INSTALL_DATA=$(INSTALL) -m 444
99
INSTALL_SCRIPT=$(INSTALL_PROGRAM)
1010
XGETTEXT=xgettext

loader_bpg.c loader_heic.c

+66-70
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/* File: loader_bpg.c
1+
/* File: loader_heic.c
22
Time-stamp: <2012-12-09 21:19:30 gawen>
33
44
Copyright (c) 2011 David Hauweele <david@hauweele.net>
55
All rights reserved.
66
7-
Modified by Vitaly "_Vi" Shukela to use bpg instead of webp.
7+
Modified by Vitaly "_Vi" Shukela to use heic instead of webp.
88
99
Redistribution and use in source and binary forms, with or without
1010
modification, are permitted provided that the following conditions
@@ -46,109 +46,105 @@
4646
#include "loader.h"
4747

4848
#include <inttypes.h>
49-
#include "libbpg.h"
49+
#include "libheif/heif.h"
5050

5151
char load(ImlibImage * im, ImlibProgressFunction progress,
5252
char progress_granularity, char immediate_load)
5353
{
5454

5555
int w,h;
56-
uint8_t *buf = NULL;
57-
int buf_len, buf_len_max;
58-
FILE *f = NULL;
59-
BPGImageInfo img_info_s, *img_info = &img_info_s;
60-
BPGDecoderContext *img = NULL;
61-
uint8_t *bgra = NULL;
62-
int decoded_image_used = 0;
6356
int y=0;
57+
58+
char retcode = 0;
59+
struct heif_context *ctx = heif_context_alloc();
60+
struct heif_error ret;
61+
struct heif_image_handle *imh = NULL;
62+
struct heif_image *img = NULL;
6463

6564
if(im->data)
6665
return 0;
6766

6867

69-
f = fopen(im->real_file, "rb");
70-
if (!f) {
71-
goto EXIT;
72-
}
68+
ret = heif_context_read_from_file(ctx, im->real_file, NULL);
7369

74-
{
75-
fseek(f, 0, SEEK_END);
76-
buf_len_max = ftell(f);
77-
fseek(f, 0, SEEK_SET);
78-
}
79-
if (buf_len_max < 1) {
80-
buf_len_max = BPG_DECODER_INFO_BUF_SIZE;
70+
if (ret.code != heif_error_Ok) {
71+
goto EXIT;
8172
}
8273

83-
buf = malloc(buf_len_max);
84-
if (!buf) {
74+
ret = heif_context_get_primary_image_handle(ctx, &imh);
75+
if (ret.code != heif_error_Ok) {
76+
goto EXIT;
77+
}
78+
if (!imh) {
8579
goto EXIT;
8680
}
87-
buf_len = fread(buf, 1, buf_len_max, f);
8881

89-
fclose(f); f = NULL;
82+
w = heif_image_handle_get_width(imh);
83+
h = heif_image_handle_get_height(imh);
84+
85+
im->w = w;
86+
im->h = h;
9087

88+
if(!IMAGE_DIMENSIONS_OK(w, h))
89+
goto EXIT;
9190

92-
img = bpg_decoder_open();
91+
if(progress) {
92+
progress(im, 0, 0, 0, w, h);
93+
}
9394

94-
if (bpg_decoder_decode(img, buf, buf_len) < 0) {
95-
goto EXIT;
95+
if (!immediate_load) {
96+
retcode = 1;
97+
goto EXIT;
9698
}
97-
free(buf); buf = NULL;
9899

100+
ret = heif_decode_image(imh, &img, heif_colorspace_RGB, heif_chroma_interleaved_RGBA, NULL);
101+
if (!imh) {
102+
goto EXIT;
103+
}
99104

100-
bpg_decoder_get_info(img, img_info);
101-
102-
w = img_info->width;
103-
h = img_info->height;
105+
/*struct heif_error heif_decode_image(const struct heif_image_handle* in_handle,
106+
struct heif_image** out_img,
107+
enum heif_colorspace colorspace,
108+
enum heif_chroma chroma,
109+
const struct heif_decoding_options* options);*/
104110

105-
bgra = malloc(4 * w * h);
106-
if (!bgra) goto EXIT;
107111

108-
bpg_decoder_start(img, BPG_OUTPUT_FORMAT_RGBA32);
109-
for (y = 0; y < h; ++y) {
110-
bpg_decoder_get_line(img, bgra + 4*w*y);
111-
112-
int x;
113-
for (x=0; x < w; ++x) {
114-
#define SWAP(a,b) { unsigned char tmp; tmp=a; a=b; b=tmp; }
115-
SWAP(bgra[4*w*y + 4*x + 0], bgra[4*w*y + 4*x + 2]);
116-
#undef SWAP
117-
}
118-
}
112+
int stride = 0;
113+
uint8_t *plane = heif_image_get_plane(img, heif_channel_interleaved, &stride);
119114

120-
bpg_decoder_close(img); img = NULL;
121-
122-
123-
if(!im->loader && !im->data) {
124-
im->w = w;
125-
im->h = h;
115+
if (!plane) {
116+
goto EXIT;
117+
}
126118

127-
if(!IMAGE_DIMENSIONS_OK(w, h))
128-
goto EXIT;
119+
uint8_t *bgra;
120+
bgra = (uint8_t*)malloc(4 * w * h);
121+
if (!bgra) goto EXIT;
129122

130-
SET_FLAGS(im->flags, F_HAS_ALPHA);
131-
132-
im->format = strdup("bpg");
123+
for (y = 0; y < h; ++y) {
124+
int x;
125+
for (x=0; x < w; ++x) {
126+
bgra[4*(y*w + x) + 0] = plane[y*stride + 4*x + 2];
127+
bgra[4*(y*w + x) + 1] = plane[y*stride + 4*x + 1];
128+
bgra[4*(y*w + x) + 2] = plane[y*stride + 4*x + 0];
129+
bgra[4*(y*w + x) + 3] = plane[y*stride + 4*x + 3];
130+
}
133131
}
134132

135-
if((!im->data && im->loader) || immediate_load || progress) {
136-
im->data = (DATA32*)bgra;
137-
decoded_image_used = 1;
138-
if(progress)
133+
im->data = (DATA32*)bgra;
134+
if(progress)
139135
progress(im, 100, 0, 0, w, h);
140-
return 1;
141-
}
142136

143-
137+
SET_FLAGS(im->flags, F_HAS_ALPHA);
138+
139+
im->format = strdup("heif");
140+
retcode = 1;
144141

145142
EXIT:
146-
if (f) fclose(f);
147-
if (buf) free(buf);
148-
if (img) bpg_decoder_close(img);
149-
if ((!decoded_image_used) && bgra) free(bgra);
143+
if (ctx) heif_context_free(ctx);
144+
if (imh) heif_image_handle_release(imh);
145+
if (img) heif_image_release(img);
150146

151-
return 0;
147+
return retcode;
152148
}
153149

154150
char save(ImlibImage *im, ImlibProgressFunction progress,
@@ -160,7 +156,7 @@ char save(ImlibImage *im, ImlibProgressFunction progress,
160156
void formats(ImlibLoader *l)
161157
{
162158
int i;
163-
char *list_formats[] = { "bpg" };
159+
char *list_formats[] = { "heic", "heif" };
164160

165161
l->num_formats = (sizeof(list_formats) / sizeof(char *));
166162
l->formats = malloc(sizeof(char *) * l->num_formats);

makefile

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
include commands.mk
22

3-
BPGDIR=/mnt/src/p/libbpg-0.9.3
4-
5-
OPTS := -O2
6-
CFLAGS := -std=c99 $(OPTS) $(shell imlib2-config --cflags) -fPIC -Wall -I${BPGDIR}
7-
LDFLAGS := $(shell imlib2-config --libs) -L${BPGDIR} -lbpg
3+
OPTS := -O3
4+
CFLAGS := -std=c99 $(OPTS) $(shell pkg-config libheif --cflags) -fPIC -Wall
5+
LDFLAGS := $(shell imlib2-config --libs) $(shell pkg-config libheif --libs)
86

97

108
SRC = $(wildcard *.c)
@@ -20,25 +18,27 @@ endif
2018

2119
.PHONY: all clean
2220

23-
all: bpg.so
21+
all: heic.so
2422

25-
bpg.so: $(OBJ)
26-
$(CC) -shared -o $@ $^ $(LDFLAGS)
23+
heic.so: $(OBJ)
24+
$(CC) -shared -o $@ $^ $(LDFLAGS)
25+
cp $@ $@.debug
26+
strip $@
2727

2828
%.o: %.c
2929
$(CC) -Wp,-MMD,$*.d -c $(CFLAGS) -o $@ $<
3030

3131
clean:
3232
$(RM) $(DEP)
3333
$(RM) $(OBJ)
34-
$(RM) bpg.so
34+
$(RM) heic.so
3535

3636
install:
3737
$(INSTALL_DIR) $(DESTDIR)$(LOADERDIR)
38-
$(INSTALL_LIB) bpg.so $(DESTDIR)$(LOADERDIR)
38+
$(INSTALL_LIB) heic.so $(DESTDIR)$(LOADERDIR)
3939

4040
uninstall:
41-
$(RM) $(PLUGINDIR)/bpg.so
41+
$(RM) $(PLUGINDIR)/heic.so
4242

4343
-include $(DEP)
4444

0 commit comments

Comments
 (0)