Make requested changes

* Remove cast in scanline
* Improve Glib::ustring conversion
This commit is contained in:
xiota 2024-04-21 02:17:19 +00:00
parent 4d715cf281
commit 8c5fc60a7d
3 changed files with 43 additions and 16 deletions

View File

@ -25,17 +25,17 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#ifdef LIBJXL
#include "jxl/decode_cxx.h"
#include "jxl/resizable_parallel_runner_cxx.h"
#endif
#include <fcntl.h> #include <fcntl.h>
#include <glib/gstdio.h> #include <glib/gstdio.h>
#include <png.h> #include <png.h>
#include <tiff.h> #include <tiff.h>
#include <tiffio.h> #include <tiffio.h>
#ifdef LIBJXL
#include "jxl/decode_cxx.h"
#include "jxl/resizable_parallel_runner_cxx.h"
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <winsock2.h> #include <winsock2.h>
#else #else
@ -849,8 +849,12 @@ int ImageIO::loadJXL(const Glib::ustring &fname)
format.endianness = JXL_NATIVE_ENDIAN; format.endianness = JXL_NATIVE_ENDIAN;
format.align = 0; format.align = 0;
std::string const contents = Glib::file_get_contents(fname); std::vector<std::uint8_t> const compressed = getFileData(fname);
std::vector<std::uint8_t> const compressed(contents.begin(), contents.end());
if (compressed.empty()) {
g_printerr("Error: loadJXL failed to get data from file\n");
return IMIO_READERROR;
}
// multi-threaded parallel runner. // multi-threaded parallel runner.
auto runner = JxlResizableParallelRunnerMake(nullptr); auto runner = JxlResizableParallelRunnerMake(nullptr);
@ -927,9 +931,9 @@ int ImageIO::loadJXL(const Glib::ustring &fname)
g_printerr("Warning: Empty ICC data.\n"); g_printerr("Warning: Empty ICC data.\n");
} }
} else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) { } else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
// Note: If this assert is ever triggered, it should be changed // Note: If assert is triggered, change to assignment.
// to an assignment. We want the maximum bit depth the decoder // We want maximum bit depth from the decoder,
// can provide regardless of the original encoding intent. // regardless of the original encoding intent.
assert(format.data_type == JXL_TYPE_FLOAT); assert(format.data_type == JXL_TYPE_FLOAT);
if (JXL_DEC_SUCCESS != if (JXL_DEC_SUCCESS !=
@ -953,7 +957,7 @@ int ImageIO::loadJXL(const Glib::ustring &fname)
// Decoding complete. Decoder will be released automatically. // Decoding complete. Decoder will be released automatically.
break; break;
} else if (status == JXL_DEC_NEED_MORE_INPUT) { } else if (status == JXL_DEC_NEED_MORE_INPUT) {
g_printerr("Error: Already provided all input\n"); g_printerr("Error: Decoder needs more input data\n");
return IMIO_READERROR; return IMIO_READERROR;
} else if (status == JXL_DEC_ERROR) { } else if (status == JXL_DEC_ERROR) {
g_printerr("Error: Decoder error\n"); g_printerr("Error: Decoder error\n");
@ -972,10 +976,10 @@ int ImageIO::loadJXL(const Glib::ustring &fname)
std::size_t line_length = width * 3 * 4; std::size_t line_length = width * 3 * 4;
for (std::size_t row = 0; row < height; ++row) { for (std::size_t row = 0; row < height; ++row) {
setScanline(row, ((const unsigned char*)buffer.data()) + (row * line_length), 32); setScanline(row, buffer.data() + (row * line_length), 32);
if (pl && !(row % 100)) { if (pl && !(row % 100)) {
pl->setProgress((double)(row) / height); pl->setProgress((double)(row + 1) / height);
} }
} }

View File

@ -19,6 +19,8 @@
#include <cmath> #include <cmath>
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <fstream>
#include <glibmm/convert.h>
#include "rt_math.h" #include "rt_math.h"
#include "utils.h" #include "utils.h"
@ -222,6 +224,23 @@ void vflip(unsigned char* img, int w, int h)
} }
} }
std::vector<std::uint8_t> getFileData(const Glib::ustring &filename)
{
try {
const std::string fn = Glib::filename_from_utf8(filename);
std::ifstream instream(fn, std::ios::in | std::ios::binary);
std::vector<std::uint8_t> contents(
(std::istreambuf_iterator<char>(instream)),
std::istreambuf_iterator<char>());
instream.close();
return contents;
} catch (...) {
return {};
}
}
Glib::ustring getFileExtension(const Glib::ustring& filename) Glib::ustring getFileExtension(const Glib::ustring& filename)
{ {
const Glib::ustring::size_type lastdot_pos = filename.find_last_of('.'); const Glib::ustring::size_type lastdot_pos = filename.find_last_of('.');

View File

@ -18,6 +18,8 @@
*/ */
#pragma once #pragma once
#include <vector>
#include <type_traits> #include <type_traits>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
@ -43,6 +45,8 @@ constexpr typename std::underlying_type<ENUM>::type toUnderlying(ENUM value)
return static_cast<typename std::underlying_type<ENUM>::type>(value); return static_cast<typename std::underlying_type<ENUM>::type>(value);
} }
std::vector<std::uint8_t> getFileData(const Glib::ustring &filename);
// Return lower case extension without the "." or "" if the given name contains no "." // Return lower case extension without the "." or "" if the given name contains no "."
Glib::ustring getFileExtension(const Glib::ustring& filename); Glib::ustring getFileExtension(const Glib::ustring& filename);
// Return true if file has .jpeg or .jpg extension (ignoring case) // Return true if file has .jpeg or .jpg extension (ignoring case)