LCOV - code coverage report
Current view: top level - src/Util - PNG.H (source / functions) Coverage Total Hit
Test: coverage_merged.info Lines: 0.0 % 134 0
Test Date: 2025-06-26 20:08:28 Functions: 0.0 % 7 0

            Line data    Source code
       1              : 
       2              : #include <string.h>
       3              : 
       4              : #include "Set/Set.H"
       5              : #include "IO/ParmParse.H"
       6              : 
       7              : #ifndef ALAMO_NOPNG
       8              : #include <png.h>
       9              : #endif
      10              : 
      11              : namespace Util
      12              : {
      13              : class PNG
      14              : {
      15              : public:
      16            0 :     PNG() {} 
      17              :     PNG(IO::ParmParse &pp,std::string name) 
      18              :     {pp.queryclass(name, *this);}
      19            0 :     ~PNG()
      20              :     {
      21              : #ifndef ALAMO_NOPNG
      22            0 :         for (int y = 0; y < png_height; y++) {
      23            0 :             free(row_pointers[y]);
      24              :         }
      25            0 :         free(row_pointers);
      26              : #endif
      27            0 :     } 
      28              : 
      29              :     //enum Type {XYZ, XY, YZ, XZ};
      30              :     enum Fit { Stretch, FitWidth, FitHeight, Coord };
      31              :     enum Channel { R = 0, G = 1, B = 2, A = 3 };
      32              : 
      33            0 :     static void Parse(PNG &value, IO::ParmParse &pp)
      34              :     {
      35              : #ifndef ALAMO_NOPNG
      36              : 
      37            0 :         std::string filename;
      38            0 :         pp_query_file("filename", filename); // BMP filename.
      39              : 
      40            0 :         std::string fit = "stretch";
      41              :         // how to position the image
      42            0 :         pp_query_validate("fit", fit, {"stretch","fitheight","fitwidth","coord"}); 
      43            0 :         if (fit == "stretch")        value.fit = Fit::Stretch;
      44            0 :         else if (fit == "fitheight") value.fit = Fit::FitHeight;
      45            0 :         else if (fit == "fitwidth")  value.fit = Fit::FitWidth;
      46            0 :         else if (fit == "coord")
      47              :         {
      48            0 :             value.fit = Fit::Coord;
      49            0 :             pp_queryarr("coord.lo", value.coord_lo); // Lower-left coordinates of image in domain
      50            0 :             pp_queryarr("coord.hi", value.coord_hi); // Upper-right coordinates of image in domain
      51              :         }
      52            0 :         else Util::Abort(INFO);
      53              : 
      54              :         // Desired minimum value to scale pixels by
      55            0 :         pp_query_default("min", value.min, 0.0  );
      56              :         // Desired maximum value to scale pixels by
      57            0 :         pp_query_default("max", value.max, 255.0);
      58              : 
      59              :         //
      60              :         //
      61              :         // Now, read in the PNG
      62              :         //
      63              :         //
      64              : 
      65              : 
      66            0 :         FILE* fp = std::fopen(filename.c_str(), "rb");
      67              : 
      68            0 :         if (fp == NULL) Util::Abort(INFO, "Cannot find file", filename);
      69              : 
      70            0 :         png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
      71            0 :         if (!png) Util::Abort(INFO);
      72              : 
      73            0 :         png_infop info = png_create_info_struct(png);
      74            0 :         if (!info) Util::Abort(INFO);
      75              : 
      76            0 :         if (setjmp(png_jmpbuf(png))) Util::Abort(INFO);
      77              : 
      78              : 
      79            0 :         png_init_io(png, fp);
      80              : 
      81            0 :         png_read_info(png, info);
      82              : 
      83            0 :         value.png_width = png_get_image_width(png, info);
      84            0 :         value.png_height = png_get_image_height(png, info);
      85            0 :         value.color_type = png_get_color_type(png, info);
      86            0 :         value.bit_depth = png_get_bit_depth(png, info);
      87              : 
      88            0 :         if (value.bit_depth == 16)
      89            0 :             png_set_strip_16(png);
      90              : 
      91            0 :         if (value.color_type == PNG_COLOR_TYPE_PALETTE)
      92            0 :             png_set_palette_to_rgb(png);
      93              : 
      94            0 :         if (value.color_type == PNG_COLOR_TYPE_GRAY && value.bit_depth < 8)
      95            0 :             png_set_expand_gray_1_2_4_to_8(png);
      96              : 
      97            0 :         if (png_get_valid(png, info, PNG_INFO_tRNS))
      98            0 :             png_set_tRNS_to_alpha(png);
      99              : 
     100            0 :         if (value.color_type == PNG_COLOR_TYPE_RGB ||
     101            0 :             value.color_type == PNG_COLOR_TYPE_GRAY ||
     102            0 :             value.color_type == PNG_COLOR_TYPE_PALETTE)
     103            0 :             png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
     104              : 
     105            0 :         if (value.color_type == PNG_COLOR_TYPE_GRAY ||
     106            0 :             value.color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
     107            0 :             png_set_gray_to_rgb(png);
     108              : 
     109            0 :         png_read_update_info(png, info);
     110              : 
     111            0 :         if (value.row_pointers) Util::Abort(INFO);
     112              : 
     113            0 :         value.row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * value.png_height);
     114            0 :         for (int y = 0; y < value.png_height; y++) {
     115            0 :             value.row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
     116              :         }
     117              : 
     118            0 :         png_read_image(png, value.row_pointers);
     119              : 
     120            0 :         fclose(fp);
     121              : 
     122            0 :         png_destroy_read_struct(&png, &info, NULL);
     123              : #else
     124              :         Util::Abort(INFO,"PNG is disabled");
     125              : #endif
     126              : 
     127            0 :     }
     128            0 :     void setDomain(Set::Vector &_lo, Set::Vector &_hi)
     129              :     {
     130              : #ifndef ALAMO_NOPNG
     131            0 :         domain_lo = _lo;
     132            0 :         domain_hi = _hi;
     133            0 :         domain_defined = false;
     134              : #endif
     135            0 :     }
     136              : 
     137              :     [[nodiscard]]
     138              :     std::array<int, 4> operator() (int I, int J)
     139              :     {
     140              : #ifndef ALAMO_NOPNG
     141              :         std::array<int, 4> ret;
     142              :         png_bytep px = &row_pointers[J][I * 4];
     143              :         ret = { px[0], px[1], px[2], px[3] };
     144              :         return ret;
     145              : #endif
     146              :     }
     147              : 
     148              :     [[nodiscard]]
     149            0 :     std::array<Set::Scalar, 4> operator()(Set::Vector &x)
     150              :     {
     151              : #ifndef ALAMO_NOPNG
     152            0 :         Util::Assert(INFO, TEST(row_pointers), "Running IC without initialization...");
     153              : 
     154              :         std::array<Set::Scalar, 4> ret;
     155              : 
     156            0 :         Set::Scalar img_width = (Set::Scalar)(png_width - 1);
     157            0 :         Set::Scalar img_height = (Set::Scalar)(png_height - 1);
     158            0 :         Set::Scalar img_dx = 1.0;
     159            0 :         Set::Scalar img_dy = 1.0;
     160              :         
     161              :         //Set::Scalar x, y;
     162            0 :         Set::Vector ximg;
     163              : 
     164            0 :         if (fit == Fit::Stretch)
     165              :         {
     166            0 :             ximg(0) = (x(0) - domain_lo(0)) / (domain_hi(0) - domain_lo(0));
     167            0 :             ximg(1) = (x(1) - domain_lo(1)) / (domain_hi(1) - domain_lo(1));
     168              :         }
     169            0 :         else if (fit == Fit::FitWidth)
     170              :         {
     171            0 :             Set::Scalar aspect_ratio = img_width / img_height;
     172            0 :             ximg(0) = (x(0) - domain_lo(0)) / (domain_hi(0) - domain_lo(0));
     173            0 :             ximg(1) = (x(1) - domain_lo(1)) / (domain_hi(1) - domain_lo(1));
     174            0 :             ximg(1) -= 0.5 - 0.5 / aspect_ratio;
     175            0 :             ximg(1) *= aspect_ratio;
     176              :         }
     177            0 :         else if (fit == Fit::FitHeight)
     178              :         {
     179            0 :             Set::Scalar aspect_ratio = img_height / img_width;
     180            0 :             ximg(0) = (x(0) - domain_lo(0)) / (domain_hi(0) - domain_lo(0));
     181            0 :             ximg(1) = (x(1) - domain_lo(1)) / (domain_hi(1) - domain_lo(1));
     182            0 :             ximg(0) -= 0.5 - 0.5 / aspect_ratio;
     183            0 :             ximg(0) *= aspect_ratio;
     184              :         }
     185            0 :         else if (fit == Fit::Coord)
     186              :         {
     187            0 :             ximg(0) = (x(0) - coord_lo(0)) / (coord_hi(0) - coord_lo(0));
     188            0 :             ximg(1) = (x(1) - coord_lo(1)) / (coord_hi(1) - coord_lo(1));
     189              :         }
     190              : 
     191              : 
     192              : 
     193            0 :         ximg(0) = std::min(ximg(0), 1.0); ximg(1) = std::min(ximg(1), 1.0);
     194            0 :         ximg(0) = std::max(ximg(0), 0.0); ximg(1) = std::max(ximg(1), 0.0);
     195              : 
     196            0 :         ximg(0) *= img_width;
     197            0 :         ximg(1) *= img_height;
     198              : 
     199            0 :         int I = (int)(ximg(0));
     200            0 :         int J = (int)(ximg(1));
     201              : 
     202            0 :         Set::Scalar x1 = I * img_dx, x2 = (I + 1) * img_dx;
     203            0 :         Set::Scalar y1 = J * img_dy, y2 = (J + 1) * img_dy;
     204              : 
     205              : 
     206            0 :         for (int channel = 0; channel < 4; channel++)
     207              :         {
     208              : 
     209            0 :             if (I > 0 && I < png_width - 1 &&
     210            0 :                 J>0 && J < png_height - 1)
     211              :             {
     212            0 :                 png_bytep px_sw = &(row_pointers[J][I * 4]);
     213            0 :                 png_bytep px_se = &(row_pointers[J][(I + 1) * 4]);
     214            0 :                 png_bytep px_nw = &(row_pointers[J + 1][I * 4]);
     215            0 :                 png_bytep px_ne = &(row_pointers[J + 1][(I + 1) * 4]);
     216              : 
     217              : 
     218            0 :                 Set::Scalar fQ11 = ((Set::Scalar)(px_sw[channel]) - min) / (max - min);
     219            0 :                 Set::Scalar fQ12 = ((Set::Scalar)(px_nw[channel]) - min) / (max - min);
     220            0 :                 Set::Scalar fQ21 = ((Set::Scalar)(px_se[channel]) - min) / (max - min);
     221            0 :                 Set::Scalar fQ22 = ((Set::Scalar)(px_ne[channel]) - min) / (max - min);
     222              : 
     223            0 :                 ret[channel] = (
     224            0 :                     fQ11 * (x2 - ximg(0)) * (y2 - ximg(1)) +
     225            0 :                     fQ21 * (ximg(0) - x1) * (y2 - ximg(1)) +
     226            0 :                     fQ12 * (x2 - ximg(0)) * (ximg(1) - y1) +
     227            0 :                     fQ22 * (ximg(0) - x1) * (ximg(1) - y1)) / (img_dx * img_dy);
     228              : 
     229            0 :             }
     230            0 :             else if ((I == 0 || I == png_width - 1) && J < png_height - 1)
     231              :             {
     232            0 :                 png_bytep px_sw = &(row_pointers[J][I * 4]);
     233            0 :                 png_bytep px_nw = &(row_pointers[J + 1][I * 4]);
     234              : 
     235            0 :                 Set::Scalar fQ11 = ((Set::Scalar)(px_sw[channel]) - min) / (max - min);
     236            0 :                 Set::Scalar fQ12 = ((Set::Scalar)(px_nw[channel]) - min) / (max - min);
     237            0 :                 ret[channel] = fQ11 + (fQ12 - fQ11) * (ximg(1) - y1);
     238            0 :             }
     239            0 :             else if (I < png_width - 1 && (J == 0 || J == png_height - 1))
     240              :             {
     241            0 :                 png_bytep px_sw = &(row_pointers[J][I * 4]);
     242            0 :                 png_bytep px_se = &(row_pointers[J][(I + 1) * 4]);
     243              : 
     244            0 :                 Set::Scalar fQ11 = ((Set::Scalar)(px_sw[channel]) - min) / (max - min);
     245            0 :                 Set::Scalar fQ21 = ((Set::Scalar)(px_se[channel]) - min) / (max - min);
     246            0 :                 ret[channel] = fQ11 + (fQ21 - fQ11) * (ximg(0) - x1);
     247            0 :             }
     248            0 :             else if (I == png_width - 1 && J == png_height - 1)
     249              :             {
     250            0 :                 png_bytep px_sw = &(row_pointers[J][I * 4]);
     251              : 
     252            0 :                 Set::Scalar fQ11 = ((Set::Scalar)(px_sw[channel]) - min) / (max - min);
     253            0 :                 ret[channel] = fQ11;
     254            0 :             }
     255              :             else
     256              :             {
     257            0 :                 ret[channel] = 0.0;
     258              :             }
     259              :         }
     260            0 :         return ret;
     261              : #endif
     262              :     }
     263              : 
     264              : private:
     265              : 
     266              : #ifndef ALAMO_NOPNG
     267              :     int png_width, png_height;
     268              :     png_byte color_type;
     269              :     png_byte bit_depth;
     270              :     png_bytep* row_pointers = NULL;
     271              :     Set::Vector coord_lo = Set::Vector::Zero();
     272              :     Set::Vector coord_hi = Set::Vector::Zero();
     273              : 
     274              :     //Util::BMP bmp;
     275              :     Fit fit = Fit::Stretch;
     276              :     Set::Scalar min = NAN, max = NAN;
     277              : 
     278              :     bool domain_defined = false;
     279              :     Set::Vector domain_lo = Set::Vector::Zero();
     280              :     Set::Vector domain_hi = Set::Vector::Zero();
     281              : #endif
     282              : 
     283              : };
     284              : 
     285              : }
        

Generated by: LCOV version 2.0-1