1/**************************************************************************/
2/* image_loader_jpegd.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "image_loader_jpegd.h"
32
33#include "core/os/os.h"
34#include "core/string/print_string.h"
35
36#include <jpgd.h>
37#include <jpge.h>
38
39#include <string.h>
40
41Error jpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
42 jpgd::jpeg_decoder_mem_stream mem_stream(p_buffer, p_buffer_len);
43
44 jpgd::jpeg_decoder decoder(&mem_stream);
45
46 if (decoder.get_error_code() != jpgd::JPGD_SUCCESS) {
47 return ERR_CANT_OPEN;
48 }
49
50 const int image_width = decoder.get_width();
51 const int image_height = decoder.get_height();
52 const int comps = decoder.get_num_components();
53 if (comps != 1 && comps != 3) {
54 return ERR_FILE_CORRUPT;
55 }
56
57 if (decoder.begin_decoding() != jpgd::JPGD_SUCCESS) {
58 return ERR_FILE_CORRUPT;
59 }
60
61 const int dst_bpl = image_width * comps;
62
63 Vector<uint8_t> data;
64
65 data.resize(dst_bpl * image_height);
66
67 uint8_t *dw = data.ptrw();
68
69 jpgd::uint8 *pImage_data = (jpgd::uint8 *)dw;
70
71 for (int y = 0; y < image_height; y++) {
72 const jpgd::uint8 *pScan_line;
73 jpgd::uint scan_line_len;
74 if (decoder.decode((const void **)&pScan_line, &scan_line_len) != jpgd::JPGD_SUCCESS) {
75 return ERR_FILE_CORRUPT;
76 }
77
78 jpgd::uint8 *pDst = pImage_data + y * dst_bpl;
79
80 if (comps == 1) {
81 memcpy(pDst, pScan_line, dst_bpl);
82 } else {
83 // For images with more than 1 channel pScan_line will always point to a buffer
84 // containing 32-bit RGBA pixels. Alpha is always 255 and we ignore it.
85 for (int x = 0; x < image_width; x++) {
86 pDst[0] = pScan_line[x * 4 + 0];
87 pDst[1] = pScan_line[x * 4 + 1];
88 pDst[2] = pScan_line[x * 4 + 2];
89 pDst += 3;
90 }
91 }
92 }
93
94 //all good
95
96 Image::Format fmt;
97 if (comps == 1) {
98 fmt = Image::FORMAT_L8;
99 } else {
100 fmt = Image::FORMAT_RGB8;
101 }
102
103 p_image->set_data(image_width, image_height, false, fmt, data);
104
105 return OK;
106}
107
108Error ImageLoaderJPG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
109 Vector<uint8_t> src_image;
110 uint64_t src_image_len = f->get_length();
111 ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
112 src_image.resize(src_image_len);
113
114 uint8_t *w = src_image.ptrw();
115
116 f->get_buffer(&w[0], src_image_len);
117
118 Error err = jpeg_load_image_from_buffer(p_image.ptr(), w, src_image_len);
119
120 return err;
121}
122
123void ImageLoaderJPG::get_recognized_extensions(List<String> *p_extensions) const {
124 p_extensions->push_back("jpg");
125 p_extensions->push_back("jpeg");
126}
127
128static Ref<Image> _jpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
129 Ref<Image> img;
130 img.instantiate();
131 Error err = jpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
132 ERR_FAIL_COND_V(err, Ref<Image>());
133 return img;
134}
135
136class ImageLoaderJPGOSFile : public jpge::output_stream {
137public:
138 Ref<FileAccess> f;
139
140 virtual bool put_buf(const void *Pbuf, int len) {
141 f->store_buffer((const uint8_t *)Pbuf, len);
142 return true;
143 }
144};
145
146class ImageLoaderJPGOSBuffer : public jpge::output_stream {
147public:
148 Vector<uint8_t> *buffer = nullptr;
149 virtual bool put_buf(const void *Pbuf, int len) {
150 uint32_t base = buffer->size();
151 buffer->resize(base + len);
152 memcpy(buffer->ptrw() + base, Pbuf, len);
153 return true;
154 }
155};
156
157static Error _jpgd_save_to_output_stream(jpge::output_stream *p_output_stream, const Ref<Image> &p_img, float p_quality) {
158 ERR_FAIL_COND_V(p_img.is_null() || p_img->is_empty(), ERR_INVALID_PARAMETER);
159 Ref<Image> image = p_img;
160 if (image->get_format() != Image::FORMAT_RGB8) {
161 image->convert(Image::FORMAT_RGB8);
162 }
163
164 jpge::params p;
165 p.m_quality = CLAMP(p_quality * 100, 1, 100);
166
167 jpge::jpeg_encoder enc;
168 enc.init(p_output_stream, image->get_width(), image->get_height(), 3, p);
169
170 const uint8_t *src_data = image->get_data().ptr();
171 for (int i = 0; i < image->get_height(); i++) {
172 enc.process_scanline(&src_data[i * image->get_width() * 3]);
173 }
174
175 enc.process_scanline(nullptr);
176
177 return OK;
178}
179
180static Vector<uint8_t> _jpgd_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
181 Vector<uint8_t> output;
182 ImageLoaderJPGOSBuffer ob;
183 ob.buffer = &output;
184 if (_jpgd_save_to_output_stream(&ob, p_img, p_quality) != OK) {
185 return Vector<uint8_t>();
186 }
187 return output;
188}
189
190static Error _jpgd_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
191 Error err;
192 Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
193 ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
194 ImageLoaderJPGOSFile ob;
195 ob.f = file;
196 return _jpgd_save_to_output_stream(&ob, p_img, p_quality);
197}
198
199ImageLoaderJPG::ImageLoaderJPG() {
200 Image::_jpg_mem_loader_func = _jpegd_mem_loader_func;
201 Image::save_jpg_func = _jpgd_save_func;
202 Image::save_jpg_buffer_func = _jpgd_buffer_save_func;
203}
204