File indexing completed on 2024-05-12 17:02:06

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2022 Xaver Hugl <xaver.hugl@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "colortransformation.h"
0010 #include "colorpipelinestage.h"
0011 
0012 #include <lcms2.h>
0013 
0014 #include "utils/common.h"
0015 
0016 namespace KWin
0017 {
0018 
0019 ColorTransformation::ColorTransformation(std::vector<std::unique_ptr<ColorPipelineStage>> &&stages)
0020     : m_pipeline(cmsPipelineAlloc(nullptr, 3, 3))
0021     , m_stages(std::move(stages))
0022 {
0023     if (!m_pipeline) {
0024         qCWarning(KWIN_CORE) << "Failed to allocate cmsPipeline!";
0025         m_valid = false;
0026         return;
0027     }
0028     for (auto &stage : m_stages) {
0029         if (!cmsPipelineInsertStage(m_pipeline, cmsAT_END, stage->stage())) {
0030             qCWarning(KWIN_CORE) << "Failed to insert cmsPipeline stage!";
0031             m_valid = false;
0032             return;
0033         }
0034     }
0035 }
0036 
0037 ColorTransformation::~ColorTransformation()
0038 {
0039     if (m_pipeline) {
0040         cmsStage *last = nullptr;
0041         do {
0042             cmsPipelineUnlinkStage(m_pipeline, cmsAT_END, &last);
0043         } while (last);
0044         cmsPipelineFree(m_pipeline);
0045     }
0046 }
0047 
0048 bool ColorTransformation::valid() const
0049 {
0050     return m_valid;
0051 }
0052 
0053 std::tuple<uint16_t, uint16_t, uint16_t> ColorTransformation::transform(uint16_t r, uint16_t g, uint16_t b) const
0054 {
0055     const uint16_t in[3] = {r, g, b};
0056     uint16_t out[3] = {0, 0, 0};
0057     cmsPipelineEval16(in, out, m_pipeline);
0058     return {out[0], out[1], out[2]};
0059 }
0060 
0061 }