Skip to content

Commit

Permalink
Wip/gray img bug (#215)
Browse files Browse the repository at this point in the history
* Added test images....

* Improvements for Gray value images

* Support for PALETTE TIFF’s

* kakadu newest version

* Update to newest Version of libcurl

Necessary for compilation on OS X high sierra

* Test-script for conversion of tifs ro jp2s

* build (travis): add new version of kakadu library

* build (travis): encrypt kakadu with new secret
  • Loading branch information
lrosenth authored and subotic committed Oct 31, 2017
1 parent abd8cd5 commit 297d032
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -20,8 +20,8 @@ env:
- REPO=dhlabbasel/sipi

script:
# decrypt the kakadu library
- openssl aes-256-cbc -K $encrypted_53935862068b_key -iv $encrypted_53935862068b_iv -in vendor/v7_9-01727L.zip.enc -out vendor/v7_9-01727L.zip -d
# decrypt archive containing the kakadu library
- openssl aes-256-cbc -K $encrypted_53935862068b_key -iv $encrypted_53935862068b_iv -in vendor/v7_A_2-01382N.zip.enc -out vendor/v7_A_2-01382N.zip -d
# run compile and tests inside docker
- docker run -v $PWD:/sipi dhlabbasel/sipi-base /bin/sh -c "cd /sipi/build; cmake ..; make; make test; make check"

Expand Down
Binary file added BE_Grossaffoltern_refK_Madonna_I.2a_S.tif
Binary file not shown.
6 changes: 3 additions & 3 deletions ext/curl/CMakeLists.txt
Expand Up @@ -7,9 +7,9 @@ include(ExternalProject)
#
ExternalProject_Add(project_curl
INSTALL_DIR ${COMMON_LOCAL}
URL https://curl.haxx.se/download/curl-7.51.0.tar.gz
SOURCE_DIR ${COMMON_SRCS}/curl-7.51.0
CONFIGURE_COMMAND ${COMMON_SRCS}/curl-7.51.0/configure --prefix=${COMMON_LOCAL} --enable-shared --enable-static --libdir=${CONFIGURE_LIBDIR} --with-ssl=${OPENSSL_ROOT_DIR} --disable-ldap
URL https://curl.haxx.se/download/curl-7.56.0.tar.gz
SOURCE_DIR ${COMMON_SRCS}/curl-7.56.0
CONFIGURE_COMMAND ${COMMON_SRCS}/curl-7.56.0/configure --prefix=${COMMON_LOCAL} --enable-shared --enable-static --libdir=${CONFIGURE_LIBDIR} --with-ssl=${OPENSSL_ROOT_DIR} --disable-ldap
BUILD_COMMAND make
BUILD_IN_SOURCE 1
)
Expand Down
16 changes: 8 additions & 8 deletions ext/kakadu/CMakeLists.txt
Expand Up @@ -10,27 +10,27 @@ message(STATUS ${CONFIGURE_LIBDIR})
ExternalProject_Add(
project_kakadu
INSTALL_DIR ${COMMON_LOCAL}
DOWNLOAD_COMMAND unzip -o -d ${COMMON_SRCS} ${COMMON_VENDOR}/v7_9-01727L.zip
PATCH_COMMAND patch -d ${COMMON_SRCS}/v7_9-01727L -p0 < ${COMMON_PATCHES}/${KDU_MAKE}.patch
DOWNLOAD_COMMAND unzip -o -d ${COMMON_SRCS} ${COMMON_VENDOR}/v7_A_2-01382N.zip
PATCH_COMMAND patch -d ${COMMON_SRCS}/v7_A_2-01382N -p0 < ${COMMON_PATCHES}/${KDU_MAKE}.patch
CONFIGURE_COMMAND ""
BUILD_COMMAND make --directory=${COMMON_SRCS}/v7_9-01727L/make/ -f ${KDU_MAKE}
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_9-01727L/lib/${KDU_ARCH} ${CONFIGURE_LIBDIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_9-01727L/bin/${KDU_ARCH} ${COMMON_LOCAL}/bin
COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_9-01727L/managed/all_includes ${COMMON_LOCAL}/include
BUILD_COMMAND make --directory=${COMMON_SRCS}/v7_A_2-01382N/make/ -f ${KDU_MAKE}
INSTALL_COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_A_2-01382N/lib/${KDU_ARCH} ${CONFIGURE_LIBDIR}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_A_2-01382N/bin/${KDU_ARCH} ${COMMON_LOCAL}/bin
COMMAND ${CMAKE_COMMAND} -E copy_directory ${COMMON_SRCS}/v7_A_2-01382N/managed/all_includes ${COMMON_LOCAL}/include
)

ExternalProject_Get_Property(project_kakadu install_dir)
if(MAKE_SHARED_SIPI)
add_library(kdu SHARED IMPORTED GLOBAL)
set_property(TARGET kdu PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu_v78R.so)
set_property(TARGET kdu PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu_v7AR.so)
else()
add_library(kdu STATIC IMPORTED GLOBAL)
set_property(TARGET kdu PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu${CMAKE_STATIC_LIBRARY_SUFFIX})
endif()
add_dependencies(kdu project_kakadu)
if(MAKE_SHARED_SIPI)
add_library(kdu_aux SHARED IMPORTED GLOBAL)
set_property(TARGET kdu_aux PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu_a78R.so)
set_property(TARGET kdu_aux PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu_a7AR.so)
else()
add_library(kdu_aux STATIC IMPORTED GLOBAL)
set_property(TARGET kdu_aux PROPERTY IMPORTED_LOCATION ${CONFIGURE_LIBDIR}/libkdu_aux${CMAKE_STATIC_LIBRARY_SUFFIX})
Expand Down
Binary file added lena512.tif
Binary file not shown.
19 changes: 11 additions & 8 deletions src/formats/SipiIOJ2k.cpp
Expand Up @@ -674,8 +674,8 @@ namespace Sipi {
siz.set(Scomponents, 0, 0, (int) img->nc);
siz.set(Sdims, 0, 0, (int) img->ny); // Height of first image component
siz.set(Sdims, 0, 1, (int) img->nx); // Width of first image component
siz.set(Sprecision, 0, 0, (int) img->bps); // Bits per sample (usually 8 or 16)
siz.set(Ssigned, 0, 0, false); // Image samples are originally unsigned
siz.set(Nprecision, 0, 0, (int) img->bps); // Bits per sample (usually 8 or 16)
siz.set(Nsigned, 0, 0, false); // Image samples are originally unsigned
kdu_params *siz_ref = &siz;
siz_ref->finalize();

Expand Down Expand Up @@ -735,13 +735,14 @@ namespace Sipi {
codestream.access_siz()->parse_string("Cprecincts={256,256}");
codestream.access_siz()->parse_string("Cblk={64,64}");
codestream.access_siz()->parse_string("Cuse_sop=yes");

//codestream.access_siz()->parse_string("Stiles={1024,1024}");
//codestream.access_siz()->parse_string("ORGgen_plt=yes");
//codestream.access_siz()->parse_string("ORGtparts=R");

codestream.access_siz()->finalize_all(); // Set up coding defaults

jp2_family_dimensions.init(&siz); // initalize dimension box

if (img->icc != nullptr) {
PredefinedProfiles icc_type = img->icc->getProfileType();
switch (icc_type) {
Expand Down Expand Up @@ -778,17 +779,18 @@ namespace Sipi {
break;
}
case icc_GRAY_D50: {
unsigned int icc_len;
kdu_byte *icc_bytes = (kdu_byte *) img->icc->iccBytes(icc_len);
jp2_family_colour.init(icc_bytes);
//unsigned int icc_len;
//kdu_byte *icc_bytes = (kdu_byte *) img->icc->iccBytes(icc_len);
//jp2_family_colour.init(icc_bytes); // TODO: DOES NOT WORK AS EXPECTED!!!!! Fallback below
jp2_family_colour.init(JP2_sLUM_SPACE);
break;
}
case icc_LUM_D65: {
jp2_family_colour.init(JP2_sLUM_SPACE);
jp2_family_colour.init(JP2_sLUM_SPACE); // TODO: just a fallback
break;
}
case icc_ROMM_GRAY: {
jp2_family_colour.init(JP2_sLUM_SPACE);
jp2_family_colour.init(JP2_sLUM_SPACE); // TODO: just a fallback
break;
}
default: {
Expand All @@ -814,6 +816,7 @@ namespace Sipi {
}
}
}

jp2_family_channels.init(img->nc - img->es.size());
for (int c = 0; c < img->nc - img->es.size(); c++) jp2_family_channels.set_colour_mapping(c, c);
for (int c = 0; c < img->es.size(); c++) jp2_family_channels.set_opacity_mapping(img->nc + c, img->nc + c);
Expand Down
55 changes: 55 additions & 0 deletions src/formats/SipiIOTiff.cpp
Expand Up @@ -441,6 +441,8 @@ namespace Sipi {
done = true;
}
}
//============================================================================


bool SipiIOTiff::read(SipiImage *img, std::string filepath, std::shared_ptr<SipiRegion> region,
std::shared_ptr<SipiSize> size, bool force_bps_8,
Expand Down Expand Up @@ -484,6 +486,27 @@ namespace Sipi {
img->photo = (PhotometricInterpretation) stmp;
}

//
// if we have a palette TIFF with a colormap, it gets complicated. We will have to
// read the colormap and later convert the image to RGB, since we do internally
// not support palette images.
//
uint16 *rcm = nullptr, *gcm = nullptr, *bcm = nullptr;
int colmap_len = 0;
if (img->photo == PALETTE) {
if (TIFFGetField(tif, TIFFTAG_COLORMAP, &rcm, &gcm, &bcm) == 0) {
TIFFClose(tif);
std::string msg = "TIFFGetField of TIFFTAG_COLORMAP failed: " + filepath;
throw Sipi::SipiImageError(__file__, __LINE__, msg);
}
colmap_len = 2;
int itmp = 0;
while (itmp < img->bps) {
colmap_len *= 2;
itmp++;
}
}

TIFF_GET_FIELD (tif, TIFFTAG_PLANARCONFIG, &planar, PLANARCONFIG_CONTIG);
TIFF_GET_FIELD (tif, TIFFTAG_SAMPLEFORMAT, &safo, SAMPLEFORMAT_UINT);

Expand Down Expand Up @@ -807,6 +830,38 @@ namespace Sipi {

TIFFClose(tif);


if (img->photo == PALETTE) {
//
// ok, we have a palette color image we have to convert to RGB...
//
uint16 cm_max = 0;
for (int i = 0; i < colmap_len; i++) {
if (rcm[i] > cm_max) cm_max = rcm[i];
if (gcm[i] > cm_max) cm_max = gcm[i];
if (bcm[i] > cm_max) cm_max = bcm[i];
}
uint8 *dataptr = new uint8[3*img->nx*img->ny];
if (cm_max < 256) { // we have a colomap with entries form 0 - 255
for (int i = 0; i < img->nx*img->ny; i++) {
dataptr[3*i] = (uint8) rcm[img->pixels[i]];
dataptr[3*i + 1] = (uint8) gcm[img->pixels[i]];
dataptr[3*i + 2] = (uint8) bcm[img->pixels[i]];
}
}
else { // we have a colormap with entries > 255, assuming 16 bit
for (int i = 0; i < img->nx*img->ny; i++) {
dataptr[3*i] = (uint8) (rcm[img->pixels[i]] >> 8);
dataptr[3*i + 1] = (uint8) (gcm[img->pixels[i]] >> 8);
dataptr[3*i + 2] = (uint8) (bcm[img->pixels[i]] >> 8);
}
}
delete[] img->pixels;
img->pixels = dataptr; dataptr = nullptr;
img->photo = RGB;
img->nc = 3;
}

if (img->icc == nullptr) {
switch (img->photo) {
case MINISBLACK: {
Expand Down
22 changes: 13 additions & 9 deletions src/metadata/SipiIcc.cpp
Expand Up @@ -124,24 +124,28 @@ namespace Sipi {
break;
}
case icc_GRAY_D50: {
cmsContext context = cmsCreateContext(0, 0);
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), cmsBuildGamma(context, 2.2));
cmsContext context = cmsCreateContext(nullptr, nullptr);
cmsToneCurve *gamma_2_2 = cmsBuildGamma(context, 2.2);
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), gamma_2_2);
cmsFreeToneCurve(gamma_2_2);
cmsDeleteContext(context);
profile_type = icc_GRAY_D50;
break;
}
case icc_LUM_D65: {
cmsContext context = cmsCreateContext(0, 0);
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), cmsBuildGamma(context, 2.4));
cmsContext context = cmsCreateContext(nullptr, nullptr);
cmsToneCurve *gamma_2_4 = cmsBuildGamma(context, 2.4);
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), gamma_2_4);
cmsFreeToneCurve(gamma_2_4);
cmsDeleteContext(context);
profile_type = icc_LUM_D65;
break;
}
case icc_ROMM_GRAY: {
cmsContext context = cmsCreateContext(nullptr, nullptr);
if (context == nullptr) std::cerr << "1 +++++++++++++++++++++++" << std::endl;
cmsToneCurve* gaga = cmsBuildGamma(context, 1.8);
if (gaga == nullptr) std::cerr << "2 +++++++++++++++++++++++" << std::endl;
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), gaga);
if (icc_profile == nullptr) std::cerr << "3 +++++++++++++++++++++++" << std::endl;
cmsToneCurve* gamma_1_8 = cmsBuildGamma(context, 1.8);
icc_profile = cmsCreateGrayProfileTHR(context, cmsD50_xyY(), gamma_1_8);
cmsFreeToneCurve(gamma_1_8);
cmsDeleteContext(context);
profile_type = icc_ROMM_GRAY;
break;
Expand Down
2 changes: 2 additions & 0 deletions test/unit/CMakeLists.txt
Expand Up @@ -6,6 +6,8 @@
# Trivial example using gtest and gmock. Can be directly added here.
# To only run this single test, run from inside the build directory (cd test/unit && ./example)
add_executable(example example.cpp)
add_dependencies(example icc_profiles)

target_link_libraries(example gtest gmock_main)
add_test(NAME example_unit_test COMMAND example)

Expand Down
2 changes: 2 additions & 0 deletions test/unit/sipiimage/CMakeLists.txt
@@ -1,3 +1,4 @@

link_directories(
/usr/local/lib
${PROJECT_SOURCE_DIR}/local/lib
Expand Down Expand Up @@ -57,6 +58,7 @@ add_executable(
${PROJECT_SOURCE_DIR}/shttps/jwt.c ${PROJECT_SOURCE_DIR}/shttps/jwt.h
${PROJECT_SOURCE_DIR}/shttps/makeunique.h ${PROJECT_SOURCE_DIR}/src/SipiFilenameHash.cpp ${PROJECT_SOURCE_DIR}/include/SipiFilenameHash.h
)
add_dependencies(sipiimage icc_profiles)

target_link_libraries(
sipiimage
Expand Down
10 changes: 10 additions & 0 deletions test_tifs.sh
@@ -0,0 +1,10 @@
#!/bin/bash
#
for file in $1/*.tif
do
if test -f "$file"
then
echo "processing $file..."
/usr/local/bin/sipi --file $file --format jpx ${file%%.*}.jp2
fi
done
Binary file removed vendor/v7_9-01727L.zip.enc
Binary file not shown.
Binary file added vendor/v7_A_2-01382N.zip.enc
Binary file not shown.

0 comments on commit 297d032

Please sign in to comment.