diff --git a/go.mod b/go.mod index 63358c4..60916cc 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d46b013..b611494 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/service/storage/tiff.go b/service/storage/tiff.go index 1b13457..6a6eec7 100644 --- a/service/storage/tiff.go +++ b/service/storage/tiff.go @@ -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" @@ -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")