3
* @package JP2Image - JPEG 2000 Image Class
4
* @author Keith Hughitt <Vincent.K.Hughitt@nasa.gov>
6
* @TODO: Extend Exception class to create more useful objects.
7
* @TODO: Use different name for intermediate PNG than final version.
8
* @TODO: Forward request to secondary server if it fails for a valid tile?
9
* @TODO: build up a "process log" string for each tile process which can be
10
* output to the log in case of failure.
13
private $file; //$jp2;
14
private $width; //$jp2Width;
15
private $height; //$jp2Height;
16
private $scale; //$jp2Scale;
19
* @param string file -- Location of the JPEG 2000 image to work with
21
public function __construct($file, $width, $height, $scale) {
23
$this->width = $width;
24
$this->height = $height;
25
$this->scale = $scale;
32
public function getScale() {
36
public function getWidth() {
40
public function getHeight() {
45
* Extract a region using kdu_expand
46
* @return String - outputFile of the expanded region
47
* @param $outputFile String - JP2 outputFile
49
* @TODO: Should precision of -reduce be limited in same manner as region strings? (e.g. MDI @ zoom-level 9)
51
public function extractRegion($outputFile, $roi, $scaleFactor = 0) {
52
$cmd = HV_KDU_EXPAND . " -i $this->file -o $outputFile ";
54
// Case 1: JP2 image resolution = desired resolution
55
// Nothing special to do...
57
// Case 2: JP2 image resolution > desired resolution (use -reduce)
59
$cmd .= "-reduce $scaleFactor ";
61
// Case 3: JP2 image resolution < desired resolution
62
// Don't do anything...
65
$cmd .= $this->getRegionString($roi);
70
// Execute the command
72
$line = exec(HV_PATH_CMD . $cmd, $out, $ret);
73
if (($ret != 0) || (sizeof($out) > 5)) {
75
throw new Exception("COMMAND: $cmd\n\t $line");
78
} catch(Exception $e) {
79
$error = "[kdu][" . date("Y/m/d H:i:s") . "]\n\t " . $e->getMessage() . "\n\n";
80
file_put_contents(HV_ERROR_LOG, $error,FILE_APPEND);
87
* Build a region string to be used by kdu_expand. e.g. "-region {0.0,0.0},{0.5,0.5}"
89
* NOTE: Because kakadu's internal precision for region strings is less than PHP,
90
* the numbers used are cut off to prevent erronious rounding.
92
private function getRegionString($roi) {
97
$bottom = $roi["bottom"];
98
$right = $roi["right"];
100
// Calculate the top, left, width, and height in terms of kdu_expand parameters (between 0 and 1)
101
$scaledTop = substr($top / $this->height, 0, $precision);
102
$scaledLeft = substr($left / $this->width, 0, $precision);
103
$scaledHeight = substr(($bottom - $top) / $this->height, 0, $precision);
104
$scaledWidth = substr(($right - $left) / $this->width, 0, $precision);
106
$region = "-region \{$scaledTop,$scaledLeft\},\{$scaledHeight,$scaledWidth\}";