1-bit tiff rendering

Frank Warmerdam warmerdam at POBOX.COM
Tue Sep 14 12:49:31 EDT 2004


GIRAUD Pierre wrote:
>  Hi list,
>
> I do have B&W tif's (1-bit coded), and when I want to get those rendered
> by mapserver, I obtain bad restitution within some zoomscales. The map
> image is also B&W. I would prefer it to be in greyscale.
> Can I easily do this with mapserver.
>
> To have this working, I transformed my tiff manually with photoshop to
> have an 8-bit greyscale image. Can I do this easily with the libtiff
> tools ?
> And then I applied gdaladdo to get overviews. This last operation seems
> to be necessary to have a good rendering.
>
> Did some of you have similar problems and got it work ?

Pierre,

If you want to get reasonable greyscale values from a BW 1bit image as you
zoom out it is necessary to build overviews, and to ensure you use the
"-r average" switch to average values rather than the default nearest
neighbour sampling.

I believe that 1 bit TIFF files are represented as pseudocolored images
with 2 color tables entries.  To get a good greyscale result you will also
likely need to recode this to an 8bit image with 0 for black and 255 for
white.  Thus the averaging for overviews will give intermediate values.  If
you just do the averaging on the "pseudocolored" 1 bit file you won't get
this effect.

Back to the original file, the reason it generally doesn't look good at
overview zoom levels is because mapserver internally uses nearest neighbour
sampling unless overviews are pre-built via GDAL.  The nearest neighbour
sampling will pick one pixel in the region mapped to an output pixel as the
value.  For continuous tone images that vary fairly smoothly this is generally
ok.  But for 1bit linework scans such as some BW map scans, you will get spotty
representation of linework in the overviews and various aliasing effect could
result in some horizontal or vertical lines essentially disappearing at some
zoom levels.

While, a bit of a hack, you can use GDAL to do all the appropriate conversions
if you have the python support working smoothly.  From the commandline you
would do:

# Step 1: convert paletted image to 24bit RGB.
pct2rgb.py input_1bit.tif 24bit.tif

# Step 2: extract one band of the RGB image as greyscale image
gdal_translate -b 1 24bit.tif 8bit.tif

# Step 3: Build overviews
gdaladdo -r average 8bit.tif 2 4 8 16 32 64

# Step 4: Use the 8bit.tif in your mapfile.
...

This process should preserve the georeferencing (if available) from the
original file.  You might also want to apply compression to file in the
step where the greyscale image is extracted if space is an issue, and
also produce the greyscale image as a tiled image for more efficient access
from mapserver.  To do this alter step 2 to be:

   gdal_translate -b 1 -co TILED=YES -co COMPRESS=PACKBITS 24bit.tif 8bit.tif

I hope this helps a bit.  Note, you can convert the original file to 24bit
with libtiff tools (the tiff2rgba program) but I don't think there is a tool
to extract a single band, and there is no overview building tool normally built
with libtiff.  You might want to write a message to the libtiff maintainer
telling him that his library and tools suck, and that GDAL rocks.

PS. That last part is a joke ... I am the maintainer/co-maintainer for both.

Best regards,

--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the mapserver-users mailing list