~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to api/lib/helioviewer/JP2Image.Manual.php

  • Committer: V. Keith Hughitt
  • Date: 2009-03-26 19:20:57 UTC
  • Revision ID: hughitt1@kore-20090326192057-u0x8rf8sf5lmmnwh
nightly build 03-26-2009: Using alpha-channel JPEG 2000 dataset

Show diffs side-by-side

added added

removed removed

Lines of Context:
80
80
         * @return
81
81
         */
82
82
        protected function buildImage($filename) {
83
 
                $relTs = $this->relativeTilesize;
84
 
                
85
83
                // extract region from JP2
86
84
                $pgm = $this->extractRegion($filename);
87
85
                
88
 
                //exit();
89
 
                
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");
 
89
                
 
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);
 
94
                }
 
95
 
 
96
                // IM command for transparency, padding, rescaling, etc.
 
97
                $cmd = "convert $png -background black ";
 
98
                
 
99
                // Apply alpha mask for images with transparent components
 
100
                if ($this->hasAlphaMask()) {
 
101
                        $mask = substr($filename, 0, -4) . "-mask.tif";
 
102
                        $cmd .= "$mask ";
 
103
                }
92
104
                
93
105
                // Determine relative size of image at this scale
94
106
                $jp2RelWidth  = $this->jp2Width  /  $this->desiredToActual;
95
107
                $jp2RelHeight = $this->jp2Height /  $this->desiredToActual;
96
108
                
97
 
                $cmd = "convert $pgm ";
98
 
 
99
 
                // For images with transparent components, convert pixels with value "0" to be transparent.
100
 
                if ($this->measurement == "0WL")
101
 
                        $cmd .= "-transparent black ";
102
 
                
103
109
                // Get dimensions of extracted region
104
110
                $extracted = $this->getImageDimensions($pgm);
105
111
 
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";
109
116
                        exec($pad);
110
117
                }               
111
118
                
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 . "! ";
116
122
 
117
123
                // Refetch dimensions of extracted region
118
 
                $tile = $this->getImageDimensions($pgm);
 
124
                $tile = $this->getImageDimensions($png);
119
125
                
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"]);
123
129
                }
124
130
                
 
131
                if ($this->hasAlphaMask()) {
 
132
                        $cmd .= "-compose copy_opacity -composite ";
 
133
                }
 
134
                
125
135
                // Compression settings & Interlacing
126
136
                $cmd .= $this->setImageParams();
127
 
 
128
 
                //echo ("$cmd $png");
 
137
                
 
138
                //echo ("$cmd $filename");
129
139
                //exit();
130
 
 
131
 
                // Apply color table
132
 
                if (($this->detector == "EIT") || ($this->measurement == "0WL")) {
133
 
                        exec("$cmd $png");
134
 
                        $clut = $this->getColorTable($this->detector, $this->measurement);
135
 
                        $this->setColorPalette($png, $clut, $filename);
136
 
                }
137
 
                else
138
 
                        exec("$cmd $filename");
139
 
                        
140
 
                // Remove intermediate file
141
 
                //unlink($pgm);
142
140
                
 
141
                //WORKING:
 
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
 
145
 
 
146
                // Execute command
 
147
                exec("$cmd $filename");
 
148
 
 
149
                // Remove intermediate file (note: remove mask)
 
150
                //unlink($pgm);         
 
151
        
143
152
                return $filename;
144
153
        }
145
154
        
148
157
         * @return String Image compression and quality related flags.
149
158
         */
150
159
        private function setImageParams() {
151
 
                $args = "-type palette -quality " . Config::PNG_COMPRESSION_QUALITY;
 
160
                $args = " -quality ";
152
161
                if ($this->getImageFormat() == "png") {
153
 
                        $args .= " -colors " . Config::NUM_COLORS;
 
162
                        $args .= Config::PNG_COMPRESSION_QUALITY . " -interlace plane";
 
163
                } else {
 
164
                        $args .= Config::JPEG_COMPRESSION_QUALITY . " -interlace line";
154
165
                }
155
 
                $args .= " -depth " . Config::BIT_DEPTH . " ";
 
166
                $args .= " -depth " . Config::BIT_DEPTH . " -colors " . Config::NUM_COLORS . " ";
156
167
                
157
168
                return $args;
158
169
        }
179
190
                // Intermediate image file
180
191
                $pgm = substr($filename, 0, -3) . "pgm";
181
192
                
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 ";
 
197
                }
 
198
                else {
 
199
                        $cmd = "$this->kdu_expand -i $this->jp2 -o $pgm ";
 
200
                }
183
201
                
184
202
                // Case 1: JP2 image resolution = desired resolution
185
203
                // Nothing special to do...
355
373
                
356
374
                // Construct padding command
357
375
                // TEST: use black instead of transparent for background?
358
 
                return "-background black -gravity $gravity -extent $ts" . "x" . "$ts ";
 
376
                return "-gravity $gravity -extent $ts" . "x" . "$ts ";
359
377
        }
360
378
        
361
379
        private function getColorTable($detector, $measurement) {
401
419
        }
402
420
        
403
421
        /**
 
422
         * hasAlphaMask
 
423
         * @return string
 
424
         */
 
425
        private function hasAlphaMask() {
 
426
                return $this->measurement === "0WL" ? true : false;
 
427
        }
 
428
        
 
429
        /**
404
430
         * getMetaInfo
405
431
         * @param $imageId Object
406
432
         */
447
473
                $gd     = imagecreatefrompng($input);
448
474
                $ctable = imagecreatefrompng($clut);
449
475
                
 
476
                //echo "$input<br> $clut<br> $output";
 
477
                //exit();
 
478
                
450
479
                for ($i = 0; $i <= 255; $i++) {
451
480
                        $rgba = imagecolorsforindex($ctable, $i);
452
481
                        imagecolorset($gd, $i, $rgba["red"], $rgba["green"], $rgba["blue"]);
455
484
                // Enable interlacing
456
485
                imageinterlace($gd, true);
457
486
                
458
 
                $this->getImageFormat() == "jpg" ? imagejpeg($gd, $output, Config::JPEG_COMPRESSION_QUALITY) : imagepng($gd, $output); 
459
 
                
 
487
                //$this->getImageFormat() == "jpg" ? imagejpeg($gd, $output, Config::JPEG_COMPRESSION_QUALITY) : imagepng($gd, $output); 
 
488
                //if ($this->getImageFormat() == "jpg")
 
489
                //      imagejpeg($gd, $output, Config::JPEG_COMPRESSION_QUALITY);
 
490
                //else
 
491
                imagepng($gd, $output);
 
492
 
460
493
                // Cleanup
461
494
                if ($input != $output)
462
495
                        unlink($input);