<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>Michael,</p>
<p>It turns out that gdal_translate was using 255.999 since the
origins in 2002 :
<a class="moz-txt-link-freetext" href="https://github.com/OSGeo/gdal/commit/47d859efb25da249b5d62eb8619c36134ae76572#diff-d57b4553312c88a736821448adc155e6797653b0d902bd42f9310572aa71f0b0R215">https://github.com/OSGeo/gdal/commit/47d859efb25da249b5d62eb8619c36134ae76572#diff-d57b4553312c88a736821448adc155e6797653b0d902bd42f9310572aa71f0b0R215</a></p>
<p>I suspect the reasoning was that, if you rounded values down
(floor) and used 255 as the scaleDstMax, and you remapped
[0,65535] to [0,255] for example, then 65534. / (65535. / 255) =
254.996.. would be rounded to 254. So you would only get 255 as
the output for 65535 as the input, which seems "unfair". But GDAL
actually rounds to closest, so if using dstMax = 65535, all input
values in range [65535 - 128, 65535] get scaled to 255, which is
OK to me. Of course that last "bucket" and the first one at 0
(input values in [0,128]) are half-width of the ones in the middle
(input values in [129,129+256] get scaled to 1) but I don't think
we can do much about that (discrete values vs intervals)<br>
</p>
<p>One downside of using dstMax=255.999 is that if your input range
is [0, 255], then -scale was not the identity (254 got transformed
to 253), which is kind of an issue! Hence I've changed dstMax to
be 255 in <a class="moz-txt-link-freetext" href="https://github.com/OSGeo/gdal/pull/8367">https://github.com/OSGeo/gdal/pull/8367</a></p>
<p>I've also noticed during testing a more subtle consistency issue
when if you did for example "gdal_translate
autotest/gcore/data/byte.tif out.asc -scale 0 1 0 1.5 -of
aaigrid", you would get values outside of the [0, 255] range
because the VRT machinery was ignoring the constraints of the
VRTRasterBand data type (Byte), and was only honouring the buffer
data type of the RasterIO() request from the AAIGrid driver
(Int32, always used when translating from source bands having
integer data types, but with the reasoning that if you query a
Byte band then you would get always value in [0,255] range)<br>
</p>
<p>Even<br>
</p>
<div class="moz-cite-prefix">Le 08/09/2023 à 04:20, Michael Sumner a
écrit :<br>
</div>
<blockquote type="cite"
cite="mid:CAAcGz9_FNEHy64LG0RMvn+gd6qyQb9ZGtyNHy1y_dciTdiKX-g@mail.gmail.com">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<div dir="ltr">
<div>Hi, I'm expecting `gdal_translate -scale` to emit values in
the range 0,255 but it seems to be targeting 0,256. (All
works as expected when using explicit src_min src_max dst_min
dst_max).
<div><br>
</div>
<div>To see the output range on a simple example (see code at
link below in case email garbles): </div>
<div><br>
</div>
<div>from osgeo import gdal<br>
ds = gdal.Translate("/vsimem/scl.tif",
"autotest/gcore/data/float32.tif", options = "-scale")<br>
ds.GetRasterBand(1).ComputeRasterMinMax()<br>
</div>
<div>## (0.0, 255.99899291992188)</div>
<div><br>
</div>
<div>
<div>Is that expected, am I missing something? I've tried
variations on the input values and output types. </div>
<div><br>
</div>
</div>
<div><a
href="https://gist.github.com/mdsumner/ee4103d8616b9aa341e82c46b44a8c8c"
moz-do-not-send="true" class="moz-txt-link-freetext">https://gist.github.com/mdsumner/ee4103d8616b9aa341e82c46b44a8c8c</a><br>
</div>
<div><br>
</div>
<div>Cheers, Mike</div>
</div>
<div><br>
</div>
<div><br>
</div>
<span class="gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="gmail_signature"
data-smartmail="gmail_signature">Michael Sumner<br>
Research Software Engineer<br>
Australian Antarctic Division<br>
Hobart, Australia<br>
e-mail: <a href="mailto:mdsumner@gmail.com" target="_blank"
moz-do-not-send="true" class="moz-txt-link-freetext">mdsumner@gmail.com</a></div>
</div>
<br>
<fieldset class="moz-mime-attachment-header"></fieldset>
<pre class="moz-quote-pre" wrap="">_______________________________________________
gdal-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:gdal-dev@lists.osgeo.org">gdal-dev@lists.osgeo.org</a>
<a class="moz-txt-link-freetext" href="https://lists.osgeo.org/mailman/listinfo/gdal-dev">https://lists.osgeo.org/mailman/listinfo/gdal-dev</a>
</pre>
</blockquote>
<pre class="moz-signature" cols="72">--
<a class="moz-txt-link-freetext" href="http://www.spatialys.com">http://www.spatialys.com</a>
My software is free, but my time generally not.</pre>
</body>
</html>