title: How can I change the whole QImage pixel value using a color map?
body: <p>I'm looking for a way to change the value of the pixel by another one.
  My idea is to change a greyscale colour into a colour image using predefined colour maps like inferno found in csv format here (<a href="http://www.kennethmoreland.com/color-advice/" rel="nofollow noreferrer">http://www.kennethmoreland.com/color-advice/</a>).
  Firstly I get the value in the csv and store it as a Qvector then I tried to use it.</p>
  <p>I've seen that we can use setColorMap but it's not working.</p>
  <p>Here is a full reproducible example:</p>
  <ul>
  <li>need to download inferno256 <a href="http://%20%20%20%20full_image.setColorTable(COLOR_MAP.inferno);%20//%20COLOR_MAP%20is%20an%20attribute%20of%20type" rel="nofollow noreferrer">inferno color map</a> and change the PATH_INFERNO_COLORMAP in ColorMap.h</li>
  </ul>
  <p>ColorMap.h</p>
  <pre><code>#ifndef COLORMAP_H
  #define COLORMAP_H
  #include &lt;QDebug&gt;
  #include &lt;QFile&gt;
  #include &lt;QRgb&gt;
  #include &lt;QTextStream&gt;
  #include &lt;QVector&gt;
  
  const QString PATH_INFERNO_COLORMAP =
      &quot;~/Documents/colormap/inferno-table-byte-0256.csv&quot;;
  
  struct ColorMap {
    QVector&lt;QRgb&gt; inferno;
  
    void computeColorMap() {
      QFile file(PATH_INFERNO_COLORMAP);
      if (!file.open(QIODevice::ReadOnly)) {
        qDebug() &lt;&lt; &quot;ERROR: can't open inferno color map&quot;;
      }
  
      QTextStream in(&amp;file);
  
      while (!in.atEnd()) {
        QString line = in.readLine();
        QStringList values = line.split(&quot;,&quot;);
        inferno.append(
            qRgb(values[1].toInt(), values[2].toInt(), values[3].toInt()));
      }
  
      file.close();
    }
  };
  
  #endif // COLORMAP_H
  </code></pre>
  <p>image.h</p>
  <pre><code>#ifndef IMAGE_H
  #define IMAGE_H
  
  #include &quot;ColorMap.h&quot;
  #include &lt;QImage&gt;
  #include &lt;QPainter&gt;
  #include &lt;QQuickItem&gt;
  #include &lt;QQuickPaintedItem&gt;
  
  class Image : public QQuickPaintedItem {
    Q_OBJECT
    Q_PROPERTY(QImage image READ image WRITE setImage NOTIFY imageChanged)
  public:
    Image(QQuickItem *parent = nullptr);
    Q_INVOKABLE void setImage(const QImage &amp;image);
    Q_INVOKABLE void applyColorMap();
    void paint(QPainter *painter);
    QImage image() const;
  signals:
    void imageChanged();
  
  private:
    QImage current_image;
    ColorMap COLORMAP;
  };
  #endif // IMAGE_H
  </code></pre>
  <p>image.cpp</p>
  <pre><code>#include &quot;image.h&quot;
  #include &lt;QImage&gt;
  #include &lt;QJsonObject&gt;
  #include &lt;QNetworkAccessManager&gt;
  #include &lt;QNetworkReply&gt;
  #include &lt;QQmlApplicationEngine&gt;
  
  Image::Image(QQuickItem *parent) : QQuickPaintedItem(parent) {
    QJsonObject reponse;
    QEventLoop event_loop;
    QImage my_image;
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &amp;event_loop,
                     SLOT(quit()));
    QString full_url_request =
        &quot;https://external-content.duckduckgo.com/iu/&quot;
        &quot;?u=https%3A%2F%2Fhdwallsbox.com%2Fwallpapers%2Fl%2F750x1334%2F20%&quot;
        &quot;2Fabstract-patterns-grayscale-artwork-750x1334-19824.jpg&amp;f=1&amp;nofb=1&quot;;
    const QUrl url_img = QUrl(full_url_request);
    QNetworkRequest request(url_img);
    QNetworkReply *reply = manager-&gt;get(request);
    event_loop.exec();
    if (reply-&gt;isFinished() &amp;&amp; reply-&gt;error() == QNetworkReply::NoError) {
      my_image.loadFromData(reply-&gt;readAll());
      this-&gt;current_image = my_image;
      event_loop.exit();
      delete manager;
    } else {
      delete manager;
    }
  }
  
  void Image::paint(QPainter *painter) {
    QRectF bounding_rect = boundingRect();
    QImage scaled = this-&gt;current_image.scaledToHeight(bounding_rect.height());
    QPointF center = bounding_rect.center() - scaled.rect().center();
  
    if (center.x() &lt; 0)
      center.setX(0);
    if (center.y() &lt; 0)
      center.setY(0);
    painter-&gt;drawImage(center, scaled);
  }
  
  QImage Image::image() const { return this-&gt;current_image; }
  
  void Image::setImage(const QImage &amp;image) {
    this-&gt;current_image = image;
    update();
  }
  
  Q_INVOKABLE void Image::applyColorMap() {
    qDebug() &lt;&lt; &quot;apply_color_map&quot;;
    current_image.setColorTable(
        COLORMAP.inferno); // COLOR_MAP is an attribute of type
    update();
  }
  </code></pre>
  <p>main.cpp</p>
  <pre><code>#include &quot;image.h&quot;
  #include &lt;QGuiApplication&gt;
  #include &lt;QImage&gt;
  #include &lt;QJsonObject&gt;
  #include &lt;QNetworkAccessManager&gt;
  #include &lt;QNetworkReply&gt;
  #include &lt;QQmlApplicationEngine&gt;
  
  int main(int argc, char *argv[]) {
  #if QT_VERSION &lt; QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  #endif
  
    QGuiApplication app(argc, argv);
  
    qmlRegisterType&lt;Image&gt;(&quot;MyImage&quot;, 1, 0, &quot;MyImage&quot;);
  
    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral(&quot;qrc:/main.qml&quot;));
    QObject::connect(
        &amp;engine, &amp;QQmlApplicationEngine::objectCreated, &amp;app,
        [url](QObject *obj, const QUrl &amp;objUrl) {
          if (!obj &amp;&amp; url == objUrl)
            QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);
    engine.load(url);
  
    return app.exec();
  }
  </code></pre>
  <p>main.qml</p>
  <pre><code>import QtQuick 2.15
  import QtQuick.Window 2.15
  import MyImage 1.0
  import QtQuick.Controls 2.15
  import QtQuick.Extras 1.4
  
  Window {
      id: window
      width: 400
      height: 480
      visible: true
  
      Rectangle{
          id: background
          color: &quot;black&quot;
          width: parent.width
          height: parent.height
          anchors.fill: parent
      }
  
      Button {
          id: button
          text: qsTr(&quot;Apply color map&quot;)
          anchors.left: parent.horizontalCenter
          anchors.right: parent.horizontalCenter
          anchors.top: parent.top
          anchors.bottom: parent.bottom
          anchors.leftMargin: -99
          anchors.rightMargin: -99
          anchors.topMargin: 8
          anchors.bottomMargin: 432
          width : 40
          height: 20
  
          onClicked: {
              my_image.applyColorMap();
      }
      }
  
      Item {
          id: item1
          y: 67
          height: 413
          anchors.left: parent.left
          anchors.right: parent.right
          anchors.rightMargin: 0
          anchors.leftMargin: 0
  
          MyImage{
              id: my_image
              height: parent.height
              width: parent.width
          }
      }
  }
  </code></pre>