Improved type ScilabInteger (avoid the Long conversion)
[scilab.git] / scilab / modules / hdf5 / src / java / org / scilab / modules / hdf5 / write / H5WriteScilabInteger.java
1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2009-2009 - DIGITEO - Antoine ELIAS
4  * 
5  *  This file must be used under the terms of the CeCILL.
6  *  This source file is licensed as described in the file COPYING, which
7  *  you should have received as part of this distribution.  The terms
8  *  are also available at
9  *  http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  * 
11  */
12
13 package org.scilab.modules.hdf5.write;
14
15 import ncsa.hdf.hdf5lib.H5;
16 import ncsa.hdf.hdf5lib.HDF5Constants;
17 import ncsa.hdf.hdf5lib.exceptions.HDF5Exception;
18
19 import org.scilab.modules.hdf5.H5ScilabConstant;
20 import org.scilab.modules.types.scilabTypes.ScilabInteger;;
21
22 public class H5WriteScilabInteger {
23         public static void writeInDataSet(int file_id, String dataSetName, ScilabInteger data) throws NullPointerException, HDF5Exception {
24                 Object[] dataToWrite = null; 
25                 ScilabInteger.IntegerType prec = data.getPrec();
26                 int nativeType = 0;
27                 String classType = "";
28
29                 if(prec == ScilabInteger.IntegerType.TYPE8) {
30                         nativeType = data.isUnsigned() ? HDF5Constants.H5T_NATIVE_UINT8 : HDF5Constants.H5T_NATIVE_INT8; 
31                         classType = data.isUnsigned() ? H5ScilabConstant.SCILAB_CLASS_UINT8 : H5ScilabConstant.SCILAB_CLASS_INT8; 
32                         dataToWrite = getDataToWrite8(data);
33                 }else if(prec == ScilabInteger.IntegerType.TYPE16) {
34                         nativeType = data.isUnsigned() ? HDF5Constants.H5T_NATIVE_UINT16 : HDF5Constants.H5T_NATIVE_INT16; 
35                         classType = data.isUnsigned() ? H5ScilabConstant.SCILAB_CLASS_UINT16 : H5ScilabConstant.SCILAB_CLASS_INT16; 
36                         dataToWrite = getDataToWrite16(data);
37                 }else if(prec == ScilabInteger.IntegerType.TYPE32) {
38                         nativeType = data.isUnsigned() ? HDF5Constants.H5T_NATIVE_UINT32 : HDF5Constants.H5T_NATIVE_INT32; 
39                         classType = data.isUnsigned() ? H5ScilabConstant.SCILAB_CLASS_UINT32 : H5ScilabConstant.SCILAB_CLASS_INT32; 
40                         dataToWrite = getDataToWrite32(data);
41                 }else if(prec == ScilabInteger.IntegerType.TYPE64) {
42                         nativeType = data.isUnsigned() ? HDF5Constants.H5T_NATIVE_UINT64 : HDF5Constants.H5T_NATIVE_INT64; 
43                         classType = data.isUnsigned() ? H5ScilabConstant.SCILAB_CLASS_UINT64 : H5ScilabConstant.SCILAB_CLASS_INT64; 
44                         dataToWrite = getDataToWrite64(data);
45                 }
46
47                 long[] dims = {data.getHeight() * data.getWidth()};
48                 int dataspaceId = H5.H5Screate_simple(1, dims, null);
49                 
50                 int datasetId = H5.H5Dcreate(file_id, "/" + dataSetName, nativeType, dataspaceId, HDF5Constants.H5P_DEFAULT);
51                 H5.H5Dwrite(datasetId, nativeType, HDF5Constants.H5S_ALL, HDF5Constants.H5S_ALL, HDF5Constants.H5P_DEFAULT, dataToWrite);
52                 
53                 H5Write.createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS, H5ScilabConstant.SCILAB_CLASS_INT);
54                 H5Write.createAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS_PREC, classType);
55
56                 H5Write.createIntAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS_ROWS, data.getHeight());
57                 H5Write.createIntAttribute(datasetId, H5ScilabConstant.SCILAB_CLASS_COLS, data.getWidth());
58                 
59                 H5.H5Dclose(datasetId);
60                 H5.H5Sclose(dataspaceId);
61         }
62
63         public static Byte[] getDataToWrite8(ScilabInteger data){
64                 Byte[] dataToWrite = new Byte[data.getHeight() * data.getWidth()];
65                 byte[][] byteData = data.getDataAsByte();
66
67                 for(int i = 0 ; i < data.getHeight() ; i++) {
68                         for(int j = 0 ; j < data.getWidth() ; j++) {
69                                 dataToWrite[i + data.getHeight() * j] = byteData[i][j];
70                         }
71                 }
72                 return dataToWrite;
73         }
74
75         public static Short[] getDataToWrite16(ScilabInteger data){
76                 Short[] dataToWrite = new Short[data.getHeight() * data.getWidth()];
77                 short[][] shortData = data.getDataAsShort();
78
79                 for(int i = 0 ; i < data.getHeight() ; i++) {
80                         for(int j = 0 ; j < data.getWidth() ; j++) {
81                                 dataToWrite[i + data.getHeight() * j] = shortData[i][j];
82                         }
83                 }
84                 return dataToWrite;
85         }
86
87         public static Integer[] getDataToWrite32(ScilabInteger data){
88                 Integer[] dataToWrite = new Integer[data.getHeight() * data.getWidth()];
89                 int[][] intData = data.getDataAsInt();
90
91                 for(int i = 0 ; i < data.getHeight() ; i++) {
92                         for(int j = 0 ; j < data.getWidth() ; j++) {
93                                 dataToWrite[i + data.getHeight() * j] = intData[i][j];
94                         }
95                 }
96                 return dataToWrite;
97         }
98
99         public static Long[] getDataToWrite64(ScilabInteger data){
100                 Long[] dataToWrite = new Long[data.getHeight() * data.getWidth()];
101                 long[][] longData = data.getDataAsLong();
102
103                 for(int i = 0 ; i < data.getHeight() ; i++) {
104                         for(int j = 0 ; j < data.getWidth() ; j++) {
105                                 dataToWrite[i + data.getHeight() * j] = longData[i][j];
106                         }
107                 }
108                 return dataToWrite;
109         }
110 }