13
13
// limitations under the License.
14
14
#include <stddef.h>
15
15
#include <string.h>
16
+ #include <stdlib.h>
16
17
#include "img_converters.h"
17
18
#include "soc/efuse_reg.h"
18
19
#include "esp_heap_caps.h"
@@ -114,28 +115,31 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
114
115
.advanced .working_buffer = work ,
115
116
.advanced .working_buffer_size = sizeof (work ),
116
117
};
117
-
118
+
119
+ bool ret = false;
120
+ uint8_t * output = NULL ;
118
121
esp_jpeg_image_output_t output_img = {};
119
122
if (esp_jpeg_get_image_info (& jpeg_cfg , & output_img ) != ESP_OK ) {
120
123
ESP_LOGE (TAG , "Failed to get image info" );
121
- return false ;
124
+ goto fail ;
122
125
}
123
-
126
+
124
127
// @todo here we allocate memory and we assume that the user will free it
125
128
// this is not the best way to do it, but we need to keep the API
126
129
// compatible with the previous version
127
130
const size_t output_size = output_img .output_len + BMP_HEADER_LEN ;
128
- uint8_t * output = _malloc (output_size );
131
+ output = _malloc (output_size );
129
132
if (!output ) {
130
133
ESP_LOGE (TAG , "Failed to allocate output buffer" );
131
- return false ;
134
+ goto fail ;
132
135
}
133
136
134
137
// Start writing decoded data after the BMP header
135
138
jpeg_cfg .outbuf = output + BMP_HEADER_LEN ;
136
139
jpeg_cfg .outbuf_size = output_img .output_len ;
137
140
if (esp_jpeg_decode (& jpeg_cfg , & output_img ) != ESP_OK ){
138
- return false;
141
+ ESP_LOGE (TAG , "JPEG decode failed" );
142
+ goto fail ;
139
143
}
140
144
141
145
output [0 ] = 'B' ;
@@ -158,8 +162,13 @@ bool jpg2bmp(const uint8_t *src, size_t src_len, uint8_t ** out, size_t * out_le
158
162
159
163
* out = output ;
160
164
* out_len = output_size ;
165
+ ret = true;
161
166
162
- return true;
167
+ fail :
168
+ if (!ret && output ) {
169
+ free (output );
170
+ }
171
+ return ret ;
163
172
}
164
173
165
174
bool fmt2rgb888 (const uint8_t * src_buf , size_t src_len , pixformat_t format , uint8_t * rgb_buf )
0 commit comments