63 , OHC_encoder(nullptr)
64 , OHC_decoder(nullptr)
68 fftw_plan_with_nthreads(omp_get_max_threads());
83 fftw_cleanup_threads();
88 string filename(fname);
90 size_t find = filename.find_last_of(
".");
91 size_t len = filename.length();
96 if (!filename.substr(find).compare(fext))
105 if (idx < 0 || idx > 2)
return false;
107 int N = width * height;
110 #pragma omp parallel for firstprivate(a) 112 for (
int i = 0; i < N; i++) {
113 dst[i * 3 + a] = src[i];
121 if (idx < 0 || idx > 2)
return false;
123 int N = width * height;
126 #pragma omp parallel for firstprivate(a) 128 for (
int i = 0; i < N; i++) {
129 dst[i] = src[i * 3 + a];
141 int _byteperline = ((width * bitsperpixel >> 3) + 3) & ~3;
142 int _pixelbytesize = height * _byteperline;
143 int _filesize = _pixelbytesize;
144 bool hasColorTable = (bitsperpixel <= 8) ?
true :
false;
145 int _headersize =
sizeof(
bitmap);
146 int _iColor = (hasColorTable) ? 256 : 0;
156 _headersize += _iColor *
sizeof(
rgbquad);
158 memset(table, 0,
sizeof(
rgbquad) * _iColor);
159 for (
int i = 0; i < _iColor; i++) {
160 table[i].rgbBlue = i;
161 table[i].rgbGreen = i;
166 _filesize += _headersize;
169 memset(pBitmap, 0x00, _filesize);
197 memcpy(&pBitmap[iCur], table,
sizeof(
rgbquad) * _iColor);
198 iCur +=
sizeof(
rgbquad) * _iColor;
201 ImgControl *pControl = ImgControl::getInstance();
203 memcpy(pTmp, src, _pixelbytesize);
211 pControl->
Rotate(180.0, pTmp, pTmp, width + padding, height, width + padding, height, bitsperpixel >> 3);
216 for (
int i = 0; i < height; i++)
218 memcpy(&pBitmap[iCur], &pTmp[width * i], width);
220 memset(&pBitmap[iCur], 0x00, padding);
226 memcpy(&pBitmap[iCur], pTmp, _pixelbytesize);
227 iCur += _pixelbytesize;
231 if (iCur != _filesize)
234 FILE* fp = fopen(fname,
"wb");
238 fwrite(pBitmap, 1, _filesize, fp);
243 if (hasColorTable && table)
delete[] table;
257 std::string fullname = fname;
268 for (
uint i = 0; i < wavelength_num; i++)
274 LOG(
"<FAILED> Saving ohc file: %s\n", fname);
284 std::string fullname = fname;
289 LOG(
"<FAILED> Load ohc : %s\n", fname);
297 vector<Real> wavelengthArray;
299 size_t nWave = wavelengthArray.size();
302 LOG(
"<FAILED> Do not load wavelength size.\n");
307 for (
int i = 0; i < nWave; i++)
323 FILE *
infile = fopen(fname,
"rb");
326 LOG(
"<FAILED> No such file.\n");
336 LOG(
"<FAILED> Not BMP file.\n");
355 FILE *
infile = fopen(fname,
"rb");
359 LOG(
"<FAILED> No such file.\n");
369 LOG(
"<FAILED> Not BMP file.\n");
404 char szExtension[FILENAME_MAX] = { 0, };
406 strcpy(szExtension, strrchr(fname,
'.') + 1);
409 if (_stricmp(szExtension,
"bmp")) {
411 if (strcasecmp(szExtension,
"bmp")) {
413 LOG(
"<FAILED> Not BMP file.\n");
419 if (
infile ==
nullptr) { LOG(
"<FAILED> Load image file.\n");
return false; }
439 int channel = channels;
440 int nBytePerLine = ((
w * channel) + 3) & ~3;
441 int nNewBytePerLine = ((neww * channel) + 3) & ~3;
443 #pragma omp parallel for firstprivate(nBytePerLine, nNewBytePerLine, w, h, neww, newh, channel) 445 for (
int y = 0; y < newh; y++)
447 int nbppY = y * nNewBytePerLine;
448 for (
int x = 0; x < neww; x++)
450 float gx = (x / (float)neww) * (
w - 1);
451 float gy = (y / (float)newh) * (
h - 1);
457 uint32_t a00, a01, a10, a11;
459 a00 = src[gxi + 0 + gyi * nBytePerLine];
460 a01 = src[gxi + 1 + gyi * nBytePerLine];
461 a10 = src[gxi + 0 + (gyi + 1) * nBytePerLine];
462 a11 = src[gxi + 1 + (gyi + 1) * nBytePerLine];
467 float w1 = (1 - dx) * (1 - dy);
468 float w2 = dx * (1 - dy);
469 float w3 = (1 - dx) * dy;
472 dst[x + y * neww] = int(a00 * w1 + a01 * w2 + a10 * w3 + a11 * w4);
474 else if (channel == 3) {
475 uint32_t b00[3], b01[3], b10[3], b11[3];
476 int srcX = gxi * channel;
477 int dstX = x * channel;
479 b00[0] = src[srcX + 0 + gyi * nBytePerLine];
480 b00[1] = src[srcX + 1 + gyi * nBytePerLine];
481 b00[2] = src[srcX + 2 + gyi * nBytePerLine];
483 b01[0] = src[srcX + 3 + gyi * nBytePerLine];
484 b01[1] = src[srcX + 4 + gyi * nBytePerLine];
485 b01[2] = src[srcX + 5 + gyi * nBytePerLine];
487 b10[0] = src[srcX + 0 + (gyi + 1) * nBytePerLine];
488 b10[1] = src[srcX + 1 + (gyi + 1) * nBytePerLine];
489 b10[2] = src[srcX + 2 + (gyi + 1) * nBytePerLine];
491 b11[0] = src[srcX + 3 + (gyi + 1) * nBytePerLine];
492 b11[1] = src[srcX + 4 + (gyi + 1) * nBytePerLine];
493 b11[2] = src[srcX + 5 + (gyi + 1) * nBytePerLine];
498 float w1 = (1 - dx) * (1 - dy);
499 float w2 = dx * (1 - dy);
500 float w3 = (1 - dx) * dy;
503 dst[dstX + 0 + nbppY] = int(b00[0] * w1 + b01[0] * w2 + b10[0] * w3 + b11[0] * w4);
504 dst[dstX + 1 + nbppY] = int(b00[1] * w1 + b01[1] * w2 + b10[1] * w3 + b11[1] * w4);
505 dst[dstX + 2 + nbppY] = int(b00[2] * w1 + b01[2] * w2 + b10[2] * w3 + b11[2] * w4);
514 unsigned int r = 0, g = 0, b = 0;
517 for (
int i = 0; i < N; i++)
519 unsigned int blue = src[i + 0];
520 unsigned int green = src[i + 1];
521 unsigned int red = src[i + 2];
522 dst[idx++] = (blue + green + red) / 3;
532 if (fft_in ==
nullptr)
533 fft_in = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * n);
534 if (fft_out ==
nullptr)
535 fft_out = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * n);
539 fft_in =
reinterpret_cast<fftw_complex*
>(in);
545 if (!bIn)
delete[] in;
548 plan_fwd = fftw_plan_dft_1d(n, fft_in, fft_out, sign, flag);
550 plan_bwd = fftw_plan_dft_1d(n, fft_in, fft_out, sign, flag);
552 LOG(
"failed fftw : wrong sign");
561 pnx = n[
_X], pny = n[
_Y];
564 if (fft_in ==
nullptr)
565 fft_in = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * N);
566 if (fft_out ==
nullptr)
567 fft_out = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * N);
571 fft_in =
reinterpret_cast<fftw_complex*
>(in);
577 plan_fwd = fftw_plan_dft_2d(pny, pnx, fft_in, fft_out, sign, flag);
579 plan_bwd = fftw_plan_dft_2d(pny, pnx, fft_in, fft_out, sign, flag);
581 LOG(
"failed fftw : wrong sign");
589 pnx = n[
_X], pny = n[
_Y], pnz = n[
_Z];
590 int size = pnx * pny * pnz;
593 if (fft_in ==
nullptr)
594 fft_in = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * size);
595 if (fft_out ==
nullptr)
596 fft_out = (fftw_complex*)fftw_malloc(
sizeof(fftw_complex) * size);
603 for (
int i = 0; i < size; i++) {
604 fft_in[i][
_RE] = in[i].real();
605 fft_in[i][
_IM] = in[i].imag();
610 if (!bIn)
delete[] in;
613 plan_fwd = fftw_plan_dft_3d(pnz, pny, pnx, fft_in, fft_out, sign, flag);
615 plan_bwd = fftw_plan_dft_3d(pnz, pny, pnx, fft_in, fft_out, sign, flag);
617 LOG(
"failed fftw : wrong sign");
626 fftw_execute(plan_fwd);
628 fftw_execute(plan_bwd);
630 LOG(
"failed fftw : wrong sign");
636 int size = pnx * pny * pnz;
640 #pragma omp parallel for 642 for (
int i = 0; i < size; i++) {
643 out[i][
_RE] = fft_out[i][
_RE];
644 out[i][
_IM] = fft_out[i][
_IM];
649 #pragma omp parallel for 651 for (
int i = 0; i < size; i++) {
652 out[i][
_RE] = fft_out[i][
_RE] / size;
653 out[i][
_IM] = fft_out[i][
_IM] / size;
666 if (fft_in ==
nullptr)
667 fft_in = (fftw_complex *)fftw_malloc(
sizeof(fftw_complex) * N);
668 if (fft_out ==
nullptr)
669 fft_out = (fftw_complex *)fftw_malloc(
sizeof(fftw_complex) * N);
671 if (plan_fwd ==
nullptr)
672 plan_fwd = fftw_plan_dft_2d(pnY, pnX, fft_in, fft_out, sign, flag);
673 if (plan_bwd ==
nullptr)
674 plan_bwd = fftw_plan_dft_2d(pnY, pnX, fft_in, fft_out, sign, flag);
680 fftw_destroy_plan(plan_fwd);
684 fftw_destroy_plan(plan_bwd);
700 const int N = nx * ny;
701 fftw_complex *in, *out;
702 const bool bIn = fft_in ==
nullptr ?
true :
false;
703 const bool bOut = fft_out ==
nullptr ?
true :
false;
706 in = (fftw_complex *)fftw_malloc(
sizeof(fftw_complex) * N);
711 out = (fftw_complex *)fftw_malloc(
sizeof(fftw_complex) * N);
717 fftw_plan plan =
nullptr;
718 if (!plan_fwd && !plan_bwd) {
719 plan = fftw_plan_dft_2d(ny, nx, in, out, type,
OPH_ESTIMATE);
724 fftw_execute_dft(plan_fwd, in, out);
726 fftw_execute_dft(plan_bwd, in, out);
731 #pragma omp parallel for 732 for (
int k = 0;
k < N;
k++) {
738 fftw_destroy_plan(plan);
755 #pragma omp parallel for firstprivate(hnx, hny) 757 for (
int i = 0; i < nx; i++)
759 for (
int j = 0; j < ny; j++)
761 int ti = i - hnx;
if (ti < 0) ti += nx;
762 int tj = j - hny;
if (tj < 0) tj += ny;
764 output[ti + tj * nx] = input[i + j * nx];
784 if (num > omp_get_max_threads())
785 omp_set_num_threads(omp_get_max_threads());
787 omp_set_num_threads(num);
789 LOG(
"Not used openMP\n");
799 num_threads = omp_get_num_threads();
810 for (
uint i = 0; i < wavelength_num; i++) {
virtual bool saveAsImg(const char *fname, uint8_t bitsperpixel, uchar *src, int width, int height)
Function for creating image files.
void fftFree(void)
Resource release method.
void setMaxThreadNum(int num)
Function for setting the max thread num.
int getMaxThreadNum()
Function for getting the max thread num.
void fftShift(int nx, int ny, Complex< Real > *input, Complex< Real > *output)
Swap the top-left quadrant of data with the bottom-right , and the top-right quadrant with the bottom...
bool Flip(FLIP mode, const uint8_t *src, uint8_t *dst, const uint32_t w, const uint32_t h, const uint8_t ch)
Flip the bitmap.
bool Rotate(const double rotate, const uint8_t *src, uint8_t *dst, const uint32_t w, const uint32_t h, const uint32_t neww, const uint32_t newh, const uint8_t ch)
Rotate the bitmap.
void fft3(ivec3 n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 3-dimension operations inside Openholo.
bool checkExtension(const char *fname, const char *ext)
Functions for extension checking.
structure for 2-dimensional integer vector and its arithmetic.
void setWaveNum(int num)
Function for setting the wave number.
void getComplexFieldData(OphComplexField &cmplx_field, uint wavelen_idx)
virtual void releaseFldData()
void addComplexFieldData(const OphComplexField &data)
bool separateColor(int idx, int width, int height, uchar *src, uchar *dst)
Function for generate each grayscale image from RGB image.
void imgScaleBilinear(uchar *src, uchar *dst, int w, int h, int neww, int newh, int channels=1)
Function for change image size.
bool getImgSize(int &w, int &h, int &bytesperpixel, const char *fname)
Function for getting the image size.
void fftExecute(Complex< Real > *out, bool bReverse=false)
Execution functions to be called after fft1, fft2, and fft3.
void fft2(ivec2 n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 2-dimension operations inside Openholo.
void convertToFormatGray8(uchar *src, uchar *dst, int w, int h, int bytesperpixel)
Function for convert image format to gray8.
ImgDecoderOhc * OHC_decoder
virtual ~Openholo(void)=0
Destructor.
void fftInit2D(ivec2 size, int sign, unsigned int flag)
initialize method for 2D FFT
bool setFileName(const std::string &_fname)
virtual bool loadAsOhc(const char *fname)
Function to read OHC file.
Openholo(void)
Constructor.
virtual uchar * loadAsImg(const char *fname)
Function for loading image files.
virtual bool saveAsOhc(const char *fname)
Function to write OHC file
structure for 3-dimensional integer vector and its arithmetic.
Complex< Real > ** complex_H
bitmapinfoheader _bitmapinfoheader
void getOHCheader(oph::ohcHeader &_Header)
#define ELAPSED_TIME(x, y)
ImgEncoderOhc * OHC_encoder
OHC file format Variables for read and write.
void fft1(int n, Complex< Real > *in, int sign=OPH_FORWARD, uint flag=OPH_ESTIMATE)
Functions for performing fftw 1-dimension operations inside Openholo.
bool loadAsImgUpSideDown(const char *fname, uchar *dst)
Function for loading image files | Output image data upside down.
void getWavelength(std::vector< double_t > &wavlen_array)
bool mergeColor(int idx, int width, int height, uchar *src, uchar *dst)
Function for generate RGB image from each grayscale image.
virtual void ophFree(void)
Pure virtual function for override in child classes.