001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.itk;
020
021
022/**
023 * ITK library support.<br>
024 * Will be externalized in future as a plugin. 
025 * 
026 * @author Stephane
027 */
028public class ItkUtil
029{
030//    public static DataType getDataTypeFromPixelID(int value)
031//    {
032//        return getDataTypeFromPixelID(PixelIDValueEnum.swigToEnum(value));
033//    }
034//
035//    public static DataType getDataTypeFromPixelID(PixelIDValueEnum value)
036//    {
037//        if (value == PixelIDValueEnum.sitkInt8)
038//            return DataType.BYTE;
039//        if (value == PixelIDValueEnum.sitkUInt8)
040//            return DataType.UBYTE;
041//        if (value == PixelIDValueEnum.sitkInt16)
042//            return DataType.SHORT;
043//        if (value == PixelIDValueEnum.sitkUInt16)
044//            return DataType.USHORT;
045//        if (value == PixelIDValueEnum.sitkInt32)
046//            return DataType.INT;
047//        if (value == PixelIDValueEnum.sitkUInt32)
048//            return DataType.UINT;
049//        if (value == PixelIDValueEnum.sitkFloat32)
050//            return DataType.FLOAT;
051//        if (value == PixelIDValueEnum.sitkFloat64)
052//            return DataType.DOUBLE;
053//
054//        return DataType.UNDEFINED;
055//    }
056//
057//    public static boolean isSignedPixelID(int value)
058//    {
059//        return isSignedPixelID(PixelIDValueEnum.swigToEnum(value));
060//    }
061//
062//    public static boolean isSignedPixelID(PixelIDValueEnum value)
063//    {
064//        return (value == PixelIDValueEnum.sitkInt8) || (value == PixelIDValueEnum.sitkInt16)
065//                || (value == PixelIDValueEnum.sitkInt32) || (value == PixelIDValueEnum.sitkFloat32)
066//                || (value == PixelIDValueEnum.sitkFloat64);
067//    }
068//
069//    public static PixelIDValueEnum getPixelIDFromDataType(DataType dataType)
070//    {
071//        switch (dataType)
072//        {
073//            case BYTE:
074//                return PixelIDValueEnum.sitkInt8;
075//            case UBYTE:
076//                return PixelIDValueEnum.sitkUInt8;
077//            case SHORT:
078//                return PixelIDValueEnum.sitkInt16;
079//            case USHORT:
080//                return PixelIDValueEnum.sitkUInt16;
081//            case INT:
082//                return PixelIDValueEnum.sitkInt32;
083//            case UINT:
084//                return PixelIDValueEnum.sitkUInt32;
085//            case FLOAT:
086//                return PixelIDValueEnum.sitkFloat32;
087//            case DOUBLE:
088//                return PixelIDValueEnum.sitkFloat64;
089//        }
090//
091//        return PixelIDValueEnum.sitkUnknown;
092//    }
093//
094//    public static int getPixelIDAsIntFromDataType(DataType dataType)
095//    {
096//        return getPixelIDFromDataType(dataType).swigValue();
097//    }
098//
099//    /**
100//     * Convert the specified ICY sequence to ITK image.<br>
101//     * ITK images are only 3D [XYZ] so multiple channels images
102//     * or multiple frames are not supported.
103//     */
104//    public static Image getItkIImage(Sequence sequence)
105//    {
106//        final int sizeX = sequence.getSizeX();
107//        final int sizeY = sequence.getSizeY();
108//        final int sizeZ = sequence.getSizeZ();
109//        final DataType dataType = sequence.getDataType_();
110//
111//        final Image result = new Image(sizeX, sizeY, sizeZ, getPixelIDFromDataType(dataType));
112//        final VectorUInt32 idx = new VectorUInt32(3);
113//
114//        switch (dataType)
115//        {
116//            case BYTE:
117//                for (int z = 0; z < sizeZ; z++)
118//                {
119//                    final byte[] data = sequence.getDataXYAsByte(0, z, 0);
120//                    int offset = 0;
121//                    // set index
122//                    idx.set(2, z);
123//
124//                    for (int y = 0; y < sizeY; y++)
125//                    {
126//                        // set index
127//                        idx.set(1, y);
128//
129//                        for (int x = 0; x < sizeX; x++, offset++)
130//                        {
131//                            // set index
132//                            idx.set(0, x);
133//                            // set pixel
134//                            result.setPixelAsInt8(idx, data[offset]);
135//                        }
136//                    }
137//                }
138//                break;
139//
140//            case UBYTE:
141//                for (int z = 0; z < sizeZ; z++)
142//                {
143//                    final byte[] data = sequence.getDataXYAsByte(0, z, 0);
144//                    int offset = 0;
145//                    // set index
146//                    idx.set(2, z);
147//
148//                    for (int y = 0; y < sizeY; y++)
149//                    {
150//                        // set index
151//                        idx.set(1, y);
152//
153//                        for (int x = 0; x < sizeX; x++, offset++)
154//                        {
155//                            // set index
156//                            idx.set(0, x);
157//                            // set pixel
158//                            result.setPixelAsUInt8(idx, data[offset]);
159//                        }
160//                    }
161//                }
162//                break;
163//
164//            case SHORT:
165//                for (int z = 0; z < sizeZ; z++)
166//                {
167//                    final short[] data = sequence.getDataXYAsShort(0, z, 0);
168//                    int offset = 0;
169//                    // set index
170//                    idx.set(2, z);
171//
172//                    for (int y = 0; y < sizeY; y++)
173//                    {
174//                        // set index
175//                        idx.set(1, y);
176//
177//                        for (int x = 0; x < sizeX; x++, offset++)
178//                        {
179//                            // set index
180//                            idx.set(0, x);
181//                            // set pixel
182//                            result.setPixelAsInt16(idx, data[offset]);
183//                        }
184//                    }
185//                }
186//                break;
187//
188//            case USHORT:
189//                for (int z = 0; z < sizeZ; z++)
190//                {
191//                    final short[] data = sequence.getDataXYAsShort(0, z, 0);
192//                    int offset = 0;
193//                    // set index
194//                    idx.set(2, z);
195//
196//                    for (int y = 0; y < sizeY; y++)
197//                    {
198//                        // set index
199//                        idx.set(1, y);
200//
201//                        for (int x = 0; x < sizeX; x++, offset++)
202//                        {
203//                            // set index
204//                            idx.set(0, x);
205//                            // set pixel
206//                            result.setPixelAsUInt16(idx, data[offset]);
207//                        }
208//                    }
209//                }
210//                break;
211//
212//            case INT:
213//                for (int z = 0; z < sizeZ; z++)
214//                {
215//                    final int[] data = sequence.getDataXYAsInt(0, z, 0);
216//                    int offset = 0;
217//                    // set index
218//                    idx.set(2, z);
219//
220//                    for (int y = 0; y < sizeY; y++)
221//                    {
222//                        // set index
223//                        idx.set(1, y);
224//
225//                        for (int x = 0; x < sizeX; x++, offset++)
226//                        {
227//                            // set index
228//                            idx.set(0, x);
229//                            // set pixel
230//                            result.setPixelAsInt32(idx, data[offset]);
231//                        }
232//                    }
233//                }
234//                break;
235//
236//            case UINT:
237//                for (int z = 0; z < sizeZ; z++)
238//                {
239//                    final int[] data = sequence.getDataXYAsInt(0, z, 0);
240//                    int offset = 0;
241//                    // set index
242//                    idx.set(2, z);
243//
244//                    for (int y = 0; y < sizeY; y++)
245//                    {
246//                        // set index
247//                        idx.set(1, y);
248//
249//                        for (int x = 0; x < sizeX; x++, offset++)
250//                        {
251//                            // set index
252//                            idx.set(0, x);
253//                            // set pixel
254//                            result.setPixelAsUInt32(idx, data[offset]);
255//                        }
256//                    }
257//                }
258//                break;
259//
260//            case FLOAT:
261//                for (int z = 0; z < sizeZ; z++)
262//                {
263//                    final float[] data = sequence.getDataXYAsFloat(0, z, 0);
264//                    int offset = 0;
265//                    // set index
266//                    idx.set(2, z);
267//
268//                    for (int y = 0; y < sizeY; y++)
269//                    {
270//                        // set index
271//                        idx.set(1, y);
272//
273//                        for (int x = 0; x < sizeX; x++, offset++)
274//                        {
275//                            // set index
276//                            idx.set(0, x);
277//                            // set pixel
278//                            result.setPixelAsFloat(idx, data[offset]);
279//                        }
280//                    }
281//                }
282//                break;
283//
284//            case DOUBLE:
285//                for (int z = 0; z < sizeZ; z++)
286//                {
287//                    final double[] data = sequence.getDataXYAsDouble(0, z, 0);
288//                    int offset = 0;
289//                    // set index
290//                    idx.set(2, z);
291//
292//                    for (int y = 0; y < sizeY; y++)
293//                    {
294//                        // set index
295//                        idx.set(1, y);
296//
297//                        for (int x = 0; x < sizeX; x++, offset++)
298//                        {
299//                            // set index
300//                            idx.set(0, x);
301//                            // set pixel
302//                            result.setPixelAsDouble(idx, data[offset]);
303//                        }
304//                    }
305//                }
306//                break;
307//        }
308//
309//        return result;
310//    }
311//
312//    /**
313//     * Convert the specified ITK image to ICY sequence.<br>
314//     * ITK images are only 3D [XYZ] so multiple channels images
315//     * or multiple frames are not supported.
316//     */
317//    public static Sequence getSequence(Image itkImg)
318//    {
319//        final int sizeX = (int) itkImg.getWidth();
320//        final int sizeY = (int) itkImg.getHeight();
321//        final int sizeZ = (int) itkImg.getDepth();
322//        final DataType dataType = getDataTypeFromPixelID(itkImg.getPixelIDValue());
323//
324//        final Sequence result = new Sequence();
325//        final VectorUInt32 idx = new VectorUInt32(3);
326//
327//        result.beginUpdate();
328//        try
329//        {
330//            switch (dataType)
331//            {
332//                case BYTE:
333//                    for (int z = 0; z < sizeZ; z++)
334//                    {
335//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
336//                        final byte[] data = img.getDataXYAsByte(0);
337//                        int offset = 0;
338//                        // set index
339//                        idx.set(2, z);
340//
341//                        for (int y = 0; y < sizeY; y++)
342//                        {
343//                            // set index
344//                            idx.set(1, y);
345//
346//                            for (int x = 0; x < sizeX; x++, offset++)
347//                            {
348//                                // set index
349//                                idx.set(0, x);
350//                                // get pixel
351//                                data[offset] = itkImg.getPixelAsInt8(idx);
352//                            }
353//                        }
354//
355//                        img.dataChanged();
356//                        result.setImage(0, z, img);
357//                    }
358//                    break;
359//
360//                case UBYTE:
361//                    for (int z = 0; z < sizeZ; z++)
362//                    {
363//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
364//                        final byte[] data = img.getDataXYAsByte(0);
365//                        int offset = 0;
366//                        // set index
367//                        idx.set(2, z);
368//
369//                        for (int y = 0; y < sizeY; y++)
370//                        {
371//                            // set index
372//                            idx.set(1, y);
373//
374//                            for (int x = 0; x < sizeX; x++, offset++)
375//                            {
376//                                // set index
377//                                idx.set(0, x);
378//                                // get pixel
379//                                data[offset] = (byte) itkImg.getPixelAsUInt8(idx);
380//                            }
381//                        }
382//
383//                        img.dataChanged();
384//                        result.setImage(0, z, img);
385//                    }
386//                    break;
387//
388//                case SHORT:
389//                    for (int z = 0; z < sizeZ; z++)
390//                    {
391//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
392//                        final short[] data = img.getDataXYAsShort(0);
393//                        int offset = 0;
394//                        // set index
395//                        idx.set(2, z);
396//
397//                        for (int y = 0; y < sizeY; y++)
398//                        {
399//                            // set index
400//                            idx.set(1, y);
401//
402//                            for (int x = 0; x < sizeX; x++, offset++)
403//                            {
404//                                // set index
405//                                idx.set(0, x);
406//                                // get pixel
407//                                data[offset] = itkImg.getPixelAsInt16(idx);
408//                            }
409//                        }
410//
411//                        img.dataChanged();
412//                        result.setImage(0, z, img);
413//                    }
414//                    break;
415//
416//                case USHORT:
417//                    for (int z = 0; z < sizeZ; z++)
418//                    {
419//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
420//                        final short[] data = img.getDataXYAsShort(0);
421//                        int offset = 0;
422//                        // set index
423//                        idx.set(2, z);
424//
425//                        for (int y = 0; y < sizeY; y++)
426//                        {
427//                            // set index
428//                            idx.set(1, y);
429//
430//                            for (int x = 0; x < sizeX; x++, offset++)
431//                            {
432//                                // set index
433//                                idx.set(0, x);
434//                                // get pixel
435//                                data[offset] = (short) itkImg.getPixelAsUInt16(idx);
436//                            }
437//                        }
438//
439//                        img.dataChanged();
440//                        result.setImage(0, z, img);
441//                    }
442//                    break;
443//
444//                case INT:
445//                    for (int z = 0; z < sizeZ; z++)
446//                    {
447//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
448//                        final int[] data = img.getDataXYAsInt(0);
449//                        int offset = 0;
450//                        // set index
451//                        idx.set(2, z);
452//
453//                        for (int y = 0; y < sizeY; y++)
454//                        {
455//                            // set index
456//                            idx.set(1, y);
457//
458//                            for (int x = 0; x < sizeX; x++, offset++)
459//                            {
460//                                // set index
461//                                idx.set(0, x);
462//                                // get pixel
463//                                data[offset] = itkImg.getPixelAsInt32(idx);
464//                            }
465//                        }
466//
467//                        img.dataChanged();
468//                        result.setImage(0, z, img);
469//                    }
470//                    break;
471//
472//                case UINT:
473//                    for (int z = 0; z < sizeZ; z++)
474//                    {
475//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
476//                        final int[] data = img.getDataXYAsInt(0);
477//                        int offset = 0;
478//                        // set index
479//                        idx.set(2, z);
480//
481//                        for (int y = 0; y < sizeY; y++)
482//                        {
483//                            // set index
484//                            idx.set(1, y);
485//
486//                            for (int x = 0; x < sizeX; x++, offset++)
487//                            {
488//                                // set index
489//                                idx.set(0, x);
490//                                // get pixel
491//                                data[offset] = (int) itkImg.getPixelAsUInt32(idx);
492//                            }
493//                        }
494//
495//                        img.dataChanged();
496//                        result.setImage(0, z, img);
497//                    }
498//                    break;
499//
500//                case FLOAT:
501//                    for (int z = 0; z < sizeZ; z++)
502//                    {
503//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
504//                        final float[] data = img.getDataXYAsFloat(0);
505//                        int offset = 0;
506//                        // set index
507//                        idx.set(2, z);
508//
509//                        for (int y = 0; y < sizeY; y++)
510//                        {
511//                            // set index
512//                            idx.set(1, y);
513//
514//                            for (int x = 0; x < sizeX; x++, offset++)
515//                            {
516//                                // set index
517//                                idx.set(0, x);
518//                                // get pixel
519//                                data[offset] = itkImg.getPixelAsFloat(idx);
520//                            }
521//                        }
522//
523//                        img.dataChanged();
524//                        result.setImage(0, z, img);
525//                    }
526//                    break;
527//
528//                case DOUBLE:
529//                    for (int z = 0; z < sizeZ; z++)
530//                    {
531//                        final IcyBufferedImage img = new IcyBufferedImage(sizeX, sizeY, 1, dataType);
532//                        final double[] data = img.getDataXYAsDouble(0);
533//                        int offset = 0;
534//                        // set index
535//                        idx.set(2, z);
536//
537//                        for (int y = 0; y < sizeY; y++)
538//                        {
539//                            // set index
540//                            idx.set(1, y);
541//
542//                            for (int x = 0; x < sizeX; x++, offset++)
543//                            {
544//                                // set index
545//                                idx.set(0, x);
546//                                // get pixel
547//                                data[offset] = itkImg.getPixelAsDouble(idx);
548//                            }
549//                        }
550//
551//                        img.dataChanged();
552//                        result.setImage(0, z, img);
553//                    }
554//                    break;
555//            }
556//        }
557//        finally
558//        {
559//            result.endUpdate();
560//        }
561//
562//        return result;
563//    }
564}