| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
img_resize.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "Matrix.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
30 Bimage* img_resize(Bimage* p, Vector3<int> newsize, Vector3<int> translate,
31 int wrap, int fill_type, float fill)
32 {
33 if ( !p->data_pointer() ) return NULL;
34
35 if ( newsize.x() < 1 ) newsize[0] = p->sizeX();
36 if ( newsize.y() < 1 ) newsize[1] = p->sizeY();
37 if ( newsize.z() < 1 ) newsize[2] = p->sizeZ();
38
39 unsigned long i, j, c, x, y, z, n;
40 long oldx, oldy, oldz;
41
42 if ( fill_type == FILL_AVERAGE ) fill = p->average();
43 if ( fill_type == FILL_MIN ) fill = p->minimum();
44 if ( fill_type == FILL_MAX ) fill = p->maximum();
45
46 if ( verbose & VERB_PROCESS ) {
47 if ( wrap ) cout << "Resizing with wrapping:" << endl;
48 else cout << "Resizing:" << endl;
49 cout << "Shift: " << translate << endl;
50 cout << "New size: " << newsize << endl;
51 if ( fill_type != FILL_BACKGROUND )
52 cout << "Fill value: " << fill << endl;
53 cout << endl;
54 } else if ( verbose & VERB_LABEL )
55 cout << "Resizing" << endl << endl;
56
57 Bimage* pnu = p->copy_header();
58 pnu->size(newsize);
59 pnu->page_size(newsize);
60 pnu->data_alloc();
61
62 for ( i=n=0; n<p->images(); n++ ) {
63 pnu->image[n].origin(pnu->image[n].origin() + translate);
64 if ( verbose & VERB_PROCESS )
65 cout << "Image " << n+1 << " new origin: " << pnu->image[n].origin() << endl;
66 if ( fill_type == FILL_BACKGROUND ) {
67 fill = p->image[n].background();
68 if ( verbose & VERB_PROCESS )
69 cout << "Image " << n+1 << " fill value: " << fill << endl;
70 }
71 for ( z=0; z<pnu->sizeZ(); z++ ) {
72 oldz = (long)z - translate.z();
73 if ( wrap ) {
74 while ( oldz < 0 ) oldz += p->sizeZ();
75 while ( oldz >= p->sizeZ() ) oldz -= p->sizeZ();
76 }
77 for ( y=0; y<pnu->sizeY(); y++ ) {
78 oldy = (long)y - translate.y();
79 if ( wrap ) {
80 while ( oldy < 0 ) oldy += p->sizeY();
81 while ( oldy >= p->sizeY() ) oldy -= p->sizeY();
82 }
83 for ( x=0; x<pnu->sizeX(); x++ ) {
84 oldx = (long)x - translate.x();
85 if ( wrap ) {
86 while ( oldx < 0 ) oldx += p->sizeX();
87 while ( oldx >= p->sizeX() ) oldx -= p->sizeX();
88 }
89 if ( oldx < 0 || oldx >= p->sizeX() || oldy < 0 ||
90 oldy >= p->sizeY() || oldz < 0 || oldz >= p->sizeZ() ) {
91 for ( c=0; c<p->channels(); c++, i++ ) pnu->set(i, fill);
92 } else {
93 // j = (((n*p->z+oldz)*p->y + oldy)*p->x + oldx)*p->channels();
94 j = p->index(0, oldx, oldy, oldz, n);
95 for ( c=0; c<p->channels(); c++, i++, j++ ) pnu->set(i, (*p)[j]);
96 }
97 }
98 }
99 }
100 }
101
102 if ( verbose & VERB_PROCESS )
103 cout << endl;
104
105 if ( pnu->statistics() )
106 cerr << tab << "in img_resize" << endl;
107
108 return pnu;
109 }
110
124 Bimage* img_resize_to_next_power2(Bimage* p, int fill_type, float fill)
125 {
126 Vector3<int> newsize(1,1,1);
127
128 if ( p->sizeX() > 1 ) newsize[0] = findNextPowerOf(p->sizeX(), 2);
129 if ( p->sizeY() > 1 ) newsize[1] = findNextPowerOf(p->sizeY(), 2);
130 if ( p->sizeZ() > 1 ) newsize[2] = findNextPowerOf(p->sizeZ(), 2);
131
132 if ( newsize.x() == p->sizeX() && newsize.y() == p->sizeY() && newsize.z() == p->sizeZ() )
133 return 0;
134
135 Vector3<int> translate(newsize.x()/2 - p->sizeX()/2, newsize.y()/2 - p->sizeY()/2, newsize.z()/2 - p->sizeZ()/2);
136
137 return img_resize(p, newsize, translate, 0, fill_type, fill);
138 }
139
155 int img_shrink_wrap(Bimage* p, Vector3<int> newsize, Vector3<int> translate)
156 {
157 if ( !p->data_pointer() ) return -1;
158
159 if ( translate.x() == 0 && translate.y() == 0 && translate.z() == 0 &&
160 newsize.x() == p->sizeX() && newsize.y() == p->sizeY() && newsize.z() == p->sizeZ() )
161 return 0;
162
163 unsigned long i, j, c, x, y, z, n;
164 long newx, newy, newz;
165
166 if ( verbose & VERB_PROCESS ) {
167 cout << "Shrinking and wrapping:" << endl;
168 cout << "New size: " << newsize << endl;
169 cout << "New origin: " << translate << endl << endl;
170 } else if ( verbose & VERB_LABEL )
171 cout << "Shrinking and wrapping" << endl << endl;
172
173 DataType olddatatype = p->data_type();
174
175 p->change_type(Float);
176
177 unsigned long nudatasize = newsize.x()*newsize.y()*newsize.z()*p->images()*p->channels();
178 float* nudata = new float[nudatasize];
179 for ( i=0; i<nudatasize; i++ ) nudata[i] = 0;
180
181 for ( i=n=0; n<p->images(); n++ ) {
182 for ( z=0; z<p->sizeZ(); z++ ) {
183 newz = z + translate.z();
184 if ( newz < 0 ) newz += newsize.z();
185 if ( newz >= newsize.z() ) newz -= newsize.z();
186 for ( y=0; y<p->sizeY(); y++ ) {
187 newy = y + translate.y();
188 if ( newy < 0 ) newy += newsize.y();
189 if ( newy >= newsize.y() ) newy -= newsize.y();
190 for ( x=0; x<p->sizeX(); x++ ) {
191 newx = x + translate.x();
192 if ( newx < 0 ) newx += newsize.x();
193 if ( newx >= newsize.x() ) newx -= newsize.x();
194 j = (((n*newsize.z()+newz)*newsize.y() + newy)*newsize.x() + newx)*p->channels();
195 for ( c=0; c<p->channels(); c++, i++, j++ )
196 nudata[j] += (*p)[i];
197 }
198 }
199 }
200 }
201
202 p->size(newsize);
203 p->page_size(newsize);
204
205 p->data_assign((unsigned char *) nudata);
206
207 if ( p->statistics() )
208 cerr << tab << "in img_shrink_wrap" << endl;
209
210 p->change_type(olddatatype);
211
212 return 0;
213 }
214
228 Bimage* img_pad(Bimage* p, int size, int fill_type, float fill)
229 {
230 Vector3<int> newsize(p->size());
231 if ( newsize[0] > 1 ) newsize[0] = size;
232 if ( newsize[1] > 1 ) newsize[1] = size;
233 if ( newsize[2] > 1 ) newsize[2] = size;
234
235 Vector3<int> translate;
236 int wrap = 0;
237
238 return img_resize(p, newsize, translate, wrap, fill_type, fill);
239 }
240
254 Bimage* img_pad(Bimage* p, Vector3<int> size, int fill_type, float fill)
255 {
256 Vector3<int> translate;
257 int wrap = 0;
258
259 return img_resize(p, size, translate, wrap, fill_type, fill);
260 }
261
Bimage::change_type
void change_type(char letter)
Get the data type indicated by a single letter code.
Definition: Bimage.cpp:1409
Vector3::z
Type z()
Definition: Vector3.h:141
Bimage::images
unsigned long images()
Definition: Bimage.h:402
Bimage::copy_header
Bimage * copy_header()
Definition: Bimage.h:478
FILL_BACKGROUND
#define FILL_BACKGROUND
Definition: utilities.h:141
img_pad
Bimage * img_pad(Bimage *p, int size, int fill_type, float fill)
Pads an image to a new size with a given fill value.
Definition: img_resize.cpp:228
Bimage::sizeX
void sizeX(unsigned long nx)
Definition: Bimage.h:332
utilities.h
Header file for general utilities.
img_shrink_wrap
int img_shrink_wrap(Bimage *p, Vector3< int > newsize, Vector3< int > translate)
Shrinks an image to a new size with wrapping of the excluded edges.
Definition: img_resize.cpp:155
Bsub_image::background
double background()
Definition: Bimage.h:123
img_resize_to_next_power2
Bimage * img_resize_to_next_power2(Bimage *p, int fill_type, float fill)
Resizes without interpolation or rescaling to the next power of 2.
Definition: img_resize.cpp:124
Float
Definition: Bimage.h:56
FILL_AVERAGE
#define FILL_AVERAGE
Definition: utilities.h:140
Vector3< int >
Matrix.h
Generalized matrix class.
VERB_PROCESS
#define VERB_PROCESS
Definition: utilities.h:99
Bimage::average
double average(unsigned long cc, double xf, double yf, double zf, unsigned long nn, double iscale)
Averages in the xy plane when scaling is less than 1.
Definition: Bimage.cpp:1119
Bimage::statistics
size_t statistics()
Calculates the statistics for an image.
Definition: Bimage.cpp:2403
Bimage::channels
unsigned long channels()
Definition: Bimage.h:327
Bimage::size
Vector3< int > size()
Definition: Bimage.h:329
Bimage
General image parameter class.
Definition: Bimage.h:149
Bimage::data_alloc
unsigned char * data_alloc()
Definition: Bimage.h:220
Bimage::index
unsigned long index(unsigned long nx, unsigned long ny)
Definition: Bimage.h:338
Bimage::maximum
double maximum()
Definition: Bimage.h:420
Bimage::set
void set(unsigned long j, double v)
Sets a single value at the given index.
Definition: Bimage.cpp:885
Bimage::data_assign
unsigned char * data_assign(unsigned char *nudata)
Assign image data.
Definition: Bimage.cpp:545
FILL_MAX
#define FILL_MAX
Definition: utilities.h:143
Vector3::x
Type x()
Definition: Vector3.h:139
DataType
DataType
Base data type specifier.
Definition: Bimage.h:45
Bsub_image::origin
void origin(double x, double y, double z)
Definition: Bimage.h:129
Bimage::data_pointer
unsigned char * data_pointer()
Definition: Bimage.h:225
verbose
int verbose
FILL_MIN
#define FILL_MIN
Definition: utilities.h:142
Bimage::sizeZ
void sizeZ(unsigned long nz)
Definition: Bimage.h:334
img_resize
Bimage * img_resize(Bimage *p, Vector3< int > newsize, Vector3< int > translate, int wrap, int fill_type, float fill)
Resizes without interpolation or rescaling.
Definition: img_resize.cpp:30
rwimg.h
Header file for 2D and 3D image I/O.
Bimage::sizeY
void sizeY(unsigned long ny)
Definition: Bimage.h:333
Bimage::page_size
Vector3< int > page_size()
Definition: Bimage.h:395
findNextPowerOf
int findNextPowerOf(int startNumber, int powerOf)
Finds the next greatest number that is a power of a given number.
Definition: utilities.cpp:367
VERB_LABEL
#define VERB_LABEL
Definition: utilities.h:98
Bimage::data_type
DataType data_type()
Definition: Bimage.h:290
Vector3::y
Type y()
Definition: Vector3.h:140
Bimage::minimum
double minimum()
Definition: Bimage.h:419
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bimage::image
Bsub_image * image
Definition: Bimage.h:180