Skip to content

Commit d29cf71

Browse files
Merge pull request #56 from observerly/docs/README
docs: amend README.md in workspace root in @observerly/fits
2 parents 3d516f4 + 6314f86 commit d29cf71

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The key tenants of the library is to use as JavaScript primatives, where we do n
88

99
The final output image is a 2D array of pixel values, which can be used to render the image in a canvas, or any other image rendering library of choice. The final output image pixel values is a ZScaleInterval of the original pixel values, returned as a Float32Array.
1010

11-
The library makes no opinion of what you can do with the resulting image data, and is designed to be as flexible as possible to allow the user to use the data in any way they see fit, whether that be rendering the image, or processing the data in some other way. However, it is more than possible to convert the values to an RGBA Uint8ClampedArray for use with the HTMLCanvasElement, the [Canvas_API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) or any other image rendering library of choice.
11+
The library makes no opinion of what you can do with the resulting image data, and is designed to be as flexible as possible to allow the user to use the data in any way they see fit, whether that be rendering the image, or processing the data in some other way. However, it is more than possible to convert the values to an RGBA Uint8ClampedArray for use with the HTMLCanvasElement, the [Web Canvas API](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) or any other image rendering library of choice.
1212

1313
### Installation
1414

@@ -85,31 +85,43 @@ const image = fits.getImageHDU() // of type Float32Array
8585
#### 2D CanvasHTMLElement Rendering
8686

8787
```ts
88+
import { FITS, ZScaleInterval } from '@observerly/fits'
89+
8890
// ... load FITS file as above
8991

90-
const image = fits.getImageHDU() // of type Float32Array
92+
const image = fits.getImageHDU() // of type Float32Array (uncorrected):
9193

9294
const canvas = document.createElement('canvas')
9395

94-
canvas.width = fits.getHeader('NAXIS1')
96+
canvas.width = fits.width
9597

96-
canvas.height = fits.getHeader('NAXIS2')
98+
canvas.height = fits.height
9799

98100
const ctx = canvas.getContext('2d')
99101

100102
const imageData = ctx.createImageData(canvas.width, canvas.height)
101103

102-
const data = imageData.data
104+
// Compute ZScaleInterval to the image data:
105+
const { vmin, vmax } = ZScaleInterval(image)
106+
107+
// Normalize the data to the [0..255] range
108+
const normalizedData = new Float32Array(resolution)
109+
110+
// Normalize the data to the [0..255] range, e.g., a Uint8ClampedArray:
111+
for (let i = 0; i < resolution; i++) {
112+
normalizedData[i] = ((image[i] - vmin) / (vmax - vmin)) * 255
113+
}
103114

115+
// Convert the Float32Array to a Uint8ClampedArray:
104116
for (let i = 0; i < image.length; i++) {
105117
const value = image[i]
106-
data[i * 4] = value
107-
data[i * 4 + 1] = value
108-
data[i * 4 + 2] = value
109-
data[i * 4 + 3] = 255
118+
normalizedData[i * 4] = value
119+
normalizedData[i * 4 + 1] = value
120+
normalizedData[i * 4 + 2] = value
121+
normalizedData[i * 4 + 3] = 255
110122
}
111123

112-
ctx.putImageData(imageData, 0, 0)
124+
ctx.putImageData(normalizedData, 0, 0)
113125
```
114126

115127
## Miscellany

0 commit comments

Comments
 (0)