about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-07-13 16:38:53 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2017-07-13 16:38:53 +0000
commit11058d75b808d60f100f4822ee4f145fe3929274 (patch)
tree46e6329381ed70eeffca838da5e68934e2abc69d
parent7a0fd9645646acc23c06098b5f3459eee7352b77 (diff)
downloadnetpbm-mirror-11058d75b808d60f100f4822ee4f145fe3929274.tar.gz
netpbm-mirror-11058d75b808d60f100f4822ee4f145fe3929274.tar.xz
netpbm-mirror-11058d75b808d60f100f4822ee4f145fe3929274.zip
Use ppphist/ppmmake instead of pamcut/pamscale to create background-color file; use ppmhist/ppmchange instead of pamarith to create background mask file
git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@3032 9d0c8265-081b-0410-96cb-a4ca84ce46f8
-rw-r--r--doc/HISTORY4
-rwxr-xr-xeditor/ppmshadow44
2 files changed, 39 insertions, 9 deletions
diff --git a/doc/HISTORY b/doc/HISTORY
index b742b8f4..0e61ab03 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -6,6 +6,10 @@ CHANGE HISTORY
 
 not yet  BJH  Release 10.80.00
 
+              ppmshadow: handle images with a black background and low
+              contrast images (i.e. little difference between foreground and
+              background).
+
               pgmmake: Fix bug: treats non-numeric gray-level argument as zero.
               Always broken (Pgmmake was new in Netpbm 10.32, February 2006).
 
diff --git a/editor/ppmshadow b/editor/ppmshadow
index a46341d7..ae6b1b0f 100755
--- a/editor/ppmshadow
+++ b/editor/ppmshadow
@@ -73,7 +73,7 @@ sub doVersionHack($) {
 sub imageDimensions($) {
     my ($fileName) = @_;
 #-----------------------------------------------------------------------------
-#  Return the dimensions of the Netpbm image in the named file
+#  Return the dimensions of the Netpbm image in the file named $fileName.
 #-----------------------------------------------------------------------------
     my ($width, $height, $depth);
     my $pamfileOutput = `pamfile $fileName`;
@@ -86,6 +86,29 @@ sub imageDimensions($) {
     return ($width, $height, $depth);
 }    
 
+sub backgroundColor($) {
+    my ($fileName) = @_;
+#-----------------------------------------------------------------------------
+#  Return the color of the backround of the image in the file named $fileName.
+#-----------------------------------------------------------------------------
+    # We call the color of the top left pixel the background color.
+
+    my $ppmhistOut = qx{pamcut 0 0 1 1 $fileName | ppmhist -noheader -float};
+
+    my ($ired, $igrn, $iblu, $lum, $count);
+
+    if ($ppmhistOut =~
+        m{\s*([01].\d+)\s*([01].\d+)\s*([01].\d+)\s*([01].\d+)\s*(\d+)}) {
+        ($ired, $igrn, $iblu, $lum, $count) = ($1, $2, $3, $4, $5);
+    } else {
+        die("Unrecognized format of output from 'ppmhist' shell command");
+    }
+    my $irgb = sprintf("rgbi:%f/%f/%f", $ired, $igrn, $iblu);
+
+    return $irgb;
+}    
+
+
 
 sub makeConvolutionKernel($$) {
     my ($convkernelfile, $ckern) = @_;
@@ -211,19 +234,22 @@ system("ppmtoppm");
 my ($sourceImageWidth, $sourceImageHeight, $sourceImageDepth) =
     imageDimensions($infile);
 
-#   Create an all-background-color image (same size as original image)
+my $bgColorIrgb = backgroundColor($infile);
+
+# Create an all-background-color image (same size as original image),
+# named $backgroundfile. 
 
 my $backgroundfile = "$ourtmp/background.ppm";
-system("pamcut -left=0 -top=0 -width=1 -height=1 $infile | " .
-       "pamscale -xsize=$sourceImageWidth " .
-       "-ysize=$sourceImageHeight >$backgroundfile");
+system("ppmmake $bgColorIrgb $sourceImageWidth $sourceImageHeight " .
+    "-maxval $sourceImageDepth " .
+    ">$backgroundfile");
 
-#   Create mask file for background, named $bgmaskfile.  It is a PBM, white
-#   wherever there is background image in the input.
+# Create mask file for background, named $bgmaskfile.  It is a PBM, white
+# wherever there is background image in the input.
 
 my $bgmaskfile = "$ourtmp/bgmask.pbm";
-system("pamarith -difference $infile $backgroundfile | pnminvert | ppmtopgm " .
-       "| pgmtopbm -thresh -value 1.0 >$bgmaskfile");
+system("ppmchange -remainder=black $bgColorIrgb white $infile | " .
+       "ppmtopgm | pgmtopbm -threshold -value=0.5 >$bgmaskfile"); 
 
 my $ckern = $convolve <= 11 ? $convolve : 11;