<commit_before>#emacs, this is -*-Python-*- mode
"""manipulate image codings"""
import numpy
import numpy as np
import warnings
cimport c_python
cimport c_numpy

cdef extern from "color_convert.h":
    ctypedef unsigned short u_int16_t
    ctypedef unsigned char u_int8_t
    ctypedef struct RGB888_t:
        unsigned char R
        unsigned char G
        unsigned char B
    ctypedef struct YUV444_t:
        unsigned char Y
        unsigned char U
        unsigned char V
    cdef RGB888_t YUV444toRGB888(unsigned char Y, unsigned char U, unsigned char V)
    cdef YUV444_t RGB888toYUV444(unsigned char r, unsigned char g, unsigned char b)
    cdef void mono16_buf_to_mono8_buf(u_int16_t *mono16_buf,
                                      u_int8_t *mono8_buf,
                                      int len)

# for PyArrayInterface
cdef int CONTIGUOUS
cdef int FORTRAN
cdef int ALIGNED
cdef int NOTSWAPPED
cdef int WRITEABLE

CONTIGUOUS= 0x01
FORTRAN= 0x02
ALIGNED= 0x100
NOTSWAPPED= 0x200
WRITEABLE= 0x400

ctypedef struct PyArrayInterface:
    int version                   # contains the integer 2 as a sanity check
    int nd                        # number of dimensions
    char typekind                 # kind in array --- character code of typestr
    int itemsize                  # size of each element
    int flags                     # flags indicating how the data should be interpreted
    c_python.Py_intptr_t *shape   # A length-nd array of shape information
    c_python.Py_intptr_t *strides # A length-nd array of stride information
    void *data                    # A pointer to the first element of the array

def rgb8_to_rgb8(arr,skip_check=False):
    rgb8 = numpy.asarray(arr) # view of data (if possible)
    s = rgb8.shape
    if len(s) == 2:
      height = s[0]
      width = s[1]/3
      rgb8.shape = ( height, width, 3)
      return rgb8
    else: return rgb8

def argb8_to_rgb8(arr,skip_check=False):
    argb8 = numpy.asarray(arr) # view of data (if possible)
    height,datawidth=arr.shape
    width=datawidth/4
    argb8.shape = ( height, width, 4)
    rgb8 = numpy.array(argb8[:,:,1:],copy=True) # make contiguous
    return rgb8

def mono8_to_rgb8(arr,skip_check=False):
    # convert to rgb8 by repeating each grayscale value for R,G,B
    cdef int i,j,k,height,width
    cdef char *rgb8_data_ptr, *mono8_data_ptr, *mono8_row_ptr
    cdef c_numpy.ndarray rgb8
    cdef PyArrayInterface* inter
    cdef int do_check

    do_check = not skip_check

    attr = arr.__array_struct__
    if not c_python.PyCObject_Check(attr):
        raise ValueError("invalid __array_struct__")

    inter = <PyArrayInterface*>c_python.PyCObject_AsVoidPtr(attr)
    if inter.version != 2:
        raise ValueError("invalid __array_struct__")

    if do_check:
        # TODO: don't really know what these flags all mean, figure out if
        # this is OK:
        if (inter.flags & (ALIGNED | WRITEABLE)) != (ALIGNED | WRITEABLE):
            raise ValueError("cannot handle misaligned or not writeable arrays.")

        if inter.nd != 2:
            raise ValueError("only 2D arrays are accepted (currently) mono8")

        if not (inter.typekind == "u"[0] and inter.itemsize==1):
            raise TypeError("must be uint8 arrays")

    height = inter.shape[0]
    width = inter.shape[1]

    rgb8 = numpy.zeros(( height, width, 3), numpy.uint8)

    rgb8_data_ptr = rgb8.data # we know rgb8 is contiguous
    c_python.Py_BEGIN_ALLOW_THREADS
    for i from 0<=i<height:
        mono8_row_ptr = <char*>inter.data + i*inter.strides[0]
        mono8_data_ptr = mono8_row_ptr
        for j from 0<=j<width:
            for k from 0<=k<3:
                rgb8_data_ptr[0] = mono8_data_ptr[0]
                rgb8_data_ptr=rgb8_data_ptr+1
            mono8_data_ptr = mono8_data_ptr + inter.strides[1]
    c_python.Py_END_ALLOW_THREADS

    return rgb8

def mono8_bayer_bggr_to_rgb8( arr ):
    # This is a super-crappy conversion I just hacked together.
    cdef int i,j,k,height,width
    cdef char *rgb8_data_ptr, *mono8_data_ptr, *mono8_row_ptr<commit_msg>bugfix: make compatible with Cython<commit_after>#emacs, this is -*-Python-*- mode
"""manipulate image codings"""
import numpy
import numpy as np
import warnings
cimport c_python
cimport c_numpy

cdef extern from "color_convert.h":
    ctypedef unsigned short u_int16_t
    ctypedef unsigned char u_int8_t
    ctypedef struct RGB888_t:
        unsigned char R
        unsigned char G
        unsigned char B
    ctypedef struct YUV444_t:
        unsigned char Y
        unsigned char U
        unsigned char V
    cdef RGB888_t YUV444toRGB888(unsigned char Y, unsigned char U, unsigned char V)
    cdef YUV444_t RGB888toYUV444(unsigned char r, unsigned char g, unsigned char b)
    cdef void mono16_buf_to_mono8_buf(u_int16_t *mono16_buf,
                                      u_int8_t *mono8_buf,
                                      int len)

# for PyArrayInterface
cdef int CONTIGUOUS
cdef int FORTRAN
cdef int ALIGNED
cdef int NOTSWAPPED
cdef int WRITEABLE

CONTIGUOUS= 0x01
FORTRAN= 0x02
ALIGNED= 0x100
NOTSWAPPED= 0x200
WRITEABLE= 0x400

ctypedef struct PyArrayInterface:
    int version                   # contains the integer 2 as a sanity check
    int nd                        # number of dimensions
    char typekind                 # kind in array --- character code of typestr
    int itemsize                  # size of each element
    int flags                     # flags indicating how the data should be interpreted
    c_python.Py_intptr_t *shape   # A length-nd array of shape information
    c_python.Py_intptr_t *strides # A length-nd array of stride information
    void *data                    # A pointer to the first element of the array

def rgb8_to_rgb8(arr,skip_check=False):
    rgb8 = numpy.asarray(arr) # view of data (if possible)
    s = rgb8.shape
    if len(s) == 2:
      height = s[0]
      width = s[1]/3
      rgb8.shape = ( height, width, 3)
      return rgb8
    else: return rgb8

def argb8_to_rgb8(arr,skip_check=False):
    argb8 = numpy.asarray(arr) # view of data (if possible)
    height,datawidth=arr.shape
    width=datawidth/4
    argb8.shape = ( height, width, 4)
    rgb8 = numpy.array(argb8[:,:,1:],copy=True) # make contiguous
    return rgb8

def mono8_to_rgb8(arr,skip_check=False):
    # convert to rgb8 by repeating each grayscale value for R,G,B
    cdef int i,j,k,height,width
    cdef char *rgb8_data_ptr, *mono8_data_ptr, *mono8_row_ptr
    cdef c_numpy.ndarray rgb8
    cdef PyArrayInterface* inter
    cdef int do_check

    do_check = not skip_check

    attr = arr.__array_struct__
    if not c_python.PyCObject_Check(attr):
        raise ValueError("invalid __array_struct__")

    inter = <PyArrayInterface*>c_python.PyCObject_AsVoidPtr(attr)
    if inter.version != 2:
        raise ValueError("invalid __array_struct__")

    if do_check:
        # TODO: don't really know what these flags all mean, figure out if
        # this is OK:
        if (inter.flags & (ALIGNED | WRITEABLE)) != (ALIGNED | WRITEABLE):
            raise ValueError("cannot handle misaligned or not writeable arrays.")

        if inter.nd != 2:
            raise ValueError("only 2D arrays are accepted (currently) mono8")

        if isinstance(inter.typekind,int):
            # Cython
            if not (inter.typekind == ord('u') and inter.itemsize==1):
                raise TypeError("must be uint8 arrays")
        else:
            # pyrex
            if not (inter.typekind == "u"[0] and inter.itemsize==1):
                raise TypeError("must be uint8 arrays")

    height = inter.shape[0]
    width = inter.shape[1]

    rgb8 = numpy.zeros(( height, width, 3), numpy.uint8)

    rgb8_data_ptr = rgb8.data # we know rgb8 is contiguous
    c_python.Py_BEGIN_ALLOW_THREADS
    for i from 0<=i<height:
        mono8_row_ptr = <char*>inter.data + i*inter.strides[0]
        mono8_data_ptr = mono8_row_ptr
        for j from 0<=j<width:
            for k from 0<=k<3:
                rgb8_data_ptr[0] = mono8_data_ptr[0]
                rgb8_data_ptr=rgb8_data_ptr+1
            mono8_data_ptr = mono8_data_ptr + inter.strides[1]
    c_python.Py_END_ALLOW_THREADS

    return rgb8

def mono8_bayer_bggr_to_rgb8( arr ):
    # This is a super-crappy conversion I just hacked together.
    cdef int i,j,k,height,width
    cdef char *rgb8_data_ptr, *mono8_data_ptr, *mono8_row_ptr
{'fraction_non_alphanumeric': 0.0748663101604278, 'fraction_numerical': 0.02947118241235888, 'mean_word_length': 2.55255382017729, 'pattern_counts': {'":': 2, '<': 19, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1592994', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2733, 'n_words': 811}