I found the problem... One of my co-workers, to read the original TIFF
file, used the same read method you used, but after that here used the
GridCoverageFactory to generate a copy in the first read, im not sure why.
This worked on all TIFF without the Alpha band but with Alpha it generates
the errore above.
Here the code used to read the original TIFF image:

GridCoverageFactory factory = new GridCoverageFactory();
GridCoverage2D gridCopy = factory.create("GridCoverage",
        grid.getRenderedImage().copyData(null), grid.getEnvelope());
// Dispose the grid resources to avoid the lock of the tiff file
ImageUtilities.disposeImage(grid.getRenderedImage());
grid.dispose(false);


Thanks a lot for your help!

Il giorno mer 24 feb 2021 alle ore 12:26 Daniele Romagnoli <
daniele.romagn...@geo-solutions.it> ha scritto:

> Hi Lorenzo,
> how do you get that grid object at the top of your code?
>
> This is the full example I'm using and it didn't raise any issue.
> It's basically your code plus a reading part at the beginning and a
> writing part at the end to check the result.
>
> public static void main(String[] args) throws IOException {
>
>     GeoTiffReader reader = new GeoTiffReader(new 
> File("D:/Download/ACQUE_SOTTERRANEE.tif"));
>     GridCoverage2D grid = reader.read(null);
>
>     ByteArrayOutputStream baos = new ByteArrayOutputStream();
>     GeoTiffWriter writer = new GeoTiffWriter(baos, null);
>     writer.write(grid, null);
>     writer.dispose();
>     byte[] bytes = baos.toByteArray();
>     baos.flush();
>     baos.close();
>     ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
>
>     try {
>         Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, 
> Boolean.TRUE);
>         reader = new GeoTiffReader(bais, hints);
>         grid = reader.read(null);
>         bais.close();
>
>         writer = new GeoTiffWriter(new File("d:/download/outputresult.tif"), 
> null);
>         writer.write(grid, null);
>         writer.dispose();
>
>
>     } catch (IllegalArgumentException e) {
>         throw new IOException("Error on deserialization on tile");
>     }
> }
>
> Please, let us know.
> Cheers,
> Daniele
>
>
> On Wed, Feb 24, 2021 at 11:21 AM Lorenzo Di Giacomo <loridi...@gmail.com>
> wrote:
>
>> Hi Daniele, im currently using 23.1 but i also tried with 23.4 and i get
>> the same result, seems like the alpha band can't be handled because i tried
>> to remove it with GDAL and everything works fine.
>>
>> Il giorno mer 24 feb 2021 alle ore 09:43 Daniele Romagnoli <
>> daniele.romagn...@geo-solutions.it> ha scritto:
>>
>>> Hi Lorenzo,
>>> which GeoTools version are you using?
>>>
>>> I have just quickly tried with GeoTools master your very same code also
>>> adding a final piece of code doing a rewrite on disk:
>>>
>>> writer = new GeoTiffWriter(new File("d:/download/writeback.tif"), null);
>>> writer.write(grid, null);
>>> writer.dispose();
>>>
>>> and I didn't see any error and the image looks as the input one.
>>>
>>>
>>> Please,let me know.
>>> Regards,
>>> Daniele
>>>
>>>
>>>
>>> On Mon, Feb 22, 2021 at 9:36 PM Lorenzo Di Giacomo <loridi...@gmail.com>
>>> wrote:
>>>
>>>> Hi, i have a problem that i dont understand if GEOTOOLS handles (maybe
>>>> with some HINTS), basically i read a TIFF image then i serialized it :
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> *public static byte[] gridToBytes(GridCoverage2D grid) throws
>>>> IOException { ByteArrayOutputStream baos = new
>>>> ByteArrayOutputStream(); GeoTiffWriter writer = new GeoTiffWriter(baos,
>>>> null); writer.write(grid, null); writer.dispose(); byte[] bytes =
>>>> baos.toByteArray(); baos.flush(); baos.close(); return bytes;}*
>>>>
>>>> Then i deserialize it with:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> *    public static GridCoverage2D bytesToGrid(byte[] bytes) throws
>>>> IOException {        ByteArrayInputStream bais = new
>>>> ByteArrayInputStream(bytes);        GeoTiffReader reader = null;        try
>>>> {            Hints hints = new
>>>> Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
>>>> reader = new GeoTiffReader(bais, hints);            GridCoverage2D grid =
>>>> reader.read(null);            bais.close();            return grid;
>>>> } catch (IllegalArgumentException e) {            throw new
>>>> IOException("Error on deserialization on tile");        }    }*
>>>>
>>>> It works fine with some images, but i found some tiff (i have 150 of
>>>> those, uploaded one for you if you wanna try:
>>>> https://mega.nz/file/qJlEyTIQ#jgCE5pwfD5Y_AiUj2hSLTvTIS0lZ4CSm5D0Vb0eTk7U
>>>> )
>>>> that gaves this error on deserialization:
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> *Caused by: java.lang.IllegalArgumentException: bandOffsets.length is
>>>> wrong! at
>>>> javax.imageio.ImageTypeSpecifier$Interleaved.<init>(ImageTypeSpecifier.java:286)
>>>> at
>>>> javax.imageio.ImageTypeSpecifier.createInterleaved(ImageTypeSpecifier.java:399)
>>>> at
>>>> it.geosolutions.imageio.plugins.tiff.TIFFDecompressor.getRawImageTypeSpecifier(TIFFDecompressor.java:1027)
>>>> at
>>>> it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader.getImageTypes(TIFFImageReader.java:1359)
>>>> at
>>>> org.geotools.coverage.grid.io.AbstractGridCoverage2DReader.setLayout(AbstractGridCoverage2DReader.java:1167)
>>>> at 
>>>> org.geotools.gce.geotiff.GeoTiffReader.getHRInfo(GeoTiffReader.java:342)*
>>>> And, using gdal i get:
>>>>
>>>> *Warning 1: TIFFReadDirectory:Sum of Photometric type-related color
>>>> channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color
>>>> channels as ExtraSamples.*
>>>> GDAL handle it putting this band (should bhe the 4th band, alpha
>>>> channel) as ExtraSamples.
>>>> How can I walk-around with GeoTools to read the image back? Are there
>>>> any HINTS that i could use?
>>>>
>>>> Thanks
>>>>
>>>> _______________________________________________
>>>> GeoTools-Devel mailing list
>>>> GeoTools-Devel@lists.sourceforge.net
>>>> https://lists.sourceforge.net/lists/listinfo/geotools-devel
>>>>
>>>
>>>
>>> --
>>> Regards,
>>> Daniele Romagnoli
>>> ==
>>> GeoServer Professional Services from the experts! Visit
>>> http://goo.gl/it488V for more information.
>>> ==
>>>
>>> Ing. Daniele Romagnoli
>>> Senior Software Engineer
>>>
>>> GeoSolutions S.A.S.
>>> Via di Montramito 3/A
>>> 55054  Massarosa (LU)
>>> Italy
>>> phone: +39 0584 962313
>>> fax:      +39 0584 1660272
>>>
>>> http://www.geo-solutions.it
>>> http://twitter.com/geosolutions_it
>>>
>>> -------------------------------------------------------
>>>
>>> Con riferimento alla normativa sul trattamento dei dati personali (Reg.
>>> UE 2016/679 - Regolamento generale sulla protezione dei dati “GDPR”), si
>>> precisa che ogni circostanza inerente alla presente email (il suo
>>> contenuto, gli eventuali allegati, etc.) è un dato la cui conoscenza è
>>> riservata al/i solo/i destinatario/i indicati dallo scrivente. Se il
>>> messaggio Le è giunto per errore, è tenuta/o a cancellarlo, ogni altra
>>> operazione è illecita. Le sarei comunque grato se potesse darmene notizia.
>>>
>>> This email is intended only for the person or entity to which it is
>>> addressed and may contain information that is privileged, confidential or
>>> otherwise protected from disclosure. We remind that - as provided by
>>> European Regulation 2016/679 “GDPR” - copying, dissemination or use of this
>>> e-mail or the information herein by anyone other than the intended
>>> recipient is prohibited. If you have received this email by mistake, please
>>> notify us immediately by telephone or e-mail.
>>>
>>
>
> --
> Regards,
> Daniele Romagnoli
> ==
> GeoServer Professional Services from the experts! Visit
> http://goo.gl/it488V for more information.
> ==
>
> Ing. Daniele Romagnoli
> Senior Software Engineer
>
> GeoSolutions S.A.S.
> Via di Montramito 3/A
> 55054  Massarosa (LU)
> Italy
> phone: +39 0584 962313
> fax:      +39 0584 1660272
>
> http://www.geo-solutions.it
> http://twitter.com/geosolutions_it
>
> -------------------------------------------------------
>
> Con riferimento alla normativa sul trattamento dei dati personali (Reg. UE
> 2016/679 - Regolamento generale sulla protezione dei dati “GDPR”), si
> precisa che ogni circostanza inerente alla presente email (il suo
> contenuto, gli eventuali allegati, etc.) è un dato la cui conoscenza è
> riservata al/i solo/i destinatario/i indicati dallo scrivente. Se il
> messaggio Le è giunto per errore, è tenuta/o a cancellarlo, ogni altra
> operazione è illecita. Le sarei comunque grato se potesse darmene notizia.
>
> This email is intended only for the person or entity to which it is
> addressed and may contain information that is privileged, confidential or
> otherwise protected from disclosure. We remind that - as provided by
> European Regulation 2016/679 “GDPR” - copying, dissemination or use of this
> e-mail or the information herein by anyone other than the intended
> recipient is prohibited. If you have received this email by mistake, please
> notify us immediately by telephone or e-mail.
>
_______________________________________________
GeoTools-Devel mailing list
GeoTools-Devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-devel

Reply via email to