Skip to content

Commit

Permalink
fix endian and scaling issue affected by some files
Browse files Browse the repository at this point in the history
  • Loading branch information
kapadia committed Jan 21, 2014
1 parent 98e1d3b commit 096f2a6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
24 changes: 14 additions & 10 deletions lib/fits.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fits",
"version": "0.6.4",
"version": "0.6.5",
"author": "Amit Kapadia <[email protected]>",
"description": "Library for reading the FITS astronomical file format",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/fits.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,5 @@ class FITS extends Base
getDataUnit: (index) -> return @getHDU(index).data


FITS.version = '0.6.4'
FITS.version = '0.6.5'
@astro.FITS = FITS
24 changes: 15 additions & 9 deletions src/image.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,30 @@ class Image extends DataUnit
if bitpix > 0
switch bitpix
when 8
arr = new Uint8Array(buffer)
arr = new Uint16Array(arr)
tmp = new Uint8Array(buffer)
tmp = new Uint16Array(tmp)
swapEndian = (value) ->
return value
when 16
arr = new Uint16Array(buffer)
tmp = new Int16Array(buffer)
swapEndian = (value) ->
return (value << 8) | (value >> 8)
return ((value & 0xFF) << 8) | ((value >> 8) & 0xFF)
when 32
arr = new Int32Array(buffer)
tmp = new Int32Array(buffer)
swapEndian = (value) ->
return ((value & 0xFF) << 24) | ((value & 0xFF00) << 8) | ((value >> 8) & 0xFF00) | ((value >> 24) & 0xFF)


# Patch for data unit with BSCALE AND BZERO ...
unless (parseInt(bzero) is bzero and parseInt(bscale) is bscale)
arr = new Float32Array(tmp.length)
else
arr = tmp
while nPixels--
value = arr[nPixels]
value = swapEndian(value)
arr[nPixels] = bzero + bscale * value + 0.5

# Swap endian and recast into typed array (needed to properly handle any overflow)
tmp[nPixels] = swapEndian( tmp[nPixels] )
arr[nPixels] = bzero + bscale * tmp[nPixels]

else
arr = new Uint32Array(buffer)

Expand Down

1 comment on commit 096f2a6

@Merg1255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kapadia You should also update the README of the github page. :)

Please sign in to comment.