diff --git a/internal/images/images.go b/internal/images/images.go index b447c27f2..5f7ed21f2 100644 --- a/internal/images/images.go +++ b/internal/images/images.go @@ -57,7 +57,7 @@ var ErrHEIC = errors.New("HEIC decoding not implemented yet") func init() { image.RegisterFormat("heic", - "????ftypheic????????????????meta????????hdlr????????pict", + "????ftypheic", func(io.Reader) (image.Image, error) { return nil, ErrHEIC }, @@ -364,7 +364,7 @@ var debug, _ = strconv.ParseBool(os.Getenv("CAMLI_DEBUG_IMAGES")) func imageDebug(msg string) { if debug { - log.Print(msg) + log.Print("internal/images: " + msg) } } @@ -396,7 +396,9 @@ func DecodeConfig(r io.Reader) (Config, error) { conf, format, err := image.DecodeConfig(tr) if err != nil { - imageDebug(fmt.Sprintf("Image Decoding failed: %v", err)) + if debug { + log.Printf("internal/images: DecodeConfig failed after reading %d bytes: %v", buf.Len(), err) + } return Config{}, err } c := Config{ diff --git a/internal/images/images_test.go b/internal/images/images_test.go index 0f908c8eb..d8a3aef02 100644 --- a/internal/images/images_test.go +++ b/internal/images/images_test.go @@ -18,6 +18,7 @@ package images import ( "bytes" + "fmt" "image" "image/jpeg" "io" @@ -377,7 +378,7 @@ func TestIssue513(t *testing.T) { } } -func TestHEIF(t *testing.T) { +func TestHEIFToJPEG(t *testing.T) { filename := filepath.Join("testdata", "IMG_8062.HEIC") f, err := os.Open(filename) if err != nil { @@ -402,3 +403,22 @@ func TestHEIF(t *testing.T) { t.Fatalf("wrong width or height, wanted (%d, %d), got (%d, %d)", wantWidth, wantHeight, conf.Width, conf.Height) } } + +func TestDecodeHEIC_WithJPEGInHeader(t *testing.T) { + filename := filepath.Join("testdata", "river-truncated.heic") + f, err := os.Open(filename) + if err != nil { + t.Fatal(err) + } + defer f.Close() + + conf, err := DecodeConfig(f) + if err != nil { + t.Fatal(err) + } + got := fmt.Sprintf("Width:%d Height:%d Format:%v HEIC:%d bytes", conf.Width, conf.Height, conf.Format, len(conf.HEICEXIF)) + want := "Width:6302 Height:3912 Format:heic HEIC:1046 bytes" + if got != want { + t.Errorf("Got:\n %s\nWant:\n %s", got, want) + } +} diff --git a/internal/images/testdata/river-truncated.heic b/internal/images/testdata/river-truncated.heic new file mode 100644 index 000000000..407be0d6d Binary files /dev/null and b/internal/images/testdata/river-truncated.heic differ diff --git a/pkg/index/receive.go b/pkg/index/receive.go index f22606073..b592f0f51 100644 --- a/pkg/index/receive.go +++ b/pkg/index/receive.go @@ -248,6 +248,9 @@ func (ix *Index) ReceiveBlob(ctx context.Context, blobRef blob.Ref, source io.Re } mm, err := ix.populateMutationMap(ctx, fetcher, blobRef, sniffer) + if debugEnv { + log.Printf("index of %v: mm=%v, err=%v", blobRef, mm, err) + } if err != nil { if err != errMissingDep { return blob.SizedRef{}, err @@ -503,6 +506,8 @@ func (ix *Index) populateFile(ctx context.Context, fetcher blob.Fetcher, b *sche } if err := readPrefixOrFile(imageBuf.Bytes, fetcher, b, decodeConfig); err == nil { mm.Set(keyImageSize.Key(blobRef), keyImageSize.Val(fmt.Sprint(conf.Width), fmt.Sprint(conf.Height))) + } else if debugEnv { + log.Printf("index: WARNING: image decodeConfig: %v", err) } exifData := imageBuf.Bytes @@ -517,6 +522,9 @@ func (ix *Index) populateFile(ctx context.Context, fetcher blob.Fetcher, b *sche if err = readPrefixOrFile(exifData, fetcher, b, fileTime); err == nil { times = append(times, ft) + } else if debugEnv { + log.Printf("index: WARNING: image fileTime: %v", err) + } if exifDebug { log.Printf("filename %q exif = %v, %v", b.FileName(), ft, err)