webp-sh / webp_server_go

Go version of WebP Server. A tool that will serve your JPG/PNG/BMP/SVGs as WebP/AVIF format with compression, on-the-fly.
https://docs.webp.sh
GNU General Public License v3.0
1.79k stars 174 forks source link

NEF image size is wrong and extra parameters are invalid #281

Closed llanc closed 10 months ago

llanc commented 11 months ago

Describe the bug When converting NEF (NIKON ELECTRIC FILM) format, the resulting webp loses its original pixel ratio. The QUALITY parameter and extra parameters is invalid. Not sure if other RAW formats are ok.

To Reproduce Just use a NEF image.

Expected behavior NEF images or other RAW images can work fine.

Screenshots and logs webp image image NEF image image

Environment (please complete the following information):

Additional context none

n0vad3v commented 11 months ago

From our docs:

Currently supported image format: JPEG, PNG, BMP, GIF, SVG, HEIC

NEF format is currently NOT supported, and the problem seems libwebp related, I've tried using cwebp like:

cwebp DSC_0001.NEF -o output.webp
TIFFReadDirectory: Warning, Unknown field with tag 36867 (0x9003) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 37398 (0x9216) encountered.
Saving file 'output.webp'
File:      DSC_0001.NEF
Dimension: 160 x 120
Output:    4310 bytes Y-U-V-All-PSNR 37.27 38.71 39.62   37.81 dB
           (1.80 bpp)
block count:  intra4:         64  (80.00%)
              intra16:        16  (20.00%)
              skipped:         6  (7.50%)
bytes used:  header:            142  (3.3%)
             mode-partition:    335  (7.8%)
 Residuals bytes  |segment 1|segment 2|segment 3|segment 4|  total
    macroblocks:  |      20%|      21%|      38%|      21%|      80
      quantizer:  |      36 |      30 |      22 |      16 |
   filter level:  |      11 |       7 |       4 |       2 |
du -ch DSC_0001.NEF
12M DSC_0001.NEF
12M total

du -ch output.webp 
8.0K    output.webp
8.0K    total

This gives similar results from what you've posted.

llanc commented 11 months ago

How about converting NEF (RAW) images to JPG first and then to WEBP?

n0vad3v commented 11 months ago

How about converting NEF (RAW) images to JPG first and then to WEBP?

No, it's not that easy, NEF images are little bit hard to deal with, for example, if you use file to check for a NEF image, you'll find it has no standard dimension data:

file 1.NEF 
1.NEF: TIFF image data, big-endian, direntries=25, height=0, bps=0, compression=none, PhotometricInterpretation=RGB, manufacturer=NIKON CORPORATION, model=NIKON XXXXX, orientation=upper-left, width=0

Note height=0 and width=0

So currently we suggest you to convert those NEF images to JPEG or some other supported image formats before using WebP Server Go to render them.

BTW, I've tried https://github.com/rwcarlsen/goexif for extracting info from the above NEF image, and has incorrect results:

package main

import (
    "fmt"
    "os"

    "github.com/rwcarlsen/goexif/exif"
    "github.com/rwcarlsen/goexif/mknote"
)

func main() {

    nefFile, err := os.Open("1.NEF")
    if err != nil {
        fmt.Println(err)
    }
    defer nefFile.Close()
    exif.RegisterParsers(mknote.All...)

    exifData, err := exif.Decode(nefFile)
    if err != nil {
        fmt.Println(err)
    }

    camModel, _ := exifData.Get(exif.Model) // normally, don't ignore errors!
    fmt.Println(camModel.StringVal())

    XResolution, _ := exifData.Get(exif.XResolution)
    YResolution, _ := exifData.Get(exif.YResolution)
    ResolutionUnit, _ := exifData.Get(exif.ResolutionUnit)
    PixelXDimension, _ := exifData.Get(exif.PixelXDimension)
    PixelYDmension, _ := exifData.Get(exif.PixelYDimension)
    ImageWidth, _ := exifData.Get(exif.ImageWidth)
    ImageLength, _ := exifData.Get(exif.ImageLength)

    fmt.Println(XResolution)
    fmt.Println(YResolution)
    fmt.Println(ResolutionUnit)
    fmt.Println(PixelXDimension)
    fmt.Println(PixelYDmension)
    fmt.Println(ImageWidth)
    fmt.Println(ImageLength)
}

The output from above program is:

NIKON XXXXX <nil>
"300/1"
"300/1"
2
<nil>
<nil>
160
120

While it's correct dimension is 4608 x 3072.

Screenshot from 2023-10-14 19-50-57

llanc commented 11 months ago

I saw a library from https://github.com/gavinwade12/nef2jpeg that can convert NEF to JPG normally: https://github.com/jeremytorres/rawparser

My test results: NEF image

JPG image image

llanc commented 10 months ago

284