/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "ArPlaybackReader.hpp" #include "Base/BlockProfiler.hpp" #include "Base/Wrapper.hpp" #include "Image/YuvUtils.hpp" #if __has_include("turbojpeg.h") #include namespace OpenVulkano::AR::Playback { void ArPlaybackReader::ReadColorImage(ColorImg& img) { //BlockProfiler profiler("Load jpeg"); std::optional>> file = ReadColorImageRaw(); long unsigned int jpegSize = file->second.Size(); unsigned char* compressedImage = reinterpret_cast(file->second.Data()); int jpegSubsamp; tjhandle jpegDecompressor = tjInitDecompress(); tjDecompressHeader2(jpegDecompressor, compressedImage, jpegSize, &img.cols, &img.rows, &jpegSubsamp); if (img.UseRGB()) { img.channels = 4; if (!img.data) img.Allocate(); tjDecompress2(jpegDecompressor, compressedImage, jpegSize, img.data, img.cols, 0/*pitch*/, img.rows, TJPF_RGBA, TJFLAG_FASTDCT); } else if (img.channels == 1) { //TODO handle grayscale } else if (img.channels != 0) { if (!img.data) img.Allocate(); std::array planes; size_t chromaSize = img.cols * img.rows / 4; planes[0] = img.data; if (!img.dataUV) img.dataUV = img.data + (img.cols * img.rows); Unique chromaTmp; if (img.channels == ColorImg::NV12 && !img.dataPtr) { chromaTmp = std::make_unique(img.cols * img.rows / 2); planes[1] = chromaTmp.get(); } else { planes[1] = img.dataUV; if (img.channels == ColorImg::NV12) planes[1] += chromaSize; } planes[2] = planes[1] + chromaSize; std::array rowSize = { img.cols, img.cols / 2, img.cols / 2 }; tjDecompressToYUVPlanes(jpegDecompressor, compressedImage, jpegSize, planes.data(), img.cols, rowSize.data(), img.rows, TJFLAG_FASTDCT); if (img.channels == ColorImg::NV12) { YuvUtils::NV12FromChromaPlanes(planes[1], img.data + (img.cols * img.rows), chromaSize); } } tjDestroy(jpegDecompressor); m_imgReadSize += 1000 + file.value().first.size; } } #else #define STB_IMAGE_IMPLEMENTATION #include namespace OpenVulkano::AR::Playback { ColorImg ArPlaybackReader::ReadColorImage() { ColorImg img; auto file = m_archiveColor.GetNextFile(); img.dataPtr = std::shared_ptr( stbi_load_from_memory(reinterpret_cast(file->second.Data()), file->second.Size(), &img.cols, &img.rows, &img.channels, 3), &stbi_image_free); img.data = img.dataPtr.get(); m_imgReadSize += 1000 + file.value().first.size; return img; } } #endif