// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
// This example encodes a file containing a floating point image to another // file containing JPEG XL image with a single frame.
/** * Reads from .pfm file (Portable FloatMap) * * @param filename name of the file to read * @param pixels vector to fill with loaded pixels as 32-bit floating point with * 3-channel RGB * @param xsize set to width of loaded image * @param ysize set to height of loaded image
*/ bool ReadPFM(constchar* filename, std::vector<float>* pixels, uint32_t* xsize,
uint32_t* ysize) {
FILE* file = fopen(filename, "rb"); if (!file) {
fprintf(stderr, "Could not open %s for reading.\n", filename); returnfalse;
}
uint32_t endian_test = 1;
uint8_t little_endian_check[4];
memcpy(little_endian_check, &endian_test, 4); bool little_endian = (little_endian_check[0] == 1);
if (fseek(file, 0, SEEK_END) != 0) {
fclose(file); returnfalse;
}
long size = ftell(file); // NOLINT // Avoid invalid file or directory. if (size >= LONG_MAX || size < 0) {
fclose(file); returnfalse;
}
if (fseek(file, 0, SEEK_SET) != 0) {
fclose(file); returnfalse;
}
if (data.size() != *ysize * *xsize * 3 * 4 + offset) {
fprintf(stderr, "%s doesn't seem to be a Portable FloatMap file (pixel data bytes " "are %d, but expected %d * %d * 3 * 4 + %d (%d).\n",
filename, static_cast<int>(data.size()), static_cast<int>(*ysize), static_cast<int>(*xsize), static_cast<int>(offset), static_cast<int>(*ysize * *xsize * 3 * 4 + offset)); returnfalse;
}
if (little_endian != input_little_endian) {
fprintf(stderr, "%s has a different endianness than we do, conversion is not " "supported.\n",
filename); returnfalse;
}
pixels->resize(*ysize * *xsize * 3);
for (int y = *ysize - 1; y >= 0; y--) { for (int x = 0; x < static_cast<int>(*xsize); x++) { for (int c = 0; c < 3; c++) {
memcpy(pixels->data() + (y * *xsize + x) * 3 + c, data.data() + offset, sizeof(float));
offset += sizeof(float);
}
}
}
returntrue;
}
/** * Compresses the provided pixels. * * @param pixels input pixels * @param xsize width of the input image * @param ysize height of the input image * @param compressed will be populated with the compressed bytes
*/ bool EncodeJxlOneshot(const std::vector<float>& pixels, const uint32_t xsize, const uint32_t ysize, std::vector<uint8_t>* compressed) { auto enc = JxlEncoderMake(/*memory_manager=*/nullptr); auto runner = JxlThreadParallelRunnerMake( /*memory_manager=*/nullptr,
JxlThreadParallelRunnerDefaultNumWorkerThreads()); if (JXL_ENC_SUCCESS != JxlEncoderSetParallelRunner(enc.get(),
JxlThreadParallelRunner,
runner.get())) {
fprintf(stderr, "JxlEncoderSetParallelRunner failed\n"); returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.