1
- /* File: loader_bpg .c
1
+ /* File: loader_heic .c
2
2
Time-stamp: <2012-12-09 21:19:30 gawen>
3
3
4
4
Copyright (c) 2011 David Hauweele <david@hauweele.net>
5
5
All rights reserved.
6
6
7
- Modified by Vitaly "_Vi" Shukela to use bpg instead of webp.
7
+ Modified by Vitaly "_Vi" Shukela to use heic instead of webp.
8
8
9
9
Redistribution and use in source and binary forms, with or without
10
10
modification, are permitted provided that the following conditions
46
46
#include "loader.h"
47
47
48
48
#include <inttypes.h>
49
- #include "libbpg .h"
49
+ #include "libheif/heif .h"
50
50
51
51
char load (ImlibImage * im , ImlibProgressFunction progress ,
52
52
char progress_granularity , char immediate_load )
53
53
{
54
54
55
55
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 ;
63
56
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 ;
64
63
65
64
if (im -> data )
66
65
return 0 ;
67
66
68
67
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 );
73
69
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 ;
81
72
}
82
73
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 ) {
85
79
goto EXIT ;
86
80
}
87
- buf_len = fread (buf , 1 , buf_len_max , f );
88
81
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 ;
90
87
88
+ if (!IMAGE_DIMENSIONS_OK (w , h ))
89
+ goto EXIT ;
91
90
92
- img = bpg_decoder_open ();
91
+ if (progress ) {
92
+ progress (im , 0 , 0 , 0 , w , h );
93
+ }
93
94
94
- if (bpg_decoder_decode (img , buf , buf_len ) < 0 ) {
95
- goto EXIT ;
95
+ if (!immediate_load ) {
96
+ retcode = 1 ;
97
+ goto EXIT ;
96
98
}
97
- free (buf ); buf = NULL ;
98
99
100
+ ret = heif_decode_image (imh , & img , heif_colorspace_RGB , heif_chroma_interleaved_RGBA , NULL );
101
+ if (!imh ) {
102
+ goto EXIT ;
103
+ }
99
104
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);*/
104
110
105
- bgra = malloc (4 * w * h );
106
- if (!bgra ) goto EXIT ;
107
111
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 );
119
114
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
+ }
126
118
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 ;
129
122
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
+ }
133
131
}
134
132
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 )
139
135
progress (im , 100 , 0 , 0 , w , h );
140
- return 1 ;
141
- }
142
136
143
-
137
+ SET_FLAGS (im -> flags , F_HAS_ALPHA );
138
+
139
+ im -> format = strdup ("heif" );
140
+ retcode = 1 ;
144
141
145
142
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 );
150
146
151
- return 0 ;
147
+ return retcode ;
152
148
}
153
149
154
150
char save (ImlibImage * im , ImlibProgressFunction progress ,
@@ -160,7 +156,7 @@ char save(ImlibImage *im, ImlibProgressFunction progress,
160
156
void formats (ImlibLoader * l )
161
157
{
162
158
int i ;
163
- char * list_formats [] = { "bpg " };
159
+ char * list_formats [] = { "heic" , "heif " };
164
160
165
161
l -> num_formats = (sizeof (list_formats ) / sizeof (char * ));
166
162
l -> formats = malloc (sizeof (char * ) * l -> num_formats );
0 commit comments