Skip to content

Commit

Permalink
Merge pull request #56 from observerly/docs/README
Browse files Browse the repository at this point in the history
docs: amend README.md in workspace root in @observerly/fits
  • Loading branch information
michealroberts authored Jan 10, 2025
2 parents 3d516f4 + 6314f86 commit d29cf71
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The key tenants of the library is to use as JavaScript primatives, where we do n

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.

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.
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.

### Installation

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

```ts
import { FITS, ZScaleInterval } from '@observerly/fits'

// ... load FITS file as above

const image = fits.getImageHDU() // of type Float32Array
const image = fits.getImageHDU() // of type Float32Array (uncorrected):

const canvas = document.createElement('canvas')

canvas.width = fits.getHeader('NAXIS1')
canvas.width = fits.width

canvas.height = fits.getHeader('NAXIS2')
canvas.height = fits.height

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

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

const data = imageData.data
// Compute ZScaleInterval to the image data:
const { vmin, vmax } = ZScaleInterval(image)

// Normalize the data to the [0..255] range
const normalizedData = new Float32Array(resolution)

// Normalize the data to the [0..255] range, e.g., a Uint8ClampedArray:
for (let i = 0; i < resolution; i++) {
normalizedData[i] = ((image[i] - vmin) / (vmax - vmin)) * 255
}

// Convert the Float32Array to a Uint8ClampedArray:
for (let i = 0; i < image.length; i++) {
const value = image[i]
data[i * 4] = value
data[i * 4 + 1] = value
data[i * 4 + 2] = value
data[i * 4 + 3] = 255
normalizedData[i * 4] = value
normalizedData[i * 4 + 1] = value
normalizedData[i * 4 + 2] = value
normalizedData[i * 4 + 3] = 255
}

ctx.putImageData(imageData, 0, 0)
ctx.putImageData(normalizedData, 0, 0)
```

## Miscellany
Expand Down

0 comments on commit d29cf71

Please sign in to comment.