⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qcolormap_x11.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qcolormap.h"#include "qapplication.h"#include "qdebug.h"#include "qdesktopwidget.h"#include "qvarlengtharray.h"#include "qx11info_x11.h"#include <private/qt_x11_p.h>#include <limits.h>class QColormapPrivate{public:    QColormapPrivate()        : ref(1), mode(QColormap::Direct), depth(0),          colormap(0), defaultColormap(true),          visual(0), defaultVisual(true),          r_max(0), g_max(0), b_max(0),          r_shift(0), g_shift(0), b_shift(0)    {}    QAtomic ref;    QColormap::Mode mode;    int depth;    Colormap colormap;    bool defaultColormap;    Visual *visual;    bool defaultVisual;    int r_max;    int g_max;    int b_max;    uint r_shift;    uint g_shift;    uint b_shift;    QVector<QColor> colors;    QVector<int> pixels;};static uint right_align(uint v){    while (!(v & 0x1))        v >>= 1;    return v;}static int lowest_bit(uint v){    int i;    uint b = 1u;    for (i = 0; ((v & b) == 0u) && i < 32;  ++i)        b <<= 1u;    return i == 32 ? -1 : i;}static int cube_root(int v){    if (v == 1)        return 1;    // brute force algorithm    int i = 1;    for (;;) {        const int b = i * i * i;        if (b <= v) {            ++i;        } else {            --i;            break;        }    }    return i;}static Visual *find_visual(Display *display,                           int screen,                           int visual_class,                           int visual_id,                           int *depth,                           bool *defaultVisual){    XVisualInfo *vi, rvi;    int count;    uint mask = VisualScreenMask;    rvi.screen = screen;    if (visual_class != -1) {        rvi.c_class = visual_class;        mask |= VisualClassMask;    }    if (visual_id != -1) {        rvi.visualid = visual_id;        mask |= VisualIDMask;    }    Visual *visual = DefaultVisual(display, screen);    *defaultVisual = true;    *depth = DefaultDepth(display, screen);    vi = XGetVisualInfo(display, mask, &rvi, &count);    if (vi) {        int best = 0;        for (int x = 0; x < count; ++x) {            if (vi[x].depth > vi[best].depth)                best = x;        }        if (best >= 0 && best <= count && vi[best].visualid != XVisualIDFromVisual(visual)) {            visual = vi[best].visual;            *defaultVisual = (visual == DefaultVisual(display, screen));            *depth = vi[best].depth;        }    }    if (vi)        XFree((char *)vi);    return visual;}static void query_colormap(QColormapPrivate *d, int screen){    Display *display = QX11Info::display();    // query existing colormap    int q_colors = (((1u << d->depth) > 256u) ? 256u : (1u << d->depth));    XColor queried[256];    memset(queried, 0, sizeof(queried));    for (int x = 0; x < q_colors; ++x)        queried[x].pixel = x;    XQueryColors(display, d->colormap, queried, q_colors);    d->colors.resize(q_colors);    for (int x = 0; x < q_colors; ++x) {        if (queried[x].red == 0            && queried[x].green == 0            && queried[x].blue == 0            && queried[x].pixel != BlackPixel(display, screen)) {            // unallocated color cell, skip it            continue;        }        d->colors[x] = QColor::fromRgbF(queried[x].red / float(USHRT_MAX),                                        queried[x].green / float(USHRT_MAX),                                        queried[x].blue / float(USHRT_MAX));    }    // for missing colors, find the closest color in the existing colormap    Q_ASSERT(d->pixels.size());    for (int x = 0; x < d->pixels.size(); ++x) {        if (d->pixels.at(x) != -1)            continue;        QRgb rgb;        if (d->mode == QColormap::Indexed) {            const int r = (x / (d->g_max * d->b_max)) % d->r_max;            const int g = (x / d->b_max) % d->g_max;            const int b = x % d->b_max;            rgb = qRgb((r * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1),                       (g * 0xff + (d->g_max - 1) / 2) / (d->g_max - 1),                       (b * 0xff + (d->b_max - 1) / 2) / (d->b_max - 1));        } else {            rgb = qRgb(x, x, x);        }        // find closest color        int mindist = INT_MAX, best = -1;        for (int y = 0; y < q_colors; ++y) {            int r =   qRed(rgb) - (queried[y].red   >> 8);            int g = qGreen(rgb) - (queried[y].green >> 8);            int b =  qBlue(rgb) - (queried[y].blue  >> 8);            int dist = (r * r) + (g * g) + (b * b);            if (dist < mindist) {                mindist = dist;                best = y;            }        }        Q_ASSERT(best >= 0 && best < q_colors);        if (d->visual->c_class & 1) {            XColor xcolor;            xcolor.red   = queried[best].red;            xcolor.green = queried[best].green;            xcolor.blue  = queried[best].blue;            xcolor.pixel = queried[best].pixel;            if (XAllocColor(display, d->colormap, &xcolor)) {                d->pixels[x] = xcolor.pixel;            } else {                // some weird stuff is going on...                d->pixels[x] = (qGray(rgb) < 127                                ? BlackPixel(display, screen)                                : WhitePixel(display, screen));            }        } else {            d->pixels[x] = best;        }    }}static void init_gray(QColormapPrivate *d, int screen){    d->pixels.resize(d->r_max);    for (int g = 0; g < d->g_max; ++g) {        const int gray = (g * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1);        const QRgb rgb = qRgb(gray, gray, gray);        d->pixels[g] = -1;        if (d->visual->c_class & 1) {            XColor xcolor;            xcolor.red   =   qRed(rgb) * 0x101;            xcolor.green = qGreen(rgb) * 0x101;            xcolor.blue  =  qBlue(rgb) * 0x101;            xcolor.pixel = 0ul;            if (XAllocColor(QX11Info::display(), d->colormap, &xcolor))                d->pixels[g] = xcolor.pixel;        }    }    query_colormap(d, screen);}static void init_indexed(QColormapPrivate *d, int screen){    d->pixels.resize(d->r_max * d->g_max * d->b_max);    // create color cube    for (int x = 0, r = 0; r < d->r_max; ++r) {        for (int g = 0; g < d->g_max; ++g) {            for (int b = 0; b < d->b_max; ++b, ++x) {                const QRgb rgb = qRgb((r * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1),                                      (g * 0xff + (d->g_max - 1) / 2) / (d->g_max - 1),                                      (b * 0xff + (d->b_max - 1) / 2) / (d->b_max - 1));                d->pixels[x] = -1;                if (d->visual->c_class & 1) {                    XColor xcolor;                    xcolor.red   =   qRed(rgb) * 0x101;                    xcolor.green = qGreen(rgb) * 0x101;                    xcolor.blue  =  qBlue(rgb) * 0x101;                    xcolor.pixel = 0ul;                    if (XAllocColor(QX11Info::display(), d->colormap, &xcolor))                        d->pixels[x] = xcolor.pixel;                }            }        }    }    query_colormap(d, screen);}static void init_direct(QColormapPrivate *d, bool ownColormap){    if (d->visual->c_class != DirectColor || !ownColormap)        return;    // preallocate 768 on the stack, so that we don't have to malloc    // for the common case (<= 24 bpp)    QVarLengthArray<XColor, 768> colorTable(d->r_max + d->g_max + d->b_max);    int i = 0;    for (int r = 0; r < d->r_max; ++r) {        colorTable[i].red = r << 8 | r;        colorTable[i].pixel = r << d->r_shift;        colorTable[i].flags = DoRed;        ++i;    }    for (int g = 0; g < d->g_max; ++g) {        colorTable[i].green = g << 8 | g;        colorTable[i].pixel = g << d->g_shift;        colorTable[i].flags = DoGreen;        ++i;    }    for (int b = 0; b < d->b_max; ++b) {        colorTable[i].blue = (b << 8 | b);        colorTable[i].pixel = b << d->b_shift;        colorTable[i].flags = DoBlue;        ++i;    }    XStoreColors(X11->display, d->colormap, colorTable.data(), colorTable.count());}/*!    \class QColormap    \ingroup multimedia    \brief The QColormap class maps device independent QColors to device dependent pixel values.*//*! \enum QColormap::Mode    This enum describes how QColormap maps device independent RGB    values to device dependent pixel values.    \value Direct Pixel values are derived directly from the RGB    values, also known as "True Color."    \value Indexed Pixel values represent indexes into a vector of    available colors, i.e. QColormap uses the index of the color that    most closely matches an RGB value.    \value Gray Similar to \c Indexed, pixel values represent a vector    of available gray tones.  QColormap uses the index of the gray    tone that most closely matches the computed gray tone of an RGB    value.*/static QColormap **cmaps = 0;/*! \internal*/void QColormap::initialize(){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -