<commit_before>/*
 * openslide-python - Python bindings for the OpenSlide library
 *
 * Copyright (c) 2015 Carnegie Mellon University
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <Python.h>

typedef unsigned char u8;

#ifdef WORDS_BIGENDIAN
#define CA 0
#define CR 1
#define CG 2
#define CB 3
#else
#define CB 0
#define CG 1
#define CR 2
#define CA 3
#endif

static void
argb2rgba(u8 *buf, Py_ssize_t len)
{
    Py_ssize_t cur;

    for (cur = 0; cur < len; cur += 4) {
        u8 a = buf[cur + CA];
        u8 r = buf[cur + CR];
        u8 g = buf[cur + CG];
        u8 b = buf[cur + CB];
        if (a != 0 && a != 255) {
            r = r * 255 / a;
            g = g * 255 / a;
            b = b * 255 / a;
        }
        buf[cur + 0] = r;
        buf[cur + 1] = g;
        buf[cur + 2] = b;
        buf[cur + 3] = a;
    }
}

// Takes one argument: a contiguous buffer object.  Modifies it in place.
static PyObject *
_convert_argb2rgba(PyObject *self, PyObject *args)
{
    PyObject *ret = NULL;
    Py_buffer view;

    if (!PyArg_ParseTuple(args, "s*", &view))
        return NULL;
    if (!PyBuffer_IsContiguous(&view, 'A')) {
        PyErr_SetString(PyExc_ValueError, "Argument is not contiguous");
        goto DONE;
    }
    if (view.readonly) {
        PyErr_SetString(PyExc_ValueError, "Argument is not writable");
        goto DONE;
    }
    if (view.len % 4) {
        PyErr_SetString(PyExc_ValueError, "Argument has invalid size");
        goto DONE;
    }

    Py_BEGIN_ALLOW_THREADS
    argb2rgba(view.buf, view.len);
    Py_END_ALLOW_THREADS

    Py_INCREF(Py_None);
    ret = Py_None;

DONE:
    PyBuffer_Release(&view);
    return ret;
}
<commit_msg>convert: process pixels as u32; avoid writing back transparent pixels<commit_after>/*
 * openslide-python - Python bindings for the OpenSlide library
 *
 * Copyright (c) 2015 <NAME>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of version 2.1 of the GNU Lesser General Public License
 * as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <Python.h>

typedef unsigned char u8;

static void
argb2rgba(PY_UINT32_T *buf, Py_ssize_t len)
{
    Py_ssize_t cur;

    for (cur = 0; cur < len; cur++) {
        PY_UINT32_T val = buf[cur];
        u8 a = val >> 24;
        switch (a) {
        case 0:
            break;
        case 255:
            val = (val << 8) | a;
#ifndef WORDS_BIGENDIAN
            // compiler should optimize this to bswap
            val = (((val & 0x000000ff) << 24) |
                   ((val & 0x0000ff00) <<  8) |
                   ((val & 0x00ff0000) >>  8) |
                   ((val & 0xff000000) >> 24));
#endif
            buf[cur] = val;
            break;
        default:
            ; // label cannot point to a variable declaration
            u8 r = 255 * ((val >> 16) & 0xff) / a;
            u8 g = 255 * ((val >>  8) & 0xff) / a;
            u8 b = 255 * ((val >>  0) & 0xff) / a;
#ifdef WORDS_BIGENDIAN
            val = r << 24 | g << 16 | b << 8 | a;
#else
            val = a << 24 | b << 16 | g << 8 | r;
#endif
            buf[cur] = val;
            break;
        }
    }
}

// Takes one argument: a contiguous buffer object.  Modifies it in place.
static PyObject *
_convert_argb2rgba(PyObject *self, PyObject *args)
{
    PyObject *ret = NULL;
    Py_buffer view;

    if (!PyArg_ParseTuple(args, "s*", &view))
        return NULL;
    if (!PyBuffer_IsContiguous(&view, 'A')) {
        PyErr_SetString(PyExc_ValueError, "Argument is not contiguous");
        goto DONE;
    }
    if (view.readonly) {
        PyErr_SetString(PyExc_ValueError, "Argument is not writable");
        goto DONE;
    }
    if (view.len % 4) {
        PyErr_SetString(PyExc_ValueError, "Argument has invalid size");
        goto DONE;
    }
    if (view.itemsize != 4) {
        PyErr_SetString(PyExc_ValueError, "Argument has invalid item size");
        goto DONE;
    }

    Py_BEGIN_ALLOW_THREADS
    argb2rgba(view.buf, view.len / 4);
    Py_END_ALLOW_THREADS

    Py_INCREF(Py_None);
    ret = Py_None;

DONE:
    PyBuffer_Release(&view);
    return ret;
}

{'fraction_non_alphanumeric': 0.1021883920076118, 'fraction_numerical': 0.029686013320647003, 'mean_word_length': 2.3520408163265305, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7279531', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1681, 'n_words': 605}