~jwh376/helioviewer.org/jonathan-dev

« back to all changes in this revision

Viewing changes to api/modules/lib/JP2Image.php

  • Committer: Jonathan Harper
  • Date: 2010-01-22 18:22:57 UTC
  • Revision ID: jharper@tethys-20100122182257-j74xk4a4cxf5fduz
Created new organization for API-- split into modules WebClient, JHelioViewer, Movies.
Added class Helper in the modules folder to include shared validation functions.
api/modules/lib holds what was previously api/lib/helioviewer, back-end processing functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * @package JP2Image - JPEG 2000 Image Class
 
4
 * @author Keith Hughitt <Vincent.K.Hughitt@nasa.gov>
 
5
 * @author Jaclyn Beck
 
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.
 
11
 */
 
12
class JP2Image {
 
13
    private $file;   //$jp2;
 
14
    private $width;  //$jp2Width;
 
15
    private $height; //$jp2Height;
 
16
    private $scale;  //$jp2Scale;
 
17
    
 
18
    /**
 
19
     * @param string file -- Location of the JPEG 2000 image to work with
 
20
     */
 
21
    public function __construct($file, $width, $height, $scale) {
 
22
        $this->file   = $file;
 
23
        $this->width  = $width;
 
24
        $this->height = $height;
 
25
        $this->scale  = $scale;
 
26
    }
 
27
    
 
28
    /**
 
29
     * getScale
 
30
     * @return 
 
31
     */
 
32
    public function getScale() {
 
33
        return $this->scale;
 
34
    }
 
35
    
 
36
    public function getWidth() {
 
37
        return $this->width;
 
38
    }
 
39
    
 
40
    public function getHeight() {
 
41
        return $this->height;
 
42
    }
 
43
 
 
44
    /**
 
45
     * Extract a region using kdu_expand
 
46
     * @return String - outputFile of the expanded region 
 
47
     * @param $outputFile String - JP2 outputFile
 
48
     * 
 
49
     * @TODO: Should precision of -reduce be limited in same manner as region strings? (e.g. MDI @ zoom-level 9)
 
50
     */
 
51
    public function extractRegion($outputFile, $roi, $scaleFactor = 0) {
 
52
        $cmd = HV_KDU_EXPAND . " -i $this->file -o $outputFile ";
 
53
         
 
54
        // Case 1: JP2 image resolution = desired resolution
 
55
        // Nothing special to do...
 
56
    
 
57
        // Case 2: JP2 image resolution > desired resolution (use -reduce)        
 
58
        if ($scaleFactor > 0)
 
59
            $cmd .= "-reduce $scaleFactor ";
 
60
 
 
61
        // Case 3: JP2 image resolution < desired resolution
 
62
        // Don't do anything...
 
63
 
 
64
        // Add desired region
 
65
        $cmd .= $this->getRegionString($roi);
 
66
        
 
67
        //echo $cmd;
 
68
        //exit();
 
69
 
 
70
        // Execute the command
 
71
        try {
 
72
            $line = exec(HV_PATH_CMD . $cmd, $out, $ret);
 
73
            if (($ret != 0) || (sizeof($out) > 5)) {
 
74
                var_dump($out);
 
75
                throw new Exception("COMMAND: $cmd\n\t $line");
 
76
            }
 
77
                
 
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);
 
81
            print $error;
 
82
        }
 
83
    }
 
84
    
 
85
    /**
 
86
     * getRegionString
 
87
     * Build a region string to be used by kdu_expand. e.g. "-region {0.0,0.0},{0.5,0.5}"
 
88
     * 
 
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.
 
91
     */
 
92
    private function getRegionString($roi) {
 
93
        $precision = 6;
 
94
        
 
95
        $top    = $roi["top"];
 
96
        $left   = $roi["left"];
 
97
        $bottom = $roi["bottom"];
 
98
        $right  = $roi["right"];
 
99
 
 
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);
 
105
        
 
106
        $region = "-region \{$scaledTop,$scaledLeft\},\{$scaledHeight,$scaledWidth\}";
 
107
 
 
108
        return $region;        
 
109
    }    
 
110
}
 
111
?>