<commit_before>#!/usr/bin/env python
"""
Miscellaneous Routines.
"""
import struct
INF=2147483647 #from sys import maxint as INF #doesn't work anymore under Python3, but PDF still uses 32 bits ints

import six #Python 2+3 compatibility

##  PNG Predictor
##
def apply_png_predictor(pred, colors, columns, bitspercomponent, data):
    if bitspercomponent != 8:
        # unsupported
        raise ValueError(bitspercomponent)
    nbytes = colors*columns*bitspercomponent//8
    i = 0
    buf = b''
    line0 = b'\x00' * columns
    for i in range(0, len(data), nbytes+1):
        ft = data[i]
        i += 1
        line1 = data[i:i+nbytes]
        line2 = b''
        if ft == b'\x00':
            # PNG none
            line2 += line1
        elif ft == b'\x01':
            # PNG sub (UNTESTED)
            c = 0
            for b in line1:
                c = (c+ord(b)) & 255
                line2 += chr(c)
        elif ft == b'\x02':
            # PNG up
            for (a, b) in zip(line0, line1):
                c = (ord(a)+ord(b)) & 255
                line2 += chr(c)
        elif ft == b'\x03':
            # PNG average (UNTESTED)
            c = 0
            for (a, b) in zip(line0, line1):
                c = ((c+ord(a)+ord(b))//2) & 255
                line2 += chr(c)
        else:
            # unsupported
            raise ValueError(ft)
        buf += line2
        line0 = line2
    return buf


##  Matrix operations
##
MATRIX_IDENTITY = (1, 0, 0, 1, 0, 0)


def mult_matrix(m1, m0):
    (a1, b1, c1, d1, e1, f1) = m1
    (a0, b0, c0, d0, e0, f0) = m0
    """Returns the multiplication of two matrices."""
    return (a0*a1+c0*b1,    b0*a1+d0*b1,
            a0*c1+c0*d1,    b0*c1+d0*d1,
            a0*e1+c0*f1+e0, b0*e1+d0*f1+f0)


def translate_matrix(m, v):
    """Translates a matrix by (x, y)."""
    (a, b, c, d, e, f) = m
    (x, y) = v
    return (a, b, c, d, x*a+y*c+e, x*b+y*d+f)<commit_msg>apply_png_predictor py3<commit_after>#!/usr/bin/env python
"""
Miscellaneous Routines.
"""
import struct
INF=2147483647 #from sys import maxint as INF #doesn't work anymore under Python3, but PDF still uses 32 bits ints

import six #Python 2+3 compatibility

##  PNG Predictor
##
def apply_png_predictor(pred, colors, columns, bitspercomponent, data):
    if bitspercomponent != 8:
        # unsupported
        raise ValueError(bitspercomponent)
    nbytes = colors*columns*bitspercomponent//8
    i = 0
    buf = b''
    line0 = b'\x00' * columns
    for i in range(0, len(data), nbytes+1):
        ft = data[i]
        if six.PY2:
            ft = six.byte2int(ft)
        i += 1
        line1 = data[i:i+nbytes]
        line2 = b''
        if ft == 0:
            # PNG none
            line2 += line1
        elif ft == 1:
            # PNG sub (UNTESTED)
            c = 0
            for b in line1:
                if six.PY2:
                    b = six.byte2int(b)
                c = (c+b) & 255
                line2 += six.int2byte(c)
        elif ft == 2:
            # PNG up
            for (a, b) in zip(line0, line1):
                if six.PY2:
                    a, b = six.byte2int(a), six.byte2int(b)
                c = (a+b) & 255
                line2 += six.int2byte(c)
        elif ft == 3:
            # PNG average (UNTESTED)
            c = 0
            for (a, b) in zip(line0, line1):
                if six.PY2:
                    a, b = six.byte2int(a), six.byte2int(b)
                c = ((c+a+b)//2) & 255
                line2 += six.int2byte(c)
        else:
            # unsupported
            raise ValueError(ft)
        buf += line2
        line0 = line2
    return buf


##  Matrix operations
##
MATRIX_IDENTITY = (1, 0, 0, 1, 0, 0)


def mult_matrix(m1, m0):
    (a1, b1, c1, d1, e1, f1) = m1
    (a0, b0, c0, d0, e0, f0) = m0
    """Returns the multiplication of two matrices."""
    return (a0*a1+c0*b1,    b0*a1+d0*b1,
            a0*c1+c0*d1,    b0*c1+d0*d1,
            a0*e1+c0*f1+e0, b0*e1+d0*f1+f0)


def translate_matrix(m, v):
    """Translates a matrix by (x, y)."""
    (a, b, c, d, e, f) = m
    (x, y) = v
    return (a, b, c, d, x*a+y*c+e, x*b+y*d+f)
{'fraction_non_alphanumeric': 0.13923745173745175, 'fraction_numerical': 0.05405405405405406, 'mean_word_length': 1.6384468491406747, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1409757', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1548, 'n_words': 422}