X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsim%2Fimagefile.cpp;h=ef8b1e4bc7d262f4834aa549e2285b409a9a5568;hp=bab87bdeff5ab0fa4486a66f629089e85efd4d91;hb=8a7697ce57b56cdc43698cd1241ad98d49f9b5ac;hpb=3ea498d51ce4597e9649cd21f155b51175ea0bea diff --git a/libctsim/imagefile.cpp b/libctsim/imagefile.cpp index bab87bd..ef8b1e4 100644 --- a/libctsim/imagefile.cpp +++ b/libctsim/imagefile.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (c) 1983-2001 Kevin Rosenberg ** -** $Id: imagefile.cpp,v 1.42 2001/03/21 21:45:31 kevin Exp $ +** $Id$ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -29,6 +29,7 @@ #ifdef HAVE_CTN_DICOM #include "ctndicom.h" #endif +#include "interpolator.h" const double ImageFile::s_dRedGrayscaleFactor = 0.299; const double ImageFile::s_dGreenGrayscaleFactor = 0.587; @@ -46,30 +47,33 @@ const int ImageFile::EXPORT_FORMAT_PNG16 = 4; #ifdef HAVE_CTN_DICOM const int ImageFile::EXPORT_FORMAT_DICOM = 5; #endif +const int ImageFile::EXPORT_FORMAT_RAW = 6; const char* ImageFile::s_aszExportFormatName[] = { - {"text"}, - {"pgm"}, - {"pgmascii"}, + "text", + "pgm", + "pgmascii", #ifdef HAVE_PNG - {"png"}, - {"png16"}, + "png", + "png16", #endif #ifdef HAVE_CTN_DICOM - {"dicom"}, + "dicom", #endif }; const char* ImageFile::s_aszExportFormatTitle[] = { - {"Text"}, - {"PGM"}, - {"PGM ASCII"}, - {"PNG"}, - {"PNG 16-bit"}, + "Text", + "PGM", + "PGM ASCII", +#ifdef HAVE_PNG + "PNG", + "PNG 16-bit", +#endif #ifdef HAVE_CTN_DICOM - {"Dicom"}, + "Dicom", #endif }; const int ImageFile::s_iExportFormatCount = sizeof(s_aszExportFormatName) / sizeof(const char*); @@ -87,21 +91,23 @@ const int ImageFile::IMPORT_FORMAT_DICOM = 2; const char* ImageFile::s_aszImportFormatName[] = { - {"ppm"}, + "ppm", #ifdef HAVE_PNG - {"png"}, + "png", #endif #ifdef HAVE_CTN_DICOM - {"dicom"}, + "dicom", #endif }; const char* ImageFile::s_aszImportFormatTitle[] = { - {"PPM"}, - {"PNG"}, + "PPM", +#ifdef HAVE_PNG + "PNG", +#endif #ifdef HAVE_CTN_DICOM - {"Dicom"}, + "Dicom", #endif }; const int ImageFile::s_iImportFormatCount = sizeof(s_aszImportFormatName) / sizeof(const char*); @@ -840,7 +846,7 @@ ImageFile::fftRows (ImageFile& result) const fftw_complex* in = new fftw_complex [m_nx]; std::complex* pcRow = new std::complex [m_nx]; - for (int iy = 0; iy < m_ny; iy++) { + for (unsigned int iy = 0; iy < m_ny; iy++) { unsigned int ix; for (ix = 0; ix < m_nx; ix++) { in[ix].re = vReal[ix][iy]; @@ -891,7 +897,7 @@ ImageFile::ifftRows (ImageFile& result) const std::complex* pcRow = new std::complex [m_nx]; unsigned int ix, iy; - unsigned int iArray = 0; + // unsigned int iArray = 0; for (iy = 0; iy < m_ny; iy++) { for (ix = 0; ix < m_nx; ix++) { double dImag = 0; @@ -942,7 +948,7 @@ ImageFile::fftCols (ImageFile& result) const std::complex* pcCol = new std::complex [m_ny]; fftw_complex* in = new fftw_complex [m_ny]; - for (int ix = 0; ix < m_nx; ix++) { + for (unsigned int ix = 0; ix < m_nx; ix++) { unsigned int iy; for (iy = 0; iy < m_ny; iy++) { in[iy].re = vReal[ix][iy]; @@ -993,7 +999,7 @@ ImageFile::ifftCols (ImageFile& result) const std::complex* pcCol = new std::complex [m_ny]; unsigned int ix, iy; - unsigned int iArray = 0; + // unsigned int iArray = 0; for (ix = 0; ix < m_nx; ix++) { for (iy = 0; iy < m_ny; iy++) { double dImag = 0; @@ -1185,7 +1191,7 @@ ImageFile::magnitude (ImageFile& result) const if (isComplex()) vRealResult[ix][iy] = ::sqrt (vReal[ix][iy] * vReal[ix][iy] + vImag[ix][iy] * vImag[ix][iy]); else - vRealResult[ix][iy] = vReal[ix][iy]; + vRealResult[ix][iy] = ::fabs(vReal[ix][iy]); } if (result.isComplex()) @@ -1608,22 +1614,24 @@ ImageFile::readImagePNG (const char* const pszFile) ImageFileArray v = getArray(); for (int iy = 0; iy < height; iy++) { for (int ix = 0; ix < width; ix++) { - double dV; + double dV = 0; if (color_type == PNG_COLOR_TYPE_GRAY) { if (bit_depth == 8) dV = row_pointers[iy][ix] / 255.; else if (bit_depth == 16) { int iBase = ix * 2; dV = (row_pointers[iy][iBase] + (row_pointers[iy][iBase+1] << 8)) / 65536.; - } + } else + dV = 0; } else if (color_type == PNG_COLOR_TYPE_RGB) { if (bit_depth == 8) { int iBase = ix * 3; double dR = row_pointers[iy][iBase] / 255.; double dG = row_pointers[iy][iBase+1] / 255.; double dB = row_pointers[iy][iBase+2] / 255.; - dV = colorToGrayscale (dR, dG, dR); - } + dV = colorToGrayscale (dR, dG, dB); + } else + dV = 0; } v[ix][height-iy-1] = dV; } @@ -1667,6 +1675,9 @@ ImageFile::exportImage (const char* const pszFormat, const char* const pszFilena return bSuccess; } #endif + else if (iFormatID == EXPORT_FORMAT_RAW) + return writeImageRaw(pszFilename, nxcell, nycell); + sys_error (ERR_SEVERE, "Invalid format %s [ImageFile::exportImage]", pszFormat); return false; @@ -1815,7 +1826,7 @@ ImageFile::writeImagePNG (const char* const outfile, int bitdepth, int nxcell, i png_init_io(png_ptr, fp); - png_set_IHDR (png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT); + png_set_IHDR (png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); png_write_info(png_ptr, info_ptr); for (int irow = ny - 1; irow >= 0; irow--) { @@ -1900,3 +1911,26 @@ ImageFile::writeImageGIF (const char* const outfile, int nxcell, int nycell, dou } #endif +bool +ImageFile::writeImageRaw (const char* const outfile, int nxcell, int nycell) +{ + FILE *fp; + int nx = m_nx; + int ny = m_ny; + ImageFileArray v = getArray(); + + if ((fp = fopen (outfile, "wb")) == NULL) + return false; + + for (int irow = ny - 1; irow >= 0; irow--) { + for (int icol = 0; icol < nx; icol++) { + float dens = v[icol][irow]; + fwrite(&dens, sizeof(float), 1, fp); + } + } + + fclose(fp); + return true; +} + +