82
82
protected function buildImage($filename) {
83
$relTs = $this->relativeTilesize;
85
83
// extract region from JP2
86
84
$pgm = $this->extractRegion($filename);
90
86
// Use PNG as intermediate format so that GD can read it in
91
87
$png = substr($filename, 0, -3) . "png";
88
exec("convert $pgm -depth 8 -quality 10 $png");
90
// Apply color-lookup table
91
if (($this->detector == "EIT") || ($this->measurement == "0WL")) {
92
$clut = $this->getColorTable($this->detector, $this->measurement);
93
$this->setColorPalette($png, $clut, $png);
96
// IM command for transparency, padding, rescaling, etc.
97
$cmd = "convert $png -background black ";
99
// Apply alpha mask for images with transparent components
100
if ($this->hasAlphaMask()) {
101
$mask = substr($filename, 0, -4) . "-mask.tif";
93
105
// Determine relative size of image at this scale
94
106
$jp2RelWidth = $this->jp2Width / $this->desiredToActual;
95
107
$jp2RelHeight = $this->jp2Height / $this->desiredToActual;
97
$cmd = "convert $pgm ";
99
// For images with transparent components, convert pixels with value "0" to be transparent.
100
if ($this->measurement == "0WL")
101
$cmd .= "-transparent black ";
103
109
// Get dimensions of extracted region
104
110
$extracted = $this->getImageDimensions($pgm);
106
112
// Pad up the the relative tilesize (in cases where region extracted for outer tiles is smaller than for inner tiles)
113
$relTs = $this->relativeTilesize;
107
114
if (($relTs < $this->tileSize) && (($extracted['width'] < $relTs) || ($extracted['height'] < $relTs))) {
108
$pad = "convert $pgm " . $this->padImage($jp2RelWidth, $jp2RelHeight, $extracted['width'], $extracted['height'], $relTs, $this->xRange["start"], $this->yRange["start"]) . " $pgm";
115
$pad = "convert $png -background black " . $this->padImage($jp2RelWidth, $jp2RelHeight, $extracted['width'], $extracted['height'], $relTs, $this->xRange["start"], $this->yRange["start"]) . " $png";
112
119
// Resize if necessary (Case 3)
113
//if (($relTs < $this->tileSize) || ($extracted['width'] > $this->tileSize) || ($extracted['height'] > $this->tileSize))
114
120
if ($relTs < $this->tileSize)
115
121
$cmd .= "-geometry " . $this->tileSize . "x" . $this->tileSize . "! ";
117
123
// Refetch dimensions of extracted region
118
$tile = $this->getImageDimensions($pgm);
124
$tile = $this->getImageDimensions($png);
120
126
// Pad if tile is smaller than it should be (Case 2)
121
127
if ((($tile['width'] < $this->tileSize) || ($tile['height'] < $this->tileSize)) && ($relTs >= $this->tileSize)) {
122
128
$cmd .= $this->padImage($jp2RelWidth, $jp2RelHeight, $tile['width'], $tile['height'], $this->tileSize, $this->xRange["start"], $this->yRange["start"]);
131
if ($this->hasAlphaMask()) {
132
$cmd .= "-compose copy_opacity -composite ";
125
135
// Compression settings & Interlacing
126
136
$cmd .= $this->setImageParams();
128
//echo ("$cmd $png");
138
//echo ("$cmd $filename");
132
if (($this->detector == "EIT") || ($this->measurement == "0WL")) {
134
$clut = $this->getColorTable($this->detector, $this->measurement);
135
$this->setColorPalette($png, $clut, $filename);
138
exec("$cmd $filename");
140
// Remove intermediate file
142
//convert /var/www/hv/cache/512/2003/10/08/1135_13_+00_+00.png -background black
143
// /var/www/hv/cache/512/2003/10/08/1135_13_+00_+00-mask.tif -gravity NorthWest -extent 512x512
144
// -compose copy_opacity -composite -quality 20 -interlace plane -depth 8 -colors 256 test.png
147
exec("$cmd $filename");
149
// Remove intermediate file (note: remove mask)
143
152
return $filename;
179
190
// Intermediate image file
180
191
$pgm = substr($filename, 0, -3) . "pgm";
182
$cmd = "$this->kdu_expand -i $this->jp2 -o $pgm ";
193
// For images with transparent parts, extract a mask as well
194
if ($this->hasAlphaMask()) {
195
$mask = substr($filename, 0, -4) . "-mask.tif";
196
$cmd = "$this->kdu_expand -i $this->jp2 -raw_components -o $pgm,$mask ";
199
$cmd = "$this->kdu_expand -i $this->jp2 -o $pgm ";
184
202
// Case 1: JP2 image resolution = desired resolution
185
203
// Nothing special to do...