Crazy SVG conversion...
This commit is contained in:
parent
da712b0e77
commit
19df4cc926
@ -86,8 +86,9 @@ class Fits
|
|||||||
{
|
{
|
||||||
$naxis1 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS1'));
|
$naxis1 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS1'));
|
||||||
$naxis2 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS2'));
|
$naxis2 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS2'));
|
||||||
|
$naxis3 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS3'));
|
||||||
|
|
||||||
$blobEnd = $naxis1 * $naxis2;
|
$blobEnd = $naxis1 * $naxis2 * $naxis3;
|
||||||
|
|
||||||
return substr(
|
return substr(
|
||||||
$this->contents,
|
$this->contents,
|
||||||
|
@ -13,6 +13,9 @@ class ImageBlob
|
|||||||
private FitsHeader $header;
|
private FitsHeader $header;
|
||||||
public readonly Bitpix $bitpix;
|
public readonly Bitpix $bitpix;
|
||||||
public readonly int $dataBits;
|
public readonly int $dataBits;
|
||||||
|
public readonly int $width;
|
||||||
|
public readonly int $height;
|
||||||
|
public readonly int $naxis3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws InvalidBitpixValue
|
* @throws InvalidBitpixValue
|
||||||
@ -24,18 +27,42 @@ class ImageBlob
|
|||||||
$this->blob = $blob;
|
$this->blob = $blob;
|
||||||
$bitpix = (int) $this->header->keyword('BITPIX')->value;
|
$bitpix = (int) $this->header->keyword('BITPIX')->value;
|
||||||
$this->bitpix = Bitpix::tryFrom($bitpix) ?? throw new InvalidBitpixValue($bitpix);
|
$this->bitpix = Bitpix::tryFrom($bitpix) ?? throw new InvalidBitpixValue($bitpix);
|
||||||
$naxis1 = (int) trim($this->header->keyword('NAXIS1')->value);
|
$this->width = (int) trim($this->header->keyword('NAXIS1')->value);
|
||||||
$naxis2 = (int) trim($this->header->keyword('NAXIS2')->value);
|
$this->height = (int) trim($this->header->keyword('NAXIS2')->value);
|
||||||
$this->dataBits = abs($this->bitpix->value) * $naxis1 * $naxis2;
|
$this->naxis3 = (int) trim($this->header->keyword('NAXIS3')->value);
|
||||||
|
$this->dataBits = abs($this->bitpix->value) * $this->width * $this->height * $this->naxis3;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Returns a generator that yields image data
|
* Returns a generator that yields image data
|
||||||
* byte by byte
|
* pixel by pixel
|
||||||
*/
|
*/
|
||||||
public function dataBytes(): \Generator
|
public function pixels(): \Generator
|
||||||
{
|
{
|
||||||
|
$n = 0;
|
||||||
|
$pixel = [];
|
||||||
|
$pixBytes = abs($this->bitpix->value) / 8;
|
||||||
|
|
||||||
for ($i = 0; $i < strlen($this->blob); $i++) {
|
for ($i = 0; $i < strlen($this->blob); $i++) {
|
||||||
yield $this->blob[$i];
|
$value = unpack(
|
||||||
|
format: 'C',
|
||||||
|
string: $this->blob[$i]
|
||||||
|
)[1];
|
||||||
|
|
||||||
|
//$value = floor($value / 256);
|
||||||
|
|
||||||
|
if ($i + $pixBytes <= strlen($this->blob) - 1) {
|
||||||
|
$value += unpack(
|
||||||
|
format: 'C',
|
||||||
|
string: $this->blob[$i + $pixBytes - 1]
|
||||||
|
)[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
$pixel[$n] = $value;
|
||||||
|
$n++;
|
||||||
|
if ($n === $this->naxis3) {
|
||||||
|
$n = 0;
|
||||||
|
yield $pixel;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -48,4 +75,42 @@ class ImageBlob
|
|||||||
$gdImg = imagecreatefromstring($this->blob);
|
$gdImg = imagecreatefromstring($this->blob);
|
||||||
return imagepng($gdImg, quality: $quality);
|
return imagepng($gdImg, quality: $quality);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to SVG for display
|
||||||
|
* @todo This assumes RGB and produces a gigantic file...
|
||||||
|
* Note: pixels are treated as SVG rectangles, RGB values are
|
||||||
|
extracted from bit values... (?!)
|
||||||
|
*/
|
||||||
|
public function toSVG(): string
|
||||||
|
{
|
||||||
|
$svg = <<<SVG
|
||||||
|
<svg version="1.1"
|
||||||
|
width="{$this->width}"
|
||||||
|
height="{$this->height}"
|
||||||
|
xmlns="http://www.w3.org/2000/svg">
|
||||||
|
|
||||||
|
SVG;
|
||||||
|
$x = 1;
|
||||||
|
$y = 1;
|
||||||
|
$cols = 1;
|
||||||
|
// Build SVG using 1x1 rectangles
|
||||||
|
foreach ($this->pixels() as $k => $pixel) {
|
||||||
|
// A pixel is a 3-element array if the image is RGB
|
||||||
|
[$r, $g, $b] = $pixel;
|
||||||
|
$r = $r / 2;
|
||||||
|
$g = $g / 2;
|
||||||
|
$b = $b / 2;
|
||||||
|
// Change row after reaching image width
|
||||||
|
if ($x === $this->width + 1) {
|
||||||
|
$y++;
|
||||||
|
$x = 1;
|
||||||
|
}
|
||||||
|
$svg .= "<rect x=\"$x\" y=\"$y\" width=\"1\" height=\"1\" fill=\"rgb($r, $g, $b)\" />\n";
|
||||||
|
$x++;
|
||||||
|
}
|
||||||
|
$svg .= '</svg>';
|
||||||
|
|
||||||
|
return $svg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,7 +26,7 @@ final class ImageBlobTest extends TestCase
|
|||||||
|
|
||||||
public function testDataBitsLength(): void
|
public function testDataBitsLength(): void
|
||||||
{
|
{
|
||||||
$this->assertSame($this->imageBlob->dataBits, 16*2448*1669);
|
$this->assertSame($this->imageBlob->dataBits, 16*2448*1669*3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user