X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=BookReaderIA%2Fdatanode%2FBookReaderImages.inc.php;h=dab21a58c1bb49eb60255d5d1c870058a16a692d;hb=b80d453515bb6b4ddfe759db4b2b8dd97c0b7f8e;hp=424cb73c3a2025ca1ce156ef966bb17745cf6ecc;hpb=c27960872fb00b44dd75dfc79d5a5338f4fe5de6;p=bookreader.git diff --git a/BookReaderIA/datanode/BookReaderImages.inc.php b/BookReaderIA/datanode/BookReaderImages.inc.php index 424cb73..dab21a5 100644 --- a/BookReaderIA/datanode/BookReaderImages.inc.php +++ b/BookReaderIA/datanode/BookReaderImages.inc.php @@ -60,6 +60,8 @@ class BookReaderImages 'tile' => 'tile', 'w' => 'width', 'h' => 'height', + 'x' => 'x', + 'y' => 'y', 'rotate' => 'rotate' ); @@ -87,7 +89,7 @@ class BookReaderImages try { $metadata = $brm->buildMetadata($_REQUEST['id'], $_REQUEST['itemPath'], $_REQUEST['subPrefix'], $_REQUEST['server']); } catch (Exception $e) { - $this->BRfatal($e->getMessage); + $this->BRfatal($e->getMessage()); } $page = $_REQUEST['page']; @@ -97,7 +99,7 @@ class BookReaderImages // deal with subPrefix if ($_REQUEST['subPrefix']) { - $parts = split('/', $_REQUEST['subPrefix']); + $parts = explode('/', $_REQUEST['subPrefix']); $bookId = $parts[count($parts) - 1 ]; } else { $bookId = $_REQUEST['id']; @@ -108,6 +110,7 @@ class BookReaderImages $basePage = $pageInfo['type']; $leaf = null; + $region = null; switch ($basePage) { case 'title': @@ -175,7 +178,7 @@ class BookReaderImages // Leaf explicitly specified $leaf = $pageInfo['value']; break; - + default: // Shouldn't be possible $this->BRfatal("Unrecognized page type requested"); @@ -191,7 +194,7 @@ class BookReaderImages $requestEnv = array( 'zip' => $metadata['zip'], 'file' => $brm->imageFilePath($leaf, $metadata['subPrefix'], $metadata['imageFormat']), - 'ext' => 'jpg', + 'ext' => 'jpg', // XXX should pass through ext ); // remove non-passthrough keys from pageInfo @@ -220,6 +223,7 @@ class BookReaderImages * Clean up temporary files */ function serveRequest($requestEnv) { + // Process some of the request parameters $zipPath = $requestEnv['zip']; $file = $requestEnv['file']; @@ -250,6 +254,26 @@ class BookReaderImages // Get the image size and depth $imageInfo = $this->getImageInfo($zipPath, $file); + $region = array(); + foreach (array('x', 'y', 'width', 'height') as $key) { + if (array_key_exists($key, $requestEnv)) { + $region[$key] = $requestEnv[$key]; + } + } + $regionDimensions = $this->getRegionDimensions($imageInfo, $region); + + /* + print('imageInfo'); + print_r($imageInfo); + print('region'); + print_r($region); + print('regionDimensions'); + print_r($regionDimensions); + print('asFloat'); + print_r($this->getRegionDimensionsAsFloat($imageInfo, $region)); + die(-1); + */ + // Output json if requested if ('json' == $ext) { // $$$ we should determine the output size first based on requested scale @@ -292,11 +316,11 @@ class BookReaderImages // image processing load on our cluster. The client should then scale to their final // needed size. - // Set scale from height or width if set - if (isset($requestEnv['height'])) { + // Set scale from height or width if set and no x or y specified + if ( isset($requestEnv['height']) && !isset($requestEnv['x']) && !isset($requestEnv['y']) ) { $powReduce = $this->nearestPow2Reduce($requestEnv['height'], $imageInfo['height']); $scale = pow(2, $powReduce); - } else if (isset($requestEnv['width'])) { + } else if ( isset($requestEnv['width']) && !isset($requestEnv['x']) && !isset($requestEnv['y']) ) { $powReduce = $this->nearestPow2Reduce($requestEnv['width'], $imageInfo['width']); $scale = pow(2, $powReduce); @@ -330,6 +354,7 @@ class BookReaderImages // $$$ consider doing a 302 here instead, to make better use of the browser cache // Limit scaling for 1-bit images. See https://bugs.edge.launchpad.net/bookreader/+bug/486011 if (1 == $imageInfo['bits']) { + if ($scale > 1) { $scale /= 2; $powReduce -= 1; @@ -351,7 +376,7 @@ class BookReaderImages $unzipCmd = $this->getUnarchiveCommand($zipPath, $file); - $decompressCmd = $this->getDecompressCmd($imageInfo['type'], $powReduce, $rotate, $scale, $stdoutLink); + $decompressCmd = $this->getDecompressCmd($imageInfo, $powReduce, $rotate, $scale, $region, $stdoutLink); // Non-integer scaling is currently disabled on the cluster // if (isset($_REQUEST['height'])) { @@ -389,23 +414,27 @@ class BookReaderImages $errorMessage = ''; + if (! $this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest // $$$ automated reporting trigger_error('BookReader Processing Error: ' . $cmd . ' -- ' . $errorMessage, E_USER_WARNING); // Try some content-specific recovery - $recovered = false; + $recovered = false; if ($imageInfo['type'] == 'jp2') { $records = $this->getJp2Records($zipPath, $file); - if ($powReduce > intval($records['Clevels'])) { - $powReduce = $records['Clevels']; - $reduce = pow(2, $powReduce); + if (array_key_exists('Clevels', $records)) { + $maxReduce = intval($records['Clevels']); + trigger_error("BookReader using max reduce $maxReduce from jp2 records"); } else { - $reduce = 1; - $powReduce = 0; + $maxReduce = 0; } - - $cmd = $unzipCmd . $this->getDecompressCmd($imageInfo['type'], $powReduce, $rotate, $scale, $stdoutLink) . $compressCmd; + + $powReduce = min($powReduce, $maxReduce); + $reduce = pow(2, $powReduce); + + $cmd = $unzipCmd . $this->getDecompressCmd($imageInfo, $powReduce, $rotate, $scale, $region, $stdoutLink) . $compressCmd; + trigger_error('BookReader rerunning with new cmd: ' . $cmd, E_USER_WARNING); if ($this->passthruIfSuccessful($headers, $cmd, $errorMessage)) { // $$$ move to BookReaderRequest $recovered = true; } else { @@ -571,12 +600,14 @@ class BookReaderImages echo $jsonOutput; } - function getDecompressCmd($imageType, $powReduce, $rotate, $scale, $stdoutLink) { + function getDecompressCmd($srcInfo, $powReduce, $rotate, $scale, $region, $stdoutLink) { - switch ($imageType) { + switch ($srcInfo['type']) { case 'jp2': + $regionAsFloat = $this->getRegionDimensionsAsFloat($srcInfo, $region); + $regionString = sprintf("{%f,%f},{%f,%f}", $regionAsFloat['y'], $regionAsFloat['x'], $regionAsFloat['h'], $regionAsFloat['w']); $decompressCmd = - " | " . $this->kduExpand . " -no_seek -quiet -reduce $powReduce -rotate $rotate -i /dev/stdin -o " . $stdoutLink; + " | " . $this->kduExpand . " -no_seek -quiet -reduce $powReduce -rotate $rotate -region $regionString -i /dev/stdin -o " . $stdoutLink; if ($this->decompressToBmp) { // We suppress output since bmptopnm always outputs on stderr $decompressCmd .= ' | (bmptopnm 2>/dev/null)'; @@ -611,6 +642,7 @@ class BookReaderImages return $decompressCmd; } + // If the command has its initial output on stdout the headers will be emitted followed // by the stdout output. If initial output is on stderr an error message will be // returned. @@ -652,15 +684,34 @@ class BookReaderImages $read = array($stdout, $stderr); $write = NULL; $except = NULL; + $numChanged = stream_select($read, $write, $except, NULL); // $$$ no timeout if (false === $numChanged) { // select failed $errorMessage = 'Select failed'; $retVal = false; - } - if ($read[0] == $stdout && (1 == $numChanged)) { - // Got output first on stdout (only) - // $$$ make sure we get all stdout + error_log('BookReader select failed!'); + } else { + if (in_array($stderr, $read)) { + // Either content in stderr, or stderr is closed (could read 0 bytes) + $error = stream_get_contents($stderr); + if ($error) { + + $errorMessage = $error; + $retVal = false; + + fclose($stderr); + fclose($stdout); + fclose($stdin); + + // It is important that you close any pipes before calling + // proc_close in order to avoid a deadlock + proc_close($process); + return $retVal; + + } + } + $output = fopen('php://output', 'w'); foreach($headers as $header) { header($header); @@ -668,11 +719,6 @@ class BookReaderImages stream_copy_to_stream($pipes[1], $output); fclose($output); // okay since tied to special php://output $retVal = true; - } else { - // Got output on stderr - // $$$ make sure we get all stderr - $errorMessage = stream_get_contents($stderr); - $retVal = false; } fclose($stderr); @@ -697,6 +743,7 @@ class BookReaderImages } // Returns true if using a power node + // XXX change to "on red box" - not working for new Xeon function onPowerNode() { exec("lspci | fgrep -c Realtek", $output, $return); if ("0" != $output[0]) { @@ -853,6 +900,103 @@ class BookReaderImages return $pageInfo; } + function getRegionDimensions($sourceDimensions, $regionDimensions) { + // Return region dimensions as { 'x' => xOffset, 'y' => yOffset, 'w' => width, 'h' => height } + // in terms of full resolution image. + // Note: this will clip the returned dimensions to fit within the source image + + $sourceX = 0; + if (array_key_exists('x', $regionDimensions)) { + $sourceX = $this->intAmount($regionDimensions['x'], $sourceDimensions['width']); + } + $sourceX = $this->clamp(0, $sourceDimensions['width'] - 2, $sourceX); // Allow at least one pixel + + $sourceY = 0; + if (array_key_exists('y', $regionDimensions)) { + $sourceY = $this->intAmount($regionDimensions['y'], $sourceDimensions['height']); + } + $sourceY = $this->clamp(0, $sourceDimensions['height'] - 2, $sourceY); // Allow at least one pixel + + $sourceWidth = $sourceDimensions['width'] - $sourceX; + if (array_key_exists('width', $regionDimensions)) { + $sourceWidth = $this->intAmount($regionDimensions['width'], $sourceDimensions['width']); + } + $sourceWidth = $this->clamp(1, max(1, $sourceDimensions['width'] - $sourceX), $sourceWidth); + + $sourceHeight = $sourceDimensions['height'] - $sourceY; + if (array_key_exists('height', $regionDimensions)) { + $sourceHeight = $this->intAmount($regionDimensions['height'], $sourceDimensions['height']); + } + $sourceHeight = $this->clamp(1, max(1, $sourceDimensions['height'] - $sourceY), $sourceHeight); + + return array('x' => $sourceX, 'y' => $sourceY, 'w' => $sourceWidth, 'h' => $sourceHeight); + } + + function getRegionDimensionsAsFloat($sourceDimensions, $regionDimensions) { + // Return region dimensions as { 'x' => xOffset, 'y' => yOffset, 'w' => width, 'h' => height } + // in terms of full resolution image. + // Note: this will clip the returned dimensions to fit within the source image + + $sourceX = 0; + if (array_key_exists('x', $regionDimensions)) { + $sourceX = $this->floatAmount($regionDimensions['x'], $sourceDimensions['width']); + } + $sourceX = $this->clamp(0.0, 1.0, $sourceX); + + $sourceY = 0; + if (array_key_exists('y', $regionDimensions)) { + $sourceY = $this->floatAmount($regionDimensions['y'], $sourceDimensions['height']); + } + $sourceY = $this->clamp(0.0, 1.0, $sourceY); + + $sourceWidth = 1 - $sourceX; + if (array_key_exists('width', $regionDimensions)) { + $sourceWidth = $this->floatAmount($regionDimensions['width'], $sourceDimensions['width']); + } + $sourceWidth = $this->clamp(0.0, 1.0, $sourceWidth); + + $sourceHeight = 1 - $sourceY; + if (array_key_exists('height', $regionDimensions)) { + $sourceHeight = $this->floatAmount($regionDimensions['height'], $sourceDimensions['height']); + } + $sourceHeight = $this->clamp(0.0, 1.0, $sourceHeight); + + return array('x' => $sourceX, 'y' => $sourceY, 'w' => $sourceWidth, 'h' => $sourceHeight); + } + + function intAmount($stringValue, $maximum) { + // Returns integer amount for string like "5" (5 units) or "0.5" (50%) + if (strpos($stringValue, '.') === false) { + // No decimal, assume int + return intval($stringValue); + } + + return floatval($stringValue) * $maximum + 0.5; + } + + function floatAmount($stringValue, $maximum) { + // Returns float amount (0.0 to 1.0) for string like "0.4" (40%) or "4" (40% if max is 10) + if (strpos($stringValue, ".") === false) { + // No decimal, assume int value out of maximum + return floatval($stringValue) / $maximum; + } + + // Given float - just pass through + return floatval($stringValue); + } + + function clamp($minValue, $maxValue, $observedValue) { + if ($observedValue < $minValue) { + return $minValue; + } + + if ($observedValue > $maxValue) { + return $maxValue; + } + + return $observedValue; + } + // Clean up temporary files and resources function cleanup() { foreach($this->tempFiles as $tempFile) {