Fixed text file permissions
[snark14.git] / tools / Display / image.cpp
1 /** @file image.cpp
2     @package snark14Display
3     @author Bruno M. Carvalho and Deniz Sarioz
4     licensed under (open-source) QPL v1.0
5     which accompanies this distribution in the file QPL
6 */
7
8 #include "variables.hpp"
9 #include "image.hpp"
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <malloc.h>
14 #include <qmessagebox.h>
15
16 Snarkimage::Snarkimage()
17 {
18   image=NULL;
19   max=-100000;
20   min=100000;
21   isloaded=false;
22   fileformat=DIGDataFormat_ASCII;
23 }
24
25 Snarkimage::Snarkimage(int imagenumber,DIGDataFormat format)
26 {
27   int i,j;
28   double *tmpimg;
29   max=-100000;
30   min=100000;
31   tmpimg=(double *)malloc(sizeof(double)*Sizey*Sizex);
32   image=matrix(0,Sizey,0,Sizex);
33   if(!format) { /* format=0 (ASCII)  */
34     fileformat=DIGDataFormat_ASCII;
35     digrecfil.SelectArraySet(imageindexes[imagenumber][0]);
36     digrecfil.SelectArray(imageindexes[imagenumber][1]);
37     digrecfil.GetArrayData(tmpimg);
38     for(i=0;i<Sizey;i++) {
39 // bug #141 fix
40       for(j=0;j<Sizex;j++) {
41         image[Sizey-i-1][j]=tmpimg[i*Sizex+j];
42         // bug! image[i][j]=tmpimg[i*Sizey+j]; fixed 2004 apr
43         if(image[Sizey-i-1][j]>max)
44           max=image[Sizey-i-1][j];
45         if(image[Sizey-i-1][j]<min)
46           min=image[Sizey-i-1][j];
47       }
48     }
49   }
50   else { /* format=1 (BINARY) */
51     fileformat=DIGDataFormat_BINARY;
52     digrecfil.SelectArraySet(imageindexes[imagenumber][0]);
53     digrecfil.SelectArray(imageindexes[imagenumber][1]);
54     digrecfil.GetArrayData(tmpimg);
55     //    for(i=0;i<Sizey;i++) {
56     for(i=Sizey-1; i>=0; i--) { // bug #141 fix
57       for(j=0;j<Sizex;j++) {
58         //bug! image[i][j]=tmpimg[i*Sizey+j]; fixed 2004 apr
59         image[Sizey-i-1][j]=tmpimg[i*Sizex+j];
60         if(image[Sizey-i-1][j]>max)
61           max=image[Sizey-i-1][j];
62         if(image[Sizey-i-1][j]<min)
63           min=image[Sizey-i-1][j];
64       }
65     }
66   }
67  isloaded=true;
68  if(max>globalmax) 
69    globalmax=max;
70  if(min<globalmin) 
71    globalmin=min;
72  free(tmpimg);
73 }
74
75 /*  
76  *  Destroys the object and frees any allocated resources
77  */
78 Snarkimage::~Snarkimage()
79 {
80     // no need to delete child widgets, Qt does it all for us
81 }
82
83 void Snarkimage::readImage(int imagenumber,DIGDataFormat format)
84 {
85   int i,j;
86   double *tmpimg;
87
88   max=-100000;
89   min=100000;
90   tmpimg=(double *)malloc(sizeof(double)*Sizey*Sizex);
91   image=matrix(0,Sizey,0,Sizex);
92   if(!format) { /* format=0 (DIGDataFormat_ASCII)  */
93     digrecfil.SelectArraySet(imageindexes[imagenumber][0]);
94     digrecfil.SelectArray(imageindexes[imagenumber][1]);
95     digrecfil.GetArrayData(tmpimg);
96     for(i=0;i<Sizey;i++) {
97       // bug #141 fix
98       for(j=0;j<Sizex;j++) {
99         image[Sizey-i-1][j]=tmpimg[i*Sizey+j];
100         if(image[Sizey-i-1][j]>max)
101           max=image[Sizey-i-1][j];
102         if(image[Sizey-i-1][j]<min)
103           min=image[Sizey-i-1][j];
104       }
105     }
106   }
107   else { /* format=1 (DIGDataFormat_BINARY) */
108     digrecfil.SelectArraySet(imageindexes[imagenumber][0]);
109     digrecfil.SelectArray(imageindexes[imagenumber][1]);
110     digrecfil.GetArrayData(tmpimg);
111     for(i=0;i<Sizey;i++) {
112       // bug #141 fix
113       for(j=0;j<Sizex;j++) {
114         image[Sizey-i-1][j]=tmpimg[i*Sizey+j];
115         if(image[Sizey-i-1][j]>max)
116           max=image[Sizey-i-1][j];
117         if(image[Sizey-i-1][j]<min)
118           min=image[Sizey-i-1][j];
119       }
120     }
121   }
122  isloaded=true;
123  if(max>globalmax) 
124    globalmax=max;
125  if(min<globalmin) 
126    globalmin=min;
127  free(tmpimg);
128 }
129
130 double** Snarkimage::getImage()
131 {
132   return image;
133 }
134
135 double Snarkimage::getMax()
136 {
137   return max;
138 }
139
140 double Snarkimage::getMin()
141 {
142   return min;
143 }
144
145 bool Snarkimage::isLoaded()
146 {
147   return isloaded;
148 }
149
150 double** Snarkimage::matrix(int nrl,int nrh,int ncl,int nch)
151 /*  Allocates a double pt array with range [npl .. nph][nrl .. nrh][ncl .. nch]  */
152 {
153    int j;
154    double **m;
155
156    m = (double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
157    if (!m) {
158        printf("Allocation failure in matrix() (y)\n");
159        exit(-1);
160    }
161    m -= nrl;
162    for(j=nrl; j<=nrh; j++) {
163      m[j] = (double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
164      if(!m[j]) {
165        printf("Allocation failure in matrix() (x)\n");
166        exit(-1);
167      }
168      m[j] -= ncl;
169    }
170    return m;
171 }
172