GeoTIFF / georaster-layer-for-leaflet

Display GeoTIFFs and soon other types of raster on your Leaflet Map
https://geotiff.github.io/georaster-layer-for-leaflet-example/
Apache License 2.0
284 stars 57 forks source link

Support YCbCr #88

Closed DanielJDufour closed 2 years ago

DanielJDufour commented 2 years ago

Related: https://github.com/radiantearth/stac-browser/issues/108#issuecomment-994661736

DanielJDufour commented 2 years ago

I "think" YCbCr could be currently achieved by passing in a pixelValuesToColorFn that converts [Y, Cb, Cr] to "rgba(r, g, b, a)".

But it would be better to have georaster-layer-for-leaflet natively support this by creating this pixelValuesToColorFn if Photometric tag is YCBCR and no pixelValuesToColorFn is supplied.

DanielJDufour commented 2 years ago

@drwelby , could you provide a direct link to a YCbCr geotiff? I'll try to add an example visualizing YCbCr to https://github.com/geotiff/georaster-layer-for-leaflet-example once I got that. Thank you for brining this issue to our attention. I've just been reading up on YCbCr since then and it's fascinating stuff!

drwelby commented 2 years ago

You and @m-mohr are too fast, I started writing out a feature request for this last night!

We've done it by supplying a pixelsValueToColorFn:

function ycbcr2color (values){
    const r = Math.round(values[0] + 1.40200 * (values[2] - 0x80));
    const g = Math.round(values[0] - 0.34414 * (values[1] - 0x80) - 0.71414 * (values[2] - 0x80));
    const b = Math.round(values[0] + 1.77200 * (values[1] - 0x80));
    return `rgb(${r},${g},${b})`;
} 

but had not gone to the point of making the detection automatic, or any other features like adding an option to force a color mode.

Here's a YCbCr COG for testing:

https://maxar-ard-samples.s3.amazonaws.com/v3/australia_vineyards/50/213133231011/2019-10-07/10500100191CD200-visual.tif

DanielJDufour commented 2 years ago

@drwelby , thank you. That is very helpful! I've added an example here: https://geotiff.github.io/georaster-layer-for-leaflet-example/examples/ycbcr.html

DanielJDufour commented 2 years ago

Updating georaster-layer-for-leaflet might be a bit tricky as I don't think the georaster dependency reveals this information. I'll try to see if there's an easy way to access the Photometric Interpretation metadata. Just curious, is photometric interpretation something that is stored in the STAC metadata? @m-mohr

m-mohr commented 2 years ago

Not that I'm aware of... Probably something that would need to be specified in the raster extension: https://github.com/stac-extensions/raster

drwelby commented 2 years ago

We could certainly go in the direction of using STAC metadata for color interpretation if needed.

drwelby commented 2 years ago

I can see PhotometricInterpretation: 6 in the IFDRequests but it seems kind of clunky not to expose the other geotiff tags, such as how the geotransform is extracted.

DanielJDufour commented 2 years ago

I think I'll be able to add this with the following code:

if ((await georaster?._geotiff.getImage()).fileDirectory.PhotometricInterpretation === 6) {
    pixelValuesToColorFn ??= function ycbcr2color (values){
      const r = Math.round(values[0] + 1.40200 * (values[2] - 0x80));
      const g = Math.round(values[0] - 0.34414 * (values[1] - 0x80) - 0.71414 * (values[2] - 0x80));
      const b = Math.round(values[0] + 1.77200 * (values[1] - 0x80));
      return `rgb(${r},${g},${b})`;
    } 
}
DanielJDufour commented 2 years ago

give me a day or two. the release process is rather lengthy with a lot of checks. this change should then percolate up to stac-layer and stac-browser.

drwelby commented 2 years ago

Has this percolated yet? ☕

DanielJDufour commented 2 years ago

not yet, sir. I'll try to get to it this weekend. sorry.

DanielJDufour commented 2 years ago

A new version of GeoRasterLayer for Leaflet has been published that includes the YCbCr support. Thank you!

drwelby commented 2 years ago

Thanks to you too!