Compare commits
3 Commits
da712b0e77
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 1569873cbb | |||
| 5e4180a4a2 | |||
| 19df4cc926 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@
|
|||||||
composer.lock
|
composer.lock
|
||||||
vendor/
|
vendor/
|
||||||
docs/
|
docs/
|
||||||
|
*.svg
|
||||||
|
|||||||
12
README.md
12
README.md
@@ -11,13 +11,15 @@ Why not? But seriously, this doesn't make any sense, you should never use it in
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
**NOTE: Not added to Packagist yet...**
|
||||||
|
|
||||||
Add the package with Composer:
|
Add the package with Composer:
|
||||||
|
|
||||||
```
|
```
|
||||||
composer require dumbastro/fits-php
|
composer require dumbastro/fits-php
|
||||||
```
|
```
|
||||||
|
|
||||||
then use classes from the `Dumbastro\FitsPhp` namespace. More info in the documentation.
|
then use classes from the `Dumbastro\FitsPhp` namespace.
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
@@ -73,9 +75,9 @@ echo $blob->bitpix->type(); //int32
|
|||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
- [x] Separate the main data table (actual image data) from the header (partly done? Who knows...)
|
- [x] Separate the main data table (actual image data) from the header (partly done? Who knows...)
|
||||||
- [x] Read keywords from the FITS header (but consider values and comments could be more than 80-bytes long)
|
- [x] Read keywords from the FITS header (COMMENT keywords could be buggy)
|
||||||
- [ ] History keywords?
|
- [ ] History keywords?
|
||||||
- [ ] FITS extensions? What to do with NAXIS > 2?
|
- [ ] FITS extensions?
|
||||||
- [ ] Actually display the image in the standard output (with SVG?)
|
- [ ] Actually display the image in the standard output
|
||||||
- [ ] Save the image to PNG and JPG
|
- [ ] Save the image to PNG and/or JPG
|
||||||
- [ ] Manipulate the bits using basic processing algorithms??
|
- [ ] Manipulate the bits using basic processing algorithms??
|
||||||
|
|||||||
@@ -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,10 @@ 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;
|
||||||
|
public readonly bool $isColor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws InvalidBitpixValue
|
* @throws InvalidBitpixValue
|
||||||
@@ -24,18 +28,57 @@ 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;
|
$naxis3 = null;
|
||||||
|
$dataBits = abs($this->bitpix->value) * $this->width * $this->height;
|
||||||
|
|
||||||
|
$naxis = (int) trim($this->header->keyword('NAXIS')->value);
|
||||||
|
|
||||||
|
// Color image (right?)
|
||||||
|
if ($naxis === 3) {
|
||||||
|
$naxis3 = (int) trim($this->header->keyword('NAXIS3')->value);
|
||||||
|
$dataBits *= $naxis3;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->naxis3 = $naxis3 ?? 1;
|
||||||
|
$this->dataBits = $dataBits;
|
||||||
|
|
||||||
|
$this->isColor = $this->naxis3 === 3;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Returns a generator that yields image data
|
* Returns a generator that yields image data
|
||||||
* byte by byte
|
* pixel by pixel
|
||||||
|
*@todo Conversion from 16 to 8-bit?
|
||||||
|
* This won't work with mono images...
|
||||||
*/
|
*/
|
||||||
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 +91,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,18 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testIsColor(): void
|
||||||
|
{
|
||||||
|
$mono = new Dumbastro\FitsPhp\Fits(__DIR__ . '/test_mono.fit');
|
||||||
|
$imageBlob = new Dumbastro\FitsPhp\ImageBlob($mono->header(), $mono->imageBlob);
|
||||||
|
|
||||||
|
$this->assertFalse($imageBlob->isColor);
|
||||||
|
$this->assertTrue($this->imageBlob->isColor);
|
||||||
|
|
||||||
|
$this->assertEquals($imageBlob->naxis3, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
tests/test_m52.fit
Normal file
2
tests/test_m52.fit
Normal file
File diff suppressed because one or more lines are too long
BIN
tests/test_mono.fit
Normal file
BIN
tests/test_mono.fit
Normal file
Binary file not shown.
19607
tests/test_orion_8bit.fit
Normal file
19607
tests/test_orion_8bit.fit
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user