From 19df4cc9264f6938d324f51489b8aacff75eb4d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20P?= Date: Thu, 26 Dec 2024 17:02:33 +0100 Subject: [PATCH] Crazy SVG conversion... --- src/Fits.php | 3 +- src/ImageBlob.php | 77 +++++++++++++++++++++++++++++++++++++---- tests/ImageBlobTest.php | 2 +- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/Fits.php b/src/Fits.php index 368438b..f86de6d 100644 --- a/src/Fits.php +++ b/src/Fits.php @@ -86,8 +86,9 @@ class Fits { $naxis1 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS1')); $naxis2 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS2')); + $naxis3 = (int)trim($this->fitsHeader->getKeywordValue('NAXIS3')); - $blobEnd = $naxis1 * $naxis2; + $blobEnd = $naxis1 * $naxis2 * $naxis3; return substr( $this->contents, diff --git a/src/ImageBlob.php b/src/ImageBlob.php index 73192bd..0d2dfe0 100644 --- a/src/ImageBlob.php +++ b/src/ImageBlob.php @@ -13,6 +13,9 @@ class ImageBlob private FitsHeader $header; public readonly Bitpix $bitpix; public readonly int $dataBits; + public readonly int $width; + public readonly int $height; + public readonly int $naxis3; /** * @throws InvalidBitpixValue @@ -24,18 +27,42 @@ class ImageBlob $this->blob = $blob; $bitpix = (int) $this->header->keyword('BITPIX')->value; $this->bitpix = Bitpix::tryFrom($bitpix) ?? throw new InvalidBitpixValue($bitpix); - $naxis1 = (int) trim($this->header->keyword('NAXIS1')->value); - $naxis2 = (int) trim($this->header->keyword('NAXIS2')->value); - $this->dataBits = abs($this->bitpix->value) * $naxis1 * $naxis2; + $this->width = (int) trim($this->header->keyword('NAXIS1')->value); + $this->height = (int) trim($this->header->keyword('NAXIS2')->value); + $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 - * 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++) { - 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); 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; + $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 .= "\n"; + $x++; + } + $svg .= ''; + + return $svg; + } } diff --git a/tests/ImageBlobTest.php b/tests/ImageBlobTest.php index 314cd1f..3f94ad1 100644 --- a/tests/ImageBlobTest.php +++ b/tests/ImageBlobTest.php @@ -26,7 +26,7 @@ final class ImageBlobTest extends TestCase public function testDataBitsLength(): void { - $this->assertSame($this->imageBlob->dataBits, 16*2448*1669); + $this->assertSame($this->imageBlob->dataBits, 16*2448*1669*3); } }