printf debug statements in ifdef

This commit is contained in:
Greg DiCristofaro 2022-03-02 09:21:53 -05:00
parent 1eaef023eb
commit 28076a26e6
9 changed files with 323 additions and 419 deletions

View File

@ -1,112 +0,0 @@
AUTOMAKE_OPTIONS = subdir-objects
examples = \
heif-convert \
heif-enc \
heif-info
examples_noinst = \
heif-test \
test-c-api
dist_man_MANS =
heif_convert_DEPENDENCIES = ../libheif/libheif.la
heif_convert_CXXFLAGS = -I$(top_srcdir) -I$(top_builddir)/.
heif_convert_LDFLAGS =
heif_convert_LDADD = ../libheif/libheif.la
heif_convert_SOURCES = encoder.cc encoder.h heif_convert.cc encoder_y4m.cc encoder_y4m.h
dist_man_MANS += heif-convert.1
if HAVE_LIBPNG
examples += heif-thumbnailer
heif_thumbnailer_DEPENDENCIES = ../libheif/libheif.la
heif_thumbnailer_CXXFLAGS = -I$(top_srcdir) -I$(top_builddir) $(libpng_CFLAGS)
heif_thumbnailer_LDFLAGS = $(libpng_LIBS)
heif_thumbnailer_LDADD = ../libheif/libheif.la
heif_thumbnailer_SOURCES = encoder.cc encoder.h heif_thumbnailer.cc encoder_png.cc encoder_png.h
dist_man_MANS += heif-thumbnailer.1
endif
if HAVE_LIBJPEG
heif_convert_CXXFLAGS += $(libjpeg_CFLAGS)
heif_convert_LDADD += $(libjpeg_LIBS)
heif_convert_SOURCES += encoder_jpeg.cc encoder_jpeg.h
endif
if HAVE_LIBPNG
heif_convert_CXXFLAGS += $(libpng_CFLAGS)
heif_convert_LDADD += $(libpng_LIBS)
heif_convert_SOURCES += encoder_png.cc encoder_png.h
endif
heif_info_DEPENDENCIES = ../libheif/libheif.la
heif_info_CXXFLAGS = -I$(top_srcdir) -I$(top_builddir)
heif_info_LDFLAGS =
heif_info_LDADD = ../libheif/libheif.la
heif_info_SOURCES = heif_info.cc
dist_man_MANS += heif-info.1
heif_enc_DEPENDENCIES = ../libheif/libheif.la
heif_enc_CXXFLAGS = -I$(top_srcdir) -I$(top_builddir)
heif_enc_LDFLAGS =
heif_enc_LDADD = ../libheif/libheif.la
heif_enc_SOURCES = heif_enc.cc
dist_man_MANS += heif-enc.1
if HAVE_LIBJPEG
heif_enc_CXXFLAGS += $(libjpeg_CFLAGS)
heif_enc_LDADD += $(libjpeg_LIBS)
endif
if HAVE_LIBPNG
heif_enc_CXXFLAGS += $(libpng_CFLAGS)
heif_enc_LDADD += $(libpng_LIBS)
endif
heif_test_DEPENDENCIES = ../libheif/libheif.la
heif_test_CXXFLAGS = -I$(top_srcdir) -I$(top_builddir)
heif_test_LDFLAGS =
heif_test_LDADD = ../libheif/libheif.la
heif_test_SOURCES = heif_test.cc
test_c_api_DEPENDENCIES = ../libheif/libheif.la
test_c_api_CFLAGS = -I$(top_srcdir) -I$(top_builddir)
test_c_api_LDFLAGS =
test_c_api_LDADD = ../libheif/libheif.la
test_c_api_SOURCES = test_c_api.c
EXTRA_DIST = \
CMakeLists.txt \
COPYING \
demo.html \
example.avif \
example.heic
if HAVE_GO
examples_noinst += \
heif-test-go
heif_test_go_SOURCES = heif-test.go
gopath:
mkdir -p ${CURDIR}/src/github.com/strukturag/libheif
ln -sf ${CURDIR}/../go ${CURDIR}/src/github.com/strukturag/libheif/
heif-test-go: gopath $(top_builddir)/libheif/libheif.la $(top_builddir)/libheif.pc heif-test.go
GOPATH=${CURDIR} PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:$(abs_top_builddir):$(abs_top_builddir)/libde265/dist/lib/pkgconfig/ CGO_CFLAGS="-I$(abs_top_builddir)" CGO_LDFLAGS="-L$(abs_top_builddir)/libheif/.libs" LD_LIBRARY_PATH=$(abs_top_builddir)/libheif/.libs $(GO) build -o heif-test-go ${heif_test_go_SOURCES}
format-go: ${heif_test_go_SOURCES}
$(GO) fmt ${heif_test_go_SOURCES}
else
format-go:
echo ""go" not present in "${PATH}", skipping formatting"
endif
if WITH_EXAMPLES
bin_PROGRAMS = $(examples)
noinst_PROGRAMS = $(examples_noinst)
endif
format-local: format-go

View File

@ -9,7 +9,7 @@ The output was:
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
Build started 3/1/2022 8:15:06 PM. Build started 3/2/2022 9:19:03 AM.
Project "C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\3.23.0-rc2\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets). Project "C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\3.23.0-rc2\CompilerIdC\CompilerIdC.vcxproj" on node 1 (default targets).
PrepareForBuild: PrepareForBuild:
Creating directory "Debug\". Creating directory "Debug\".
@ -35,7 +35,7 @@ Build succeeded.
0 Warning(s) 0 Warning(s)
0 Error(s) 0 Error(s)
Time Elapsed 00:00:00.67 Time Elapsed 00:00:00.64
Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CompilerIdC.exe" Compilation of the C compiler identification source "CMakeCCompilerId.c" produced "CompilerIdC.exe"
@ -54,7 +54,7 @@ The output was:
Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
Build started 3/1/2022 8:15:07 PM. Build started 3/2/2022 9:19:04 AM.
Project "C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\3.23.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets). Project "C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\3.23.0-rc2\CompilerIdCXX\CompilerIdCXX.vcxproj" on node 1 (default targets).
PrepareForBuild: PrepareForBuild:
Creating directory "Debug\". Creating directory "Debug\".
@ -80,7 +80,7 @@ Build succeeded.
0 Warning(s) 0 Warning(s)
0 Error(s) 0 Error(s)
Time Elapsed 00:00:00.61 Time Elapsed 00:00:00.48
Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CompilerIdCXX.exe" Compilation of the CXX compiler identification source "CMakeCXXCompilerId.cpp" produced "CompilerIdCXX.exe"
@ -92,42 +92,42 @@ The CXX compiler identification is MSVC, found in "C:/Users/gregd/Documents/Sour
Detecting C compiler ABI info compiled with the following output: Detecting C compiler ABI info compiled with the following output:
Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_d5263.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_4b51a.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64 Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
CMakeCCompilerABI.c CMakeCCompilerABI.c
cl /c /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_d5263.dir\Debug\\" /Fd"cmTC_d5263.dir\Debug\vc143.pdb" /external:W3 /Gd /TC /errorReport:queue "C:\Program Files\CMake\share\cmake-3.23\Modules\CMakeCCompilerABI.c" Copyright (C) Microsoft Corporation. All rights reserved.
cmTC_d5263.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_d5263.exe cl /c /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_4b51a.dir\Debug\\" /Fd"cmTC_4b51a.dir\Debug\vc143.pdb" /external:W3 /Gd /TC /errorReport:queue "C:\Program Files\CMake\share\cmake-3.23\Modules\CMakeCCompilerABI.c"
cmTC_4b51a.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_4b51a.exe
Detecting CXX compiler ABI info compiled with the following output: Detecting CXX compiler ABI info compiled with the following output:
Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_2a9c8.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_d7e81.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64 Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
CMakeCXXCompilerABI.cpp CMakeCXXCompilerABI.cpp
cl /c /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"cmTC_2a9c8.dir\Debug\\" /Fd"cmTC_2a9c8.dir\Debug\vc143.pdb" /external:W3 /Gd /TP /errorReport:queue "C:\Program Files\CMake\share\cmake-3.23\Modules\CMakeCXXCompilerABI.cpp" cl /c /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"cmTC_d7e81.dir\Debug\\" /Fd"cmTC_d7e81.dir\Debug\vc143.pdb" /external:W3 /Gd /TP /errorReport:queue "C:\Program Files\CMake\share\cmake-3.23\Modules\CMakeCXXCompilerABI.cpp"
cmTC_2a9c8.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_2a9c8.exe cmTC_d7e81.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_d7e81.exe
Performing C++ SOURCE FILE Test HAVE_JPEG_WRITE_ICC_PROFILE succeeded with the following output: Performing C++ SOURCE FILE Test HAVE_JPEG_WRITE_ICC_PROFILE succeeded with the following output:
Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp Change Dir: C:/Users/gregd/Documents/Source/autopsy/thirdparty/libheif/HeifConvertTestJNI/dist/CMakeFiles/CMakeTmp
Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_87163.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework Run Build Command(s):C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe cmTC_02584.vcxproj /p:Configuration=Debug /p:Platform=x64 /p:VisualStudioVersion=17.0 /v:m && Microsoft (R) Build Engine version 17.1.0+ae57d105c for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64 Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31104 for x64
src.cxx src.cxx
Copyright (C) Microsoft Corporation. All rights reserved. Copyright (C) Microsoft Corporation. All rights reserved.
cl /c /I"C:\Users\gregd\vcpkg\installed\x64-windows\include" /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D HAVE_JPEG_WRITE_ICC_PROFILE /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"cmTC_87163.dir\Debug\\" /Fd"cmTC_87163.dir\Debug\vc143.pdb" /external:W3 /Gd /TP /errorReport:queue C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\src.cxx cl /c /I"C:\Users\gregd\vcpkg\installed\x64-windows\include" /Zi /W3 /WX- /diagnostics:column /Od /Ob0 /D _MBCS /D WIN32 /D _WINDOWS /D HAVE_JPEG_WRITE_ICC_PROFILE /D "CMAKE_INTDIR=\"Debug\"" /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"cmTC_02584.dir\Debug\\" /Fd"cmTC_02584.dir\Debug\vc143.pdb" /external:W3 /Gd /TP /errorReport:queue C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\src.cxx
cmTC_87163.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_87163.exe cmTC_02584.vcxproj -> C:\Users\gregd\Documents\Source\autopsy\thirdparty\libheif\HeifConvertTestJNI\dist\CMakeFiles\CMakeTmp\Debug\cmTC_02584.exe
Source file was: Source file was:

View File

@ -27,18 +27,6 @@
#ifndef _Included_org_sleuthkit_autopsy_modules_pictureanalyzer_impls_HeifJNI #ifndef _Included_org_sleuthkit_autopsy_modules_pictureanalyzer_impls_HeifJNI
#define _Included_org_sleuthkit_autopsy_modules_pictureanalyzer_impls_HeifJNI #define _Included_org_sleuthkit_autopsy_modules_pictureanalyzer_impls_HeifJNI
//#if defined(HAVE_CONFIG_H)
//#include "config.h"
//#endif
//
//#include <cstring>
//
//#if defined(HAVE_UNISTD_H)
//
//#include <unistd.h>
//
//#endif
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
@ -91,26 +79,9 @@ jint throwIllegalState(JNIEnv* env, const char* message)
return throwException(env, className.c_str(), message); return throwException(env, className.c_str(), message);
} }
//void println(JNIEnv* env, std::string message)
//{
// // Get system class
// jclass syscls = env->FindClass("java/lang/System");
// // Lookup the "out" field
// jfieldID fid = env->GetStaticFieldID(syscls, "out", "Ljava/io/PrintStream;");
// jobject out = env->GetStaticObjectField(syscls, fid);
// // Get PrintStream class
// jclass pscls = env->FindClass("java/io/PrintStream");
// // Lookup printLn(String)
// jmethodID mid = env->GetMethodID(pscls, "println", "(Ljava/lang/String;)V");
// // Invoke the method
// jchar* cppstr = (jchar*)message.c_str(); // make an array of jchar (UTF-16 unsigned short encoding)
// jstring str = env->NewString(cppstr, message.length());
// env->CallVoidMethod(out, mid, str);
//}
int convertToDisk int convertToDisk
(JNIEnv* env, jclass cls, jbyteArray byteArr, jstring outputPath) { (JNIEnv* env, jclass cls, jbyteArray byteArr, jstring outputPath) {
size_t arrLen = env->GetArrayLength(byteArr); size_t arrLen = env->GetArrayLength(byteArr);
std::vector<jbyte> nativeByteArr(arrLen); std::vector<jbyte> nativeByteArr(arrLen);
@ -120,62 +91,73 @@ int convertToDisk
std::string output_filename = env->GetStringUTFChars(outputPath, 0); std::string output_filename = env->GetStringUTFChars(outputPath, 0);
const int quality = 100; const int quality = 100;
#ifdef _DEBUG
printf("Checking heif file type...\n"); printf("Checking heif file type...\n");
#endif
enum heif_filetype_result filetype_check = heif_check_filetype((const uint8_t*)&nativeByteArr[0], 12); enum heif_filetype_result filetype_check = heif_check_filetype((const uint8_t*)&nativeByteArr[0], 12);
if (filetype_check == heif_filetype_no) { if (filetype_check == heif_filetype_no) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalArgument(env, "Input file is not an HEIF/AVIF file"); throwIllegalArgument(env, "Input file is not an HEIF/AVIF file");
return 1; return 1;
} }
#ifdef _DEBUG
printf("Checking heif file type supported...\n"); printf("Checking heif file type supported...\n");
#endif
if (filetype_check == heif_filetype_yes_unsupported) { if (filetype_check == heif_filetype_yes_unsupported) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalArgument(env, "Input file is an unsupported HEIF/AVIF file type"); throwIllegalArgument(env, "Input file is an unsupported HEIF/AVIF file type");
return 1; return 1;
} }
#ifdef _DEBUG
printf("Creating heif context...\n"); printf("Creating heif context...\n");
#endif
struct heif_context* ctx = heif_context_alloc(); struct heif_context* ctx = heif_context_alloc();
if (!ctx) { if (!ctx) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, "Could not create context object"); throwIllegalState(env, "Could not create context object");
return 1; return 1;
} }
#ifdef _DEBUG
printf("Reading in heif bytes...\n"); printf("Reading in heif bytes...\n");
#endif
ContextReleaser cr(ctx); ContextReleaser cr(ctx);
struct heif_error err; struct heif_error err;
err = heif_context_read_from_memory_without_copy(ctx, (void*)&nativeByteArr[0], arrLen, nullptr); err = heif_context_read_from_memory_without_copy(ctx, (void*)&nativeByteArr[0], arrLen, nullptr);
if (err.code != 0) { if (err.code != 0) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
std::string err_message = "Could not read HEIF/AVIF file:"; std::string err_message = "Could not read HEIF/AVIF file:";
err_message += err.message; err_message += err.message;
throwIllegalState(env, err_message.c_str()); throwIllegalState(env, err_message.c_str());
return 1; return 1;
} }
#ifdef _DEBUG
printf("Checking heif file type...\n"); printf("Checking heif file type...\n");
#endif
int num_images = heif_context_get_number_of_top_level_images(ctx); int num_images = heif_context_get_number_of_top_level_images(ctx);
if (num_images == 0) { if (num_images == 0) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, "File doesn't contain any images"); throwIllegalState(env, "File doesn't contain any images");
return 1; return 1;
} }
#ifdef _DEBUG
printf("File contains %d images. Reading in image ids...\n", num_images); printf("File contains %d images. Reading in image ids...\n", num_images);
#endif
std::vector<heif_item_id> image_IDs(num_images); std::vector<heif_item_id> image_IDs(num_images);
num_images = heif_context_get_list_of_top_level_image_IDs(ctx, image_IDs.data(), num_images); num_images = heif_context_get_list_of_top_level_image_IDs(ctx, image_IDs.data(), num_images);
#ifdef _DEBUG
printf("Resetting encoder...\n"); printf("Resetting encoder...\n");
#endif
std::string filename; std::string filename;
std::unique_ptr<Encoder> encoder(new JpegEncoder(quality)); std::unique_ptr<Encoder> encoder(new JpegEncoder(quality));
size_t image_index = 1; // Image filenames are "1" based. size_t image_index = 1; // Image filenames are "1" based.
for (int idx = 0; idx < num_images; ++idx) { for (int idx = 0; idx < num_images; ++idx) {
#ifdef _DEBUG
printf("Looping through for image %d\n", idx); printf("Looping through for image %d\n", idx);
#endif
if (num_images > 1) { if (num_images > 1) {
std::ostringstream s; std::ostringstream s;
@ -183,18 +165,23 @@ int convertToDisk
s << "-" << image_index; s << "-" << image_index;
s << output_filename.substr(output_filename.find_last_of('.')); s << output_filename.substr(output_filename.find_last_of('.'));
filename.assign(s.str()); filename.assign(s.str());
#ifdef _DEBUG
printf("Assigning filename of %s\n", s.str().c_str()); printf("Assigning filename of %s\n", s.str().c_str());
#endif
} }
else { else {
filename.assign(output_filename); filename.assign(output_filename);
#ifdef _DEBUG
printf("Assigning filename of %s\n", output_filename.c_str()); printf("Assigning filename of %s\n", output_filename.c_str());
#endif
} }
#ifdef _DEBUG
printf("acquiring heif image handle...\n"); printf("acquiring heif image handle...\n");
#endif
struct heif_image_handle* handle; struct heif_image_handle* handle;
err = heif_context_get_image_handle(ctx, image_IDs[idx], &handle); err = heif_context_get_image_handle(ctx, image_IDs[idx], &handle);
if (err.code) { if (err.code) {
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
std::string err_message = "Could not read HEIF/AVIF image "; std::string err_message = "Could not read HEIF/AVIF image ";
err_message += idx; err_message += idx;
err_message += ": "; err_message += ": ";
@ -203,7 +190,9 @@ int convertToDisk
return 1; return 1;
} }
#ifdef _DEBUG
printf("handling alpha...\n"); printf("handling alpha...\n");
#endif
int has_alpha = heif_image_handle_has_alpha_channel(handle); int has_alpha = heif_image_handle_has_alpha_channel(handle);
struct heif_decoding_options* decode_options = heif_decoding_options_alloc(); struct heif_decoding_options* decode_options = heif_decoding_options_alloc();
encoder->UpdateDecodingOptions(handle, decode_options); encoder->UpdateDecodingOptions(handle, decode_options);
@ -212,12 +201,13 @@ int convertToDisk
if (bit_depth < 0) { if (bit_depth < 0) {
heif_decoding_options_free(decode_options); heif_decoding_options_free(decode_options);
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, "Input image has undefined bit-depth"); throwIllegalState(env, "Input image has undefined bit-depth");
return 1; return 1;
} }
#ifdef _DEBUG
printf("decoding heif image...\n"); printf("decoding heif image...\n");
#endif
struct heif_image* image; struct heif_image* image;
err = heif_decode_image(handle, err = heif_decode_image(handle,
&image, &image,
@ -231,27 +221,34 @@ int convertToDisk
err_message += idx; err_message += idx;
err_message += ": "; err_message += ": ";
err_message += err.message; err_message += err.message;
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, err_message.c_str()); throwIllegalState(env, err_message.c_str());
return 1; return 1;
} }
if (image) { if (image) {
#ifdef _DEBUG
printf("valid image found.\n"); printf("valid image found.\n");
#endif
bool written = encoder->Encode(handle, image, filename); bool written = encoder->Encode(handle, image, filename);
if (!written) { if (!written) {
#ifdef _DEBUG
printf("could not write image\n"); printf("could not write image\n");
#endif
} }
else { else {
#ifdef _DEBUG
printf("Written to %s\n", filename.c_str()); printf("Written to %s\n", filename.c_str());
#endif
} }
heif_image_release(image); heif_image_release(image);
int has_depth = heif_image_handle_has_depth_image(handle); int has_depth = heif_image_handle_has_depth_image(handle);
if (has_depth) { if (has_depth) {
#ifdef _DEBUG
printf("has depth...\n"); printf("has depth...\n");
#endif
heif_item_id depth_id; heif_item_id depth_id;
int nDepthImages = heif_image_handle_get_list_of_depth_image_IDs(handle, &depth_id, 1); int nDepthImages = heif_image_handle_get_list_of_depth_image_IDs(handle, &depth_id, 1);
assert(nDepthImages == 1); assert(nDepthImages == 1);
@ -261,14 +258,15 @@ int convertToDisk
err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle); err = heif_image_handle_get_depth_image_handle(handle, depth_id, &depth_handle);
if (err.code) { if (err.code) {
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, "Could not read depth channel"); throwIllegalState(env, "Could not read depth channel");
return 1; return 1;
} }
int depth_bit_depth = heif_image_handle_get_luma_bits_per_pixel(depth_handle); int depth_bit_depth = heif_image_handle_get_luma_bits_per_pixel(depth_handle);
#ifdef _DEBUG
printf("decoding depth image...\n"); printf("decoding depth image...\n");
#endif
struct heif_image* depth_image; struct heif_image* depth_image;
err = heif_decode_image(depth_handle, err = heif_decode_image(depth_handle,
&depth_image, &depth_image,
@ -278,7 +276,6 @@ int convertToDisk
if (err.code) { if (err.code) {
heif_image_handle_release(depth_handle); heif_image_handle_release(depth_handle);
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
std::string err_message = "Could not decode depth image: "; std::string err_message = "Could not decode depth image: ";
err_message += err.message; err_message += err.message;
throwIllegalState(env, err_message.c_str()); throwIllegalState(env, err_message.c_str());
@ -290,13 +287,19 @@ int convertToDisk
s << "-depth"; s << "-depth";
s << output_filename.substr(output_filename.find('.')); s << output_filename.substr(output_filename.find('.'));
#ifdef _DEBUG
printf("Encoding to %s.\n", s.str().c_str()); printf("Encoding to %s.\n", s.str().c_str());
#endif
written = encoder->Encode(depth_handle, depth_image, s.str()); written = encoder->Encode(depth_handle, depth_image, s.str());
if (!written) { if (!written) {
#ifdef _DEBUG
printf("could not write depth image\n"); printf("could not write depth image\n");
#endif
} }
else { else {
#ifdef _DEBUG
printf("Depth image written to %s\n", s.str().c_str()); printf("Depth image written to %s\n", s.str().c_str());
#endif
} }
heif_image_release(depth_image); heif_image_release(depth_image);
@ -304,14 +307,18 @@ int convertToDisk
} }
#ifdef _DEBUG
printf("checking for aux images...\n"); printf("checking for aux images...\n");
#endif
// --- aux images // --- aux images
int nAuxImages = heif_image_handle_get_number_of_auxiliary_images(handle, LIBHEIF_AUX_IMAGE_FILTER_OMIT_ALPHA | LIBHEIF_AUX_IMAGE_FILTER_OMIT_DEPTH); int nAuxImages = heif_image_handle_get_number_of_auxiliary_images(handle, LIBHEIF_AUX_IMAGE_FILTER_OMIT_ALPHA | LIBHEIF_AUX_IMAGE_FILTER_OMIT_DEPTH);
if (nAuxImages > 0) { if (nAuxImages > 0) {
#ifdef _DEBUG
printf("found %d aux images.\n", nAuxImages); printf("found %d aux images.\n", nAuxImages);
#endif
std::vector<heif_item_id> auxIDs(nAuxImages); std::vector<heif_item_id> auxIDs(nAuxImages);
heif_image_handle_get_list_of_auxiliary_image_IDs(handle, heif_image_handle_get_list_of_auxiliary_image_IDs(handle,
@ -319,18 +326,21 @@ int convertToDisk
auxIDs.data(), nAuxImages); auxIDs.data(), nAuxImages);
for (heif_item_id auxId : auxIDs) { for (heif_item_id auxId : auxIDs) {
#ifdef _DEBUG
printf("getting aux handle...\n"); printf("getting aux handle...\n");
#endif
struct heif_image_handle* aux_handle; struct heif_image_handle* aux_handle;
err = heif_image_handle_get_auxiliary_image_handle(handle, auxId, &aux_handle); err = heif_image_handle_get_auxiliary_image_handle(handle, auxId, &aux_handle);
if (err.code) { if (err.code) {
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
throwIllegalState(env, "Could not read auxiliary image"); throwIllegalState(env, "Could not read auxiliary image");
return 1; return 1;
} }
#ifdef _DEBUG
printf("decoding aux handle image...\n"); printf("decoding aux handle image...\n");
#endif
int aux_bit_depth = heif_image_handle_get_luma_bits_per_pixel(aux_handle); int aux_bit_depth = heif_image_handle_get_luma_bits_per_pixel(aux_handle);
struct heif_image* aux_image; struct heif_image* aux_image;
@ -342,20 +352,20 @@ int convertToDisk
if (err.code) { if (err.code) {
heif_image_handle_release(aux_handle); heif_image_handle_release(aux_handle);
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
std::string err_message = "Could not decode auxiliary image: "; std::string err_message = "Could not decode auxiliary image: ";
err_message += err.message; err_message += err.message;
throwIllegalState(env, err_message.c_str()); throwIllegalState(env, err_message.c_str());
return 1; return 1;
} }
#ifdef _DEBUG
printf("decoding aux image handle auxiliary type...\n"); printf("decoding aux image handle auxiliary type...\n");
#endif
const char* auxTypeC = nullptr; const char* auxTypeC = nullptr;
err = heif_image_handle_get_auxiliary_type(aux_handle, &auxTypeC); err = heif_image_handle_get_auxiliary_type(aux_handle, &auxTypeC);
if (err.code) { if (err.code) {
heif_image_handle_release(aux_handle); heif_image_handle_release(aux_handle);
heif_image_handle_release(handle); heif_image_handle_release(handle);
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
std::string err_message = "Could not get type of auxiliary image: "; std::string err_message = "Could not get type of auxiliary image: ";
err_message += err.message; err_message += err.message;
throwIllegalState(env, err_message.c_str()); throwIllegalState(env, err_message.c_str());
@ -364,7 +374,9 @@ int convertToDisk
std::string auxType = std::string(auxTypeC); std::string auxType = std::string(auxTypeC);
#ifdef _DEBUG
printf("freeing auxiliary type.\n"); printf("freeing auxiliary type.\n");
#endif
heif_image_handle_free_auxiliary_types(aux_handle, &auxTypeC); heif_image_handle_free_auxiliary_types(aux_handle, &auxTypeC);
std::ostringstream s; std::ostringstream s;
@ -373,14 +385,20 @@ int convertToDisk
s << output_filename.substr(output_filename.find('.')); s << output_filename.substr(output_filename.find('.'));
throwIllegalArgument(env, s.str().c_str()); throwIllegalArgument(env, s.str().c_str());
#ifdef _DEBUG
printf("Writing aux to output: %s\n", s.str().c_str()); printf("Writing aux to output: %s\n", s.str().c_str());
#endif
written = encoder->Encode(aux_handle, aux_image, s.str()); written = encoder->Encode(aux_handle, aux_image, s.str());
if (!written) { if (!written) {
#ifdef _DEBUG
printf("could not write auxiliary image\n"); printf("could not write auxiliary image\n");
#endif
} }
else { else {
#ifdef _DEBUG
printf("Auxiliary image written to %s\n", s.str().c_str()); printf("Auxiliary image written to %s\n", s.str().c_str());
#endif
} }
heif_image_release(aux_image); heif_image_release(aux_image);
@ -394,8 +412,6 @@ int convertToDisk
image_index++; image_index++;
} }
// env->ReleaseByteArrayElements(byteArr, nativeByteArr, 0);
return 0; return 0;
} }