<commit_before>// ImageControl.cc for FbTk - Fluxbox Toolkit
// Copyright (c) 2001 - 2003 <NAME> (fluxbox at users.sourceforge.net)
//
// Image.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 <NAME> (bhughes at tcac.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: ImageControl.cc,v 1.9 2004/01/02 22:54:29 fluxgen Exp $

#include "ImageControl.hh"

#include "TextureRender.hh"
#include "App.hh"
#include "SimpleCommand.hh"

//use GNU extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif // HAVE_SYS_TYPES_H

#include <cstdlib>
#include <cstring>
#include <cstdio>

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif // HAVE_CTYPE_H

#include <iostream>

using namespace std;

namespace FbTk {

// lookup table for texture
unsigned long *ImageControl::sqrt_table = 0;
#ifdef TIMEDCACHE
bool ImageControl::s_timed_cache = true;
#else
bool ImageControl::s_timed_cache = false;
#endif // TIMEDCACHE

namespace { // anonymous

inline unsigned long bsqrt(unsigned long x) {
    if (x <= 0) return 0;
    if (x == 1) return 1;

    unsigned long r = x >> 1;
    unsigned long q;

    while (1) {
        q = x / r;
        if (q >= r) return r;
        r = (r + q) >> 1;
    }
}

}; // end anonymous namespace

ImageControl::ImageControl(int screen_num, bool dither,
                           int cpc, unsigned long cache_timeout, unsigned long cmax):
    m_dither(dither),
    m_colors(0),
    m_num_colors(0),
    m_colors_per_channel(cpc) {

    Display *disp = FbTk::App::instance()->display();

    m_screen_depth = DefaultDepth(disp, screen_num);
    m_screen_num = screen_num;
    m_root_window = RootWindow(disp, screen_num);
    m_visual = DefaultVisual(disp, screen_num);
    m_colormap = DefaultColormap(disp, screen_num);

    cache_max = cmax;

    if (cache_timeout && s_timed_cache) {
        m_timer.setTimeout(cache_timeout);
        RefCount<Command> clean_cache(new SimpleCommand<ImageControl>(*this, &ImageControl::cleanCache));
        m_timer.setCommand(clean_cache);
        m_timer.start();
    }
	
    createColorTable();
}


ImageControl::~ImageControl() {
    if (sqrt_table) {
        delete [] sqrt_table;
    }

    if (grad_xbuffer) {
        delete [] grad_xbuffer;
    }

    if (grad_ybuffer) {
        delete [] grad_ybuffer;
    }

    if (m_colors) {
        unsigned long *pixels = new unsigned long [m_num_colors];

        for (unsigned int color = 0; color < m_num_colors; color++)
            *(pixels + color) = (*(m_colors + color)).pixel;

        XFreeColors(FbTk::App::instance()->display(), m_colormap, pixels, m_num_colors, 0);

        delete [] m_colors;
    }

    if (cache.size() > 0) {
        CacheList::iterator it = cache.begin();
        CacheList::iterator it_end = cache.end();
        Display *disp = FbTk::App::instance()->display();
        for (; it != it_end; ++it) {
            XFreePixmap(disp, (*it)->pixmap);
            delete (*it);
        }

    }
}


Pixmap ImageControl::searchCache(unsigned int width, unsigned int height,
                                 const Texture &text) const {
    
    if (text.pixmap().drawable() != None) {
        // do comparsion with width/height and texture_pixmap
        CacheList::iterator it = cache.begin();
        CacheList::iterator it_end = cache.end();
        for (; it != it_end; ++it) {
            if ((*it)->texture_pixmap == text.pixmap().drawable() &&
                (*it)->width == width && (*it)->height == height) {
                (*it)->count++;
                return (*it)->pixmap;
            }
        }
        return None;
    }

    Cache tmp;
    tmp.texture_pixmap = text.pixmap().drawable();
    tmp.width = width;
    tmp.height = height;
    tmp.texture = text.type();
    tmp.pixel1 = text.color().pixel();
    tmp.pixel2 = text.colorTo().pixel();
    CacheList::iterator it = cache.find(&tmp);
    if (it == cache.end()) {
        return None;
    } else {
        (*it)->count++;
        return (*it)->pixmap;
    }

    return None;
    <commit_msg>check texture type too, it could be tiled<commit_after>// ImageControl.cc for FbTk - Fluxbox Toolkit
// Copyright (c) 2001 - 2003 <NAME> (fluxbox at users.sourceforge.net)
//
// Image.cc for Blackbox - an X11 Window manager
// Copyright (c) 1997 - 2000 <NAME> (bhughes at tcac.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: ImageControl.cc,v 1.10 2004/01/03 01:12:10 fluxgen Exp $

#include "ImageControl.hh"

#include "TextureRender.hh"
#include "App.hh"
#include "SimpleCommand.hh"

//use GNU extensions
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif // HAVE_SYS_TYPES_H

#include <cstdlib>
#include <cstring>
#include <cstdio>

#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif // HAVE_CTYPE_H

#include <iostream>

using namespace std;

namespace FbTk {

// lookup table for texture
unsigned long *ImageControl::sqrt_table = 0;
#ifdef TIMEDCACHE
bool ImageControl::s_timed_cache = true;
#else
bool ImageControl::s_timed_cache = false;
#endif // TIMEDCACHE

namespace { // anonymous

inline unsigned long bsqrt(unsigned long x) {
    if (x <= 0) return 0;
    if (x == 1) return 1;

    unsigned long r = x >> 1;
    unsigned long q;

    while (1) {
        q = x / r;
        if (q >= r) return r;
        r = (r + q) >> 1;
    }
}

}; // end anonymous namespace

ImageControl::ImageControl(int screen_num, bool dither,
                           int cpc, unsigned long cache_timeout, unsigned long cmax):
    m_dither(dither),
    m_colors(0),
    m_num_colors(0),
    m_colors_per_channel(cpc) {

    Display *disp = FbTk::App::instance()->display();

    m_screen_depth = DefaultDepth(disp, screen_num);
    m_screen_num = screen_num;
    m_root_window = RootWindow(disp, screen_num);
    m_visual = DefaultVisual(disp, screen_num);
    m_colormap = DefaultColormap(disp, screen_num);

    cache_max = cmax;

    if (cache_timeout && s_timed_cache) {
        m_timer.setTimeout(cache_timeout);
        RefCount<Command> clean_cache(new SimpleCommand<ImageControl>(*this, &ImageControl::cleanCache));
        m_timer.setCommand(clean_cache);
        m_timer.start();
    }
	
    createColorTable();
}


ImageControl::~ImageControl() {
    if (sqrt_table) {
        delete [] sqrt_table;
    }

    if (grad_xbuffer) {
        delete [] grad_xbuffer;
    }

    if (grad_ybuffer) {
        delete [] grad_ybuffer;
    }

    if (m_colors) {
        unsigned long *pixels = new unsigned long [m_num_colors];

        for (unsigned int color = 0; color < m_num_colors; color++)
            *(pixels + color) = (*(m_colors + color)).pixel;

        XFreeColors(FbTk::App::instance()->display(), m_colormap, pixels, m_num_colors, 0);

        delete [] m_colors;
    }

    if (cache.size() > 0) {
        CacheList::iterator it = cache.begin();
        CacheList::iterator it_end = cache.end();
        Display *disp = FbTk::App::instance()->display();
        for (; it != it_end; ++it) {
            XFreePixmap(disp, (*it)->pixmap);
            delete (*it);
        }

    }
}


Pixmap ImageControl::searchCache(unsigned int width, unsigned int height,
                                 const Texture &text) const {
    
    if (text.pixmap().drawable() != None) {
        // do comparsion with width/height and texture_pixmap
        CacheList::iterator it = cache.begin();
        CacheList::iterator it_end = cache.end();
        for (; it != it_end; ++it) {
            if ((*it)->texture_pixmap == text.pixmap().drawable() &&
                (*it)->width == width && (*it)->height == height &&
                (*it)->texture == text.type()) {
                (*it)->count++;
                return (*it)->pixmap;
            }
        }
        return None;
    }

    Cache tmp;
    tmp.texture_pixmap = text.pixmap().drawable();
    tmp.width = width;
    tmp.height = height;
    tmp.texture = text.type();
    tmp.pixel1 = text.color().pixel();
    tmp.pixel2 = text.colorTo().pixel();
    CacheList::iterator it = cache.find(&tmp);
    if (it == cache.end()) {
        return None;
    } else {
        (*it)->count++;
        return (*it)->pixmap;
    }

    return None;
    
{'fraction_non_alphanumeric': 0.1244023713903232, 'fraction_numerical': 0.00946643717728055, 'mean_word_length': 2.7949927431059507, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3592592', 'n_tokens_mistral': 3365, 'n_tokens_neox': 3178, 'n_words': 979}