Skip to content

Commit

Permalink
Added color-band normalization for geoTIFF's
Browse files Browse the repository at this point in the history
  • Loading branch information
mtiessen1175 committed Oct 12, 2024
1 parent 66f67e6 commit 644d70d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
48 changes: 40 additions & 8 deletions src/Jobs/TileSingleOverlay.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use FileCache;
use Biigle\Modules\Geo\GeoOverlay;
use File;
use Jcupitt\Vips;
use Jcupitt\Vips\Image as VipsImage;

class TileSingleOverlay extends TileSingleObject
{
Expand Down Expand Up @@ -66,13 +66,45 @@ public function handle()
public function generateTiles($file, $path)
{
$vipsImage = $this->getVipsImage($path);
$sourceSpace = Vips\FFI::vips()->vips_image_guess_interpretation($vipsImage);
// dd($sourceSpace);
$min = $vipsImage->min();
$max = $vipsImage->max();

$vipsImage->colourspace(Vips\Interpretation::RGB16)->dzsave($this->tempPath, [
'layout' => 'zoomify',
'container' => 'fs',
'strip' => true,
]);
if($min < 0 || $max > 255) {
$this->imageNormalization($vipsImage, $min, $max)->dzsave($this->tempPath, [
'layout' => 'zoomify',
'container' => 'fs',
'strip' => true,
]);
} else {
parent::generateTiles($file, $path);
}

}

/**
* Get the vips image instance.
*
* @param string $path
*
* @return \Jcupitt\Vips\Image
*/
protected function getVipsImage($path)
{
return VipsImage::newFromFile($path);
}

/**
* Normalize the image band to 0 to 255
*
* @param \Jcupitt\Vips\Image $vipsImage
* @param float $min minimum value of color-level of the input image
* @param float $max maximum value of color-level of the input image
*
* @return \Jcupitt\Vips\Image
*/
protected function imageNormalization($vipsImage, $min, $max)
{
// band intensity normalization x' = (x - $min * (255 / ($max - $min))
return $vipsImage->subtract($min)->multiply(255 / ($max - $min));
}
}
25 changes: 12 additions & 13 deletions tests/Jobs/TileSingleOverlayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Biigle\Tests\Modules\Geo\GeoOverlayTest;
use Biigle\FileCache\GenericFile;
use File;
use FileCache;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Jcupitt\Vips\Image;
Expand All @@ -20,7 +21,14 @@ public function testGenerateOverlayTiles()
$overlay = GeoOverlayTest::createGeotiffOverlay();

// save fake UploadedFile to geo-overlay storage
$overlayFile = UploadedFile::fake()->create($overlay->name, 20, 'image/tiff');
// $overlayFile = UploadedFile::fake()->create($overlay->name, 20, 'image/tiff');
$overlayFile = new UploadedFile(
__DIR__."/../files/geotiff_standardEPSG2013.tif",
'standardEPSG2013.tif',
'image/tiff',
null,
true
);
$overlay->storeFile($overlayFile);
$this->assertTrue(Storage::disk('geo-overlays')->exists($overlay->path));

Expand All @@ -29,19 +37,10 @@ public function testGenerateOverlayTiles()
$file = new GenericFile("{$disk}://{$overlay->path}");

$targetPath = "{$overlay->id}/{$overlay->id}_tiles";
$job = new TileSingleOverlayStub($overlay, $disk, $targetPath);
$mock = Mockery::mock(Image::class);
$mock->shouldReceive('dzsave')
->once()
->with($job->tempPath, [
'layout' => 'zoomify',
'container' => 'fs',
'strip' => true,
]);
$job = new TileSingleOverlay($overlay, $disk, $targetPath);

$job->mock = $mock;

$job->generateTiles($file, '');
$tempPath = config('geo.tiles.tmp_dir')."/{$overlay->id}";
FileCache::getOnce($file, [$job, 'generateTiles']);
}

public function testUploadOverlayToStorage()
Expand Down

0 comments on commit 644d70d

Please sign in to comment.