Skip to content

Commit 8351596

Browse files
author
ddwiggins
committedFeb 18, 2011
first stab at improving colorspace conversion for preview images. Requires ImageMagick with LCMS support to work, and is disabled by default. git-svn-id: http://svn.resourcespace.org/svn/resourcespace@2379 c08608d7-6e46-0410-86ca-f2a6f1370df5
1 parent c960003 commit 8351596

5 files changed

+113
-7
lines changed
 
2.98 KB
Binary file not shown.
Binary file not shown.

‎include/config.default.php

+18
Original file line numberDiff line numberDiff line change
@@ -1661,3 +1661,21 @@
16611661
# $calibre_path="/usr/bin";
16621662
# Files with these extensions will be passed to calibre (if enabled above) for conversion to PDF and auto thumb-preview generation.
16631663
$calibre_extensions=array("epub","mobi","lrf","pdb","chm","cbr","cbz");
1664+
1665+
1666+
1667+
# ICC Color Management Features (Experimental)
1668+
# Note that ImageMagick must be installed and configured with LCMS support
1669+
# for this to work
1670+
1671+
# Enable extraction and use of ICC profiles from original images
1672+
$icc_extraction = false;
1673+
1674+
# target color profile for preview generation
1675+
# the file must be located in the /iccprofiles folder
1676+
# this target preview will be used for the conversion
1677+
# but will not be embedded
1678+
$icc_preview_profile = 'sRGB_IEC61966-2-1_black_scaled.icc';
1679+
1680+
# additional options for profile conversion during preview generation
1681+
$icc_preview_options = '-intent relative -black-point-compensation';

‎include/image_processing.php

+77-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<?php
23
/**
34
* Image processing functions
@@ -74,6 +75,10 @@ function upload_file($ref,$no_exif=false,$revert=false,$autorotate=false)
7475
$old_path=get_resource_path($ref,true,"",true,$old_extension);
7576
if (file_exists($old_path)) {unlink($old_path);}
7677
}
78+
79+
// also remove any existing extracted icc profiles
80+
$icc_path=get_resource_path($ref,true,"",true,'icc');
81+
if (file_exists($icc_path)) {unlink($icc_path);}
7782
}
7883

7984
if (!$revert){
@@ -110,7 +115,14 @@ function upload_file($ref,$no_exif=false,$revert=false,$autorotate=false)
110115
}
111116

112117
chmod($filepath,0777);
113-
$status="Your file has been uploaded.";
118+
119+
global $icc_extraction;
120+
if ($icc_extraction){
121+
extract_icc_profile($filepath);
122+
}
123+
124+
125+
$status="Your file has been uploaded.";
114126
}
115127
}
116128
}
@@ -759,13 +771,36 @@ function create_previews_using_im($ref,$thumbonly=false,$extension="jpg",$previe
759771
$wpath=get_resource_path($ref,true,$ps[$n]["id"],false,"jpg",-1,1,true,"",$alternative);
760772
if (file_exists($wpath)){unlink($wpath);}
761773

762-
# Preserve colour profiles? (omit for smaller sizes)
763-
$profile="+profile \"*\" -colorspace RGB"; # By default, strip the colour profiles ('+' is remove the profile, confusingly)
764-
if ($imagemagick_preserve_profiles && $id!="thm" && $id!="col" && $id!="pre" && $id!="scr") {$profile="";}
774+
775+
# EXPERIMENTAL CODE TO USE EXISTING ICC PROFILE IF PRESENT
776+
global $icc_extraction, $icc_preview_profile, $icc_preview_options;
777+
if ($icc_extraction){
778+
$iccpath = get_resource_path($ref,true,'',false,'icc');
779+
if (!file_exists($iccpath) && !isset($iccfound)) {
780+
// extracted profile doesn't exist. Try extracting.
781+
if (extract_icc_profile($file)){
782+
$iccfound = true;
783+
} else {
784+
$iccfound = false;
785+
}
786+
}
787+
}
788+
789+
if($icc_extraction && file_exists($iccpath)){
790+
// we have an extracted ICC profile, so use it as source
791+
$targetprofile = dirname(__FILE__) . '/../iccprofiles/' . $icc_preview_profile;
792+
$profile = " +profile \"*\" -profile $iccpath $icc_preview_options -profile $targetprofile ";
793+
} else {
794+
// use existing strategy for color profiles
795+
# Preserve colour profiles? (omit for smaller sizes)
796+
$profile="+profile \"*\" -colorspace RGB"; # By default, strip the colour profiles ('+' is remove the profile, confusingly)
797+
if ($imagemagick_preserve_profiles && $id!="thm" && $id!="col" && $id!="pre" && $id!="scr") {$profile="";}
798+
}
799+
765800

766801
$runcommand = $command ." +matte $profile -resize " . $tw . "x" . $th . "\">\" ".escapeshellarg($path);
767802
$output=shell_exec($runcommand);
768-
# echo $runcommand."<br>";
803+
# echo $runcommand."<br>\n";
769804
# Add a watermarked image too?
770805
global $watermark;
771806
if ($alternative==-1 && isset($watermark) && ($ps[$n]["internal"]==1 || $ps[$n]["allow_preview"]==1))
@@ -1415,4 +1450,41 @@ function AutoRotateImage ($src_image){
14151450
}
14161451

14171452

1453+
function extract_icc_profile($infile) {
1454+
global $config_windows, $imagemagick_path;
1455+
1456+
# Locate imagemagick, or fail this if it isn't installed
1457+
$command=$imagemagick_path . "/bin/convert";
1458+
if (!file_exists($command)) {$command=$imagemagick_path . "/convert";}
1459+
if (!file_exists($command)) {$command=$imagemagick_path . "\convert.exe";}
1460+
if (!file_exists($command)) {return false;}
1461+
1462+
if ($config_windows){ $stderrclause = ''; } else { $stderrclause = '2>&1'; }
1463+
1464+
// outfile will be same name as infile, except with icc ext
1465+
$ext = strrchr($infile,'.');
1466+
if ($ext !== false){
1467+
$outfile = substr($infile,0,-strlen($ext)) . '.icc';
1468+
} else {
1469+
$outfile = $infile . '.icc';
1470+
}
1471+
1472+
if (file_exists($outfile)){
1473+
// extracted profile already existed. We'll remove it and start over
1474+
unlink($outfile);
1475+
}
1476+
1477+
$cmdout= shell_exec("$command $infile $outfile $stderrclause ");
1478+
1479+
if ( preg_match("/no color profile is available/",$cmdout) || !file_exists($outfile) ||filesize($outfile) == 0){
1480+
// the icc profile extraction failed. So delete file.
1481+
if (file_exists($outfile)){ unlink ($outfile); };
1482+
return false;
1483+
}
1484+
1485+
if (file_exists($outfile)) { return true; } else { return false; }
1486+
1487+
}
1488+
1489+
14181490
?>

‎include/resource_functions.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ function delete_resource($ref)
710710
$extensions[]=$resource['file_extension']?$resource['file_extension']:"jpg";
711711
$extensions[]=$resource['preview_extension']?$resource['preview_extension']:"jpg";
712712
$extensions[]=$GLOBALS['ffmpeg_preview_extension'];
713+
$extensions[]='icc'; // also remove any extracted icc profiles
713714
$extensions=array_unique($extensions);
714715

715716
foreach ($extensions as $extension)
@@ -1128,6 +1129,7 @@ function delete_alternative_file($resource,$ref)
11281129
$extensions[]=isset($info['preview_extension'])?$info['preview_extension']:"jpg";
11291130
$extensions[]=$GLOBALS['ffmpeg_preview_extension'];
11301131
$extensions[]='jpg'; // always look for jpegs, just in case
1132+
$extensions[]='icc'; // always look for extracted icc profiles
11311133
$extensions=array_unique($extensions);
11321134
$sizes = sql_array('select id value from preview_size');
11331135

@@ -1988,13 +1990,20 @@ function get_resource_files($ref,$includeorphan=false){
19881990
unset($file_checklist[$original]);
19891991
}
19901992

1991-
// in some cases, the system also generates a jpeg equivalent of the original, so check for that
1993+
// in some cases, the system also generates an mp3 equivalent of the original, so check for that
19921994
$original = get_resource_path($ref,true,'',false,'mp3');
19931995
if (file_exists($original)){
19941996
array_push($filearray,$original);
19951997
unset($file_checklist[$original]);
19961998
}
19971999

2000+
// in some cases, the system also generates an extracted icc profile, so check for that
2001+
$original = get_resource_path($ref,true,'',false,'icc');
2002+
if (file_exists($original)){
2003+
array_push($filearray,$original);
2004+
unset($file_checklist[$original]);
2005+
}
2006+
19982007

19992008
# check for pages
20002009
$page = 1;
@@ -2032,7 +2041,7 @@ function get_resource_files($ref,$includeorphan=false){
20322041
array_push($filearray,$thepath);
20332042
unset($file_checklist[$thepath]);
20342043
}
2035-
2044+
20362045

20372046
// now check for previews
20382047
foreach($sizearray as $size){
@@ -2068,6 +2077,13 @@ function get_resource_files($ref,$includeorphan=false){
20682077
array_push($filearray,$original);
20692078
unset($file_checklist[$original]);
20702079
}
2080+
2081+
// in some cases, the system also generates an extracted icc profile, so check for that
2082+
$original = get_resource_path($ref,true,'',false,'icc',-1,1,'','',$altfile['ref']);
2083+
if (file_exists($original)){
2084+
array_push($filearray,$original);
2085+
unset($file_checklist[$original]);
2086+
}
20712087
}
20722088

20732089

0 commit comments

Comments
 (0)
Please sign in to comment.