From d2c11a6c3de773483eca5d0c89cd51166dff7c3d Mon Sep 17 00:00:00 2001
From: Christophe Geuzaine <cgeuzaine@ulg.ac.be>
Date: Thu, 1 Sep 2016 16:25:04 +0000
Subject: [PATCH] fix for images with alpha channel

---
 Graphics/ReadImg.cpp | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/Graphics/ReadImg.cpp b/Graphics/ReadImg.cpp
index 731875a514..e46ebc2bd1 100644
--- a/Graphics/ReadImg.cpp
+++ b/Graphics/ReadImg.cpp
@@ -20,7 +20,7 @@
 // from an image, we create a post-procession view
 
 static PViewDataList *Img2Data(Fl_RGB_Image &img_init, int quads=1,
-                               int resizex=0, int resizey=0) 
+                               int resizex=0, int resizey=0)
 {
   img_init.desaturate(); // convert to grayscale
 
@@ -36,7 +36,7 @@ static PViewDataList *Img2Data(Fl_RGB_Image &img_init, int quads=1,
   int width = img->w();
   int dim = img->d();
 
-  if(dim != 1) {
+  if(dim != 1 && dim != 2) {
     Msg::Error("Unable to obtain one-channel image");
     return 0;
   }
@@ -52,12 +52,12 @@ static PViewDataList *Img2Data(Fl_RGB_Image &img_init, int quads=1,
     for(int j = 0; j < width - 1; j++) {
       double x = j;
       double x1 = j + 1;
-      double val1 = (double)a[j]/255.;
-      double val2 = (double)a1[j]/255.;
-      double val3 = (double)a1[j + 1]/255.;
-      double val4 = (double)a[j + 1]/255.;
+      double val1 = (double)a[j * dim]/255.;
+      double val2 = (double)a1[j * dim]/255.;
+      double val3 = (double)a1[(j + 1) * dim]/255.;
+      double val4 = (double)a[(j + 1) * dim]/255.;
       if(quads){ // generate quads
-        d->SQ.push_back(x); d->SQ.push_back(x); 
+        d->SQ.push_back(x); d->SQ.push_back(x);
         d->SQ.push_back(x1); d->SQ.push_back(x1);
         d->SQ.push_back(y); d->SQ.push_back(y1);
         d->SQ.push_back(y1); d->SQ.push_back(y);
@@ -112,25 +112,25 @@ static int EndPos(const char *name, PViewData *d)
   }
 }
 
-int read_pnm(std::string fileName) 
+int read_pnm(std::string fileName)
 {
   Fl_PNM_Image img(fileName.c_str());
   return EndPos(fileName.c_str(), Img2Data(img));
 }
 
-int read_jpeg(std::string fileName) 
+int read_jpeg(std::string fileName)
 {
   Fl_JPEG_Image img(fileName.c_str());
   return EndPos(fileName.c_str(), Img2Data(img));
 }
 
-int read_png(std::string fileName) 
+int read_png(std::string fileName)
 {
   Fl_PNG_Image img(fileName.c_str());
   return EndPos(fileName.c_str(), Img2Data(img));
 }
 
-int read_bmp(std::string fileName) 
+int read_bmp(std::string fileName)
 {
   Fl_BMP_Image img(fileName.c_str());
   return EndPos(fileName.c_str(), Img2Data(img));
-- 
GitLab