Skip to content

Commit

Permalink
feat: add astrotiff.Encode support to getFITSAsTIFF utility in @obser…
Browse files Browse the repository at this point in the history
…verly/birpc

feat: add astrotiff.Encode support to getFITSAsTIFF utility in @observerly/birpc
  • Loading branch information
michealroberts committed Oct 17, 2024
1 parent 483f29c commit e31bba5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
firebase.google.com/go/v4 v4.14.1
github.com/google/uuid v1.6.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/observerly/iris v0.34.0
github.com/observerly/iris v0.35.0
github.com/rs/zerolog v1.33.0
golang.org/x/image v0.20.0
golang.org/x/net v0.29.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ github.com/microsoft/go-mssqldb v1.7.2 h1:CHkFJiObW7ItKTJfHo1QX7QBBD1iV+mn1eOyRP
github.com/microsoft/go-mssqldb v1.7.2/go.mod h1:kOvZKUdrhhFQmxLZqbwUV0rHkNkZpthMITIb2Ko1IoA=
github.com/modocache/gover v0.0.0-20171022184752-b58185e213c5/go.mod h1:caMODM3PzxT8aQXRPkAt8xlV/e7d7w8GM5g0fa5F0D8=
github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/observerly/iris v0.34.0 h1:k5KhTAg22YyenppTVYgJS/OC5HC3bjzQcMCg+2m39Po=
github.com/observerly/iris v0.34.0/go.mod h1:YKEXtV4s79ytEMsjfgHl5oQAyPIpRcDUr5mZwfSItPc=
github.com/observerly/iris v0.35.0 h1:SBzIeD4vGk955GfhQGkCdSGX+S1H4Wn00UD045nzg9k=
github.com/observerly/iris v0.35.0/go.mod h1:umxMm/MDEsIivLF71qnhi3PCMt6NwSlusCb2u8Xx8DQ=
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c h1:+mdjkGKdHQG3305AYmdv1U2eRNDiU2ErMBj1gwrq8eQ=
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c/go.mod h1:7rwL4CYBLnjLxUqIJNnCWiEdr3bn6IUYi15bNlnbCCU=
Expand Down
41 changes: 38 additions & 3 deletions service/storage/tiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

cloud "cloud.google.com/go/storage"
"connectrpc.com/connect"
"github.com/observerly/iris/pkg/astrotiff"
metadata "github.com/observerly/iris/pkg/ifd"
"github.com/observerly/iris/pkg/image"
"github.com/rs/zerolog/log"
"golang.org/x/image/tiff"
Expand Down Expand Up @@ -75,11 +77,44 @@ func (s *server) getFITSAsTIFF(ctx context.Context, req *connect.Request[pb.GetF
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to get image as 16-bit grayscale from FITS: %w", err))
}

// Create a new buffer to store the object data:
// Create a new buffer to store the header data:
headerb := new(bytes.Buffer)

// Write the FITS header to the buffer:
headerb, err = fit.Header.WriteToBuffer(headerb)

if err != nil {
s.Logger.Error().Err(err).Msg("Failed to write header to buffer")
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to write header to buffer: %w", err))
}

// Convert the FITS header to a series of uint32 values:
var description []uint32
for _, b := range fit.Header.AddLineFeedCharacteToHeaderRow(headerb.Bytes(), "\n") {
description = append(description, uint32(b))
}

// Create an Image File Directory (IFD) for the TIFF:
ifd := []metadata.IFDEntry{
{
Tag: metadata.TagTypeImageDescription,
DataType: metadata.DataTypeASCII,
Data: description,
},
{
Tag: metadata.TagTypeOrientation,
DataType: metadata.DataTypeShort,
Data: []uint32{1},
},
}

// Create a new buffer to store the image data:
tiffb := new(bytes.Buffer)

// Encode the image as a TIFF:
err = tiff.Encode(tiffb, img, nil)
// Encode the image as a TIFF with Deflate compression and horizontal predictor:
err = astrotiff.Encode(tiffb, img, &tiff.Options{
Compression: tiff.Deflate,
}, ifd)

if err != nil {
s.Logger.Error().Err(err).Msg("Failed to encode image as TIFF")
Expand Down

0 comments on commit e31bba5

Please sign in to comment.