Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions COPYRIGHT.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ Comment: libbacktrace
Copyright: 2012-2021, Free Software Foundation, Inc.
License: BSD-3-clause

Files: thirdparty/libjpeg/*
Comment: libjpeg
Copyright: 1991-2024, Thomas G. Lane, Guido Vollbeding.
License: IJG-License

Files: thirdparty/libktx/*
Comment: KTX
Copyright: 2013-2020, Mark Callow
Expand Down Expand Up @@ -1672,6 +1677,43 @@ License: HarfBuzz
ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

License: IJG

The authors make NO WARRANTY or representation, either express or implied,
with respect to this software, its quality, accuracy, merchantability, or
fitness for a particular purpose. This software is provided "AS IS", and you,
its user, assume the entire risk as to its quality and accuracy.

This software is copyright (C) 1991-2024, Thomas G. Lane, Guido Vollbeding.
All Rights Reserved except as specified below.

Permission is hereby granted to use, copy, modify, and distribute this
software (or portions thereof) for any purpose, without fee, subject to these
conditions:
(1) If any part of the source code for this software is distributed, then this
README file must be included, with this copyright and no-warranty notice
unaltered; and any additions, deletions, or changes to the original files
must be clearly indicated in accompanying documentation.
(2) If only executable code is distributed, then the accompanying
documentation must state that "this software is based in part on the work of
the Independent JPEG Group".
(3) Permission for use of this software is granted only if the user accepts
full responsibility for any undesirable consequences; the authors accept
NO LIABILITY for damages of any kind.

These conditions apply to any software derived from or based on the IJG code,
not just to the unmodified library. If you use our work, you ought to
acknowledge us.

Permission is NOT granted for the use of any IJG author's name or company name
in advertising or publicity relating to this software or products derived from
it. This software may be referred to only as "the Independent JPEG Group's
software".

We specifically permit and encourage the use of this software as the basis of
commercial products, provided that all warranty or liability claims are
assumed by the product vendor.

License: MPL-2.0
Mozilla Public License Version 2.0
==================================
Expand Down
5 changes: 5 additions & 0 deletions modules/jpg/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
/**************************************************************************/

#include "register_types.h"
#include "modules/modules_enabled.gen.h"

#include "image_loader_jpegd.h"

Expand All @@ -39,15 +40,19 @@ void initialize_jpg_module(ModuleInitializationLevel p_level) {
return;
}

#ifndef MODULE_LIBJPEG_ENABLED
image_loader_jpg.instantiate();
ImageLoader::add_image_format_loader(image_loader_jpg);
#endif
}

void uninitialize_jpg_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}

#ifndef MODULE_LIBJPEG_ENABLED
ImageLoader::remove_image_format_loader(image_loader_jpg);
image_loader_jpg.unref();
#endif
}
72 changes: 72 additions & 0 deletions modules/libjpeg/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")
Import("env_modules")

env_libjpeg = env_modules.Clone()

# Thirdparty source files

thirdparty_obj = []

# Not unbundled for now as they are not commonly available as shared library
thirdparty_dir = "#thirdparty/libjpeg/"
thirdparty_sources = [
"jaricom.c",
"jcapimin.c",
"jcapistd.c",
"jcarith.c",
"jccoefct.c",
"jccolor.c",
"jcdctmgr.c",
"jchuff.c",
"jcmainct.c",
"jcmarker.c",
"jcmaster.c",
"jcomapi.c",
"jcsample.c",
"jdapimin.c",
"jdapistd.c",
"jdarith.c",
"jdatadst.c",
"jdatasrc.c",
"jdcoefct.c",
"jdcolor.c",
"jddctmgr.c",
"jdhuff.c",
"jdinput.c",
"jdmainct.c",
"jdmarker.c",
"jdmaster.c",
"jdmerge.c",
"jdpostct.c",
"jdsample.c",
"jerror.c",
"jidctflt.c",
"jidctfst.c",
"jidctint.c",
"jmemansi.c",
"jmemmgr.c",
"jquant1.c",
"jquant2.c",
"jutils.c",
]
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]

env_libjpeg.Prepend(CPPPATH=[thirdparty_dir])

env_thirdparty = env_libjpeg.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.add_source_files(thirdparty_obj, thirdparty_sources)
env.modules_sources += thirdparty_obj

# Godot source files

module_obj = []

env_libjpeg.add_source_files(module_obj, "*.cpp")
env.modules_sources += module_obj

# Needed to force rebuilding the module files when the thirdparty library is updated.
env.Depends(module_obj, thirdparty_obj)
6 changes: 6 additions & 0 deletions modules/libjpeg/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def can_build(env, platform):
return True


def configure(env):
pass
140 changes: 140 additions & 0 deletions modules/libjpeg/image_loader_libjpeg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**************************************************************************/
/* image_loader_libjpeg.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "image_loader_libjpeg.h"

#include <jpeglib.h>

#include <string.h>

METHODDEF(noreturn_t)
error_dontexit(j_common_ptr cinfo) {
(*cinfo->err->output_message)(cinfo);
jpeg_destroy(cinfo);
}

Error libjpeg_load_image_from_buffer(Image *p_image, const uint8_t *p_buffer, int p_buffer_len) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = error_dontexit;
jpeg_create_decompress(&cinfo);
jpeg_mem_src(&cinfo, p_buffer, p_buffer_len);

if (jpeg_read_header(&cinfo, TRUE) != JPEG_HEADER_OK) {
return FAILED;
}

jpeg_start_decompress(&cinfo);

const unsigned int output_width = cinfo.output_width;
const unsigned int output_height = cinfo.output_height;
const unsigned int row_stride = output_width * cinfo.output_components;

Vector<uint8_t> data;
data.resize(row_stride * output_height);
uint8_t *dw = data.ptrw();

while (cinfo.output_scanline < output_height) {
unsigned char *buffer_array[1];
buffer_array[0] = dw + (cinfo.output_scanline) * row_stride;
jpeg_read_scanlines(&cinfo, buffer_array, 1);
}

jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

Image::Format fmt;
switch (cinfo.out_color_space) {
case JCS_GRAYSCALE:
fmt = Image::FORMAT_L8;
break;

case JCS_RGB:
fmt = Image::FORMAT_RGB8;
break;

default:
// TODO assert or warn?
fmt = Image::FORMAT_RGB8;
}

p_image->set_data(output_width, output_height, false, fmt, data);

return OK;
}

Error ImageLoaderLibJPEG::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
Vector<uint8_t> src_image;
uint64_t src_image_len = f->get_length();
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
src_image.resize(src_image_len);

uint8_t *w = src_image.ptrw();

f->get_buffer(&w[0], src_image_len);

Error err = libjpeg_load_image_from_buffer(p_image.ptr(), w, src_image_len);

return err;
}

void ImageLoaderLibJPEG::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("jpg");
p_extensions->push_back("jpeg");
}

static Ref<Image> _libjpegd_mem_loader_func(const uint8_t *p_png, int p_size) {
Ref<Image> img;
img.instantiate();
Error err = libjpeg_load_image_from_buffer(img.ptr(), p_png, p_size);
ERR_FAIL_COND_V(err, Ref<Image>());
return img;
}

static Vector<uint8_t> _libjpeg_buffer_save_func(const Ref<Image> &p_img, float p_quality) {
// TODO implementation
return Vector<uint8_t>();
}

static Error _libjpeg_save_func(const String &p_path, const Ref<Image> &p_img, float p_quality) {
Error err;
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
ERR_FAIL_COND_V_MSG(err, err, vformat("Can't save JPG at path: '%s'.", p_path));
// TODO implementation
return FAILED;
}

ImageLoaderLibJPEG::ImageLoaderLibJPEG() {
Image::_jpg_mem_loader_func = _libjpegd_mem_loader_func;
Image::save_jpg_func = _libjpeg_save_func;
Image::save_jpg_buffer_func = _libjpeg_buffer_save_func;
}
40 changes: 40 additions & 0 deletions modules/libjpeg/image_loader_libjpeg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**************************************************************************/
/* image_loader_libjpeg.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/io/image_loader.h"

class ImageLoaderLibJPEG : public ImageFormatLoader {
public:
virtual Error load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
ImageLoaderLibJPEG();
};
53 changes: 53 additions & 0 deletions modules/libjpeg/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**************************************************************************/
/* register_types.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "register_types.h"

#include "image_loader_libjpeg.h"

static Ref<ImageLoaderLibJPEG> image_loader_libjpeg;

void initialize_libjpeg_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}

image_loader_libjpeg.instantiate();
ImageLoader::add_image_format_loader(image_loader_libjpeg);
}

void uninitialize_libjpeg_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}

ImageLoader::remove_image_format_loader(image_loader_libjpeg);
image_loader_libjpeg.unref();
}
Loading