-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageLoader.cpp
146 lines (108 loc) · 3.65 KB
/
imageLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* imageLoader.cpp
*
* Created on: 26/09/2011
* Author: kimbjerge
*/
// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
// includes, bmp utilities
#include "defs.h"
#include "BmpUtil.h"
#include "imageLoader.h"
// Moved image pointer to next image
byte *NextImage(byte *pImage, int imgStride, ROI size){
return (pImage + (imgStride*size.height));
}
// Loads image from file
// Allocates memory for source and destination of image
// based size of image, image type must be bmp
int loadImage(char* fileName, const char* path, byte** imgSrc, ROI* imgSize, int *imgStride){
//preload image (acquire dimensions)
int ImgWidth, ImgHeight;
//char *pImageFpath = cutFindFilePath(fileName, path);
char *pImageFpath = fileName;
DEBUG_MSG("[loadImage]\n%s\n", pImageFpath);
int res = PreLoadBmp(pImageFpath, &ImgWidth, &ImgHeight);
imgSize->width = ImgWidth;
imgSize->height = ImgHeight;
if (res){
printf("\nError %d: Image file %s not found or invalid!\n", res, pImageFpath);
printf("Press ENTER to exit...\n");
getchar();
return 1;
}
//allocate image buffers
*imgSrc = MallocPlaneByte(ImgWidth, ImgHeight, imgStride);
//load sample image
LoadBmpAsGray(pImageFpath, *imgStride, *imgSize, *imgSrc);
//check image dimensions are multiples of BLOCK_SIZE
if (ImgWidth % BLOCK_SIZE != 0 || ImgHeight % BLOCK_SIZE != 0){
printf("\nError: Input image dimensions must be multiples of 8!\n");
printf("Press ENTER to exit...\n");
getchar();
return 1;
}
DEBUG_MSG("Image size [%d x %d], %d \n", ImgWidth, ImgHeight, *imgStride);
return 0;
}
// Loads image from files 1-9
// Allocates memory for source images in 3D cube
// based size of image, image type must be bmp
int loadImages(char* fileName, const char* path, byte** imgSrc, ROI* imgSize, int *imgStride, int depth){
//preload image (acquire dimensions)
int ImgWidth, ImgHeight, tmpW, tmpH;
ROI imgOrgSize; // Original
byte *imgCur;
//char *pImageFpath = cutFindFilePath(fileName, path);
char ImageName[50];
sprintf(ImageName, fileName, 1);
DEBUG_MSG("[loadImages]\n");
int res = PreLoadBmp(ImageName, &ImgWidth, &ImgHeight);
if (res){
printf("\nError %d: Image file %s not found or invalid!\n", res, ImageName);
printf("Press ENTER to exit...\n");
getchar();
return 1;
}
tmpW = ImgWidth % BLOCK_SIZE;
if (tmpW > 0)
ImgWidth += BLOCK_SIZE - tmpW;
tmpH = ImgHeight % BLOCK_SIZE;
if (tmpH > 0)
ImgHeight += BLOCK_SIZE - tmpH;
//check image dimensions are multiples of BLOCK_SIZE
if (ImgWidth % BLOCK_SIZE != 0 || ImgHeight % BLOCK_SIZE != 0){
printf("\nError: Input image dimensions must be multiples of 8!\n");
printf("Press ENTER to exit...\n");
getchar();
return 1;
}
//allocate image buffers
*imgSrc = MallocCubeByte(ImgWidth, ImgHeight, depth, imgStride);
imgSize->width = ImgWidth;
imgSize->height = ImgHeight;
imgCur = *imgSrc;
//load sample images
for (int i = 1; i <= depth; i++){
DEBUG_MSG("Loading image %s [%d,%d,%d] \n", ImageName, ImgWidth, ImgHeight, *imgStride);
sprintf(ImageName, fileName, i);
res = PreLoadBmp(ImageName, &tmpW, &tmpH);
if (res){
printf("\nError %d: Image file %s not found or invalid!\n", res, ImageName);
printf("Press ENTER to exit...\n");
getchar();
return 1;
}
imgOrgSize.height = tmpH;
imgOrgSize.width = tmpW;
DEBUG_MSG("Loading org image %s [%d,%d] \n", ImageName, tmpW, tmpH, *imgStride);
LoadBmpAsGray(ImageName, *imgStride, imgOrgSize, imgCur);
imgCur = NextImage(imgCur, *imgStride, *imgSize);
}
DEBUG_MSG("Images size [%d * %d * %d], stride %d \n", ImgWidth, ImgHeight, depth, *imgStride);
return 0;
}