<commit_before>//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "WrapHelper.h"

#include "../player/BoostPython.h"
#include "../player/BitmapManager.h"

#include "../graphics/Bitmap.h"
#include "../graphics/BitmapLoader.h"
#include "../graphics/FilterResizeBilinear.h"

#include "../base/CubicSpline.h"
#include "../base/GeomHelper.h"

#include "../glm/gtx/vector_angle.hpp"

#include <vector>
#include <sstream>

using namespace boost::python;
using namespace std;
using namespace avg;

namespace bp = boost::python;

template<class POINT>
class_<POINT> export_point(const string& sName)
{
    return class_<POINT>(sName.c_str(), no_init)
        .def("__len__", &Vec2Helper::len)
        .def("__getitem__", &Vec2Helper::getItem)
        .def("__str__", &Vec2Helper::str)
        .def("__repr__", &Vec2Helper::repr)
        .def("__hash__", &Vec2Helper::getHash)
        .def("getNormalized", &Vec2Helper::safeGetNormalized)
        .def("getNorm", &Vec2Helper::getNorm)
        .def("getRotated", &getRotated)
        .def("getRotated", &getRotatedPivot)
        .def(self == self)
        .def(self != self)
        .def(-self)
        .def(self + self)
        .def(self - self)
        .def(float() * self)
        .def(self * float())
        .def(self / float())
        .def("isInPolygon", &pointInPolygon)
        .def("getAngle", &getAngle)
        .def("fromPolar", &fromPolar)
        .staticmethod("fromPolar")
        .def("angle", &Vec2Helper::vecAngle)
        .staticmethod("angle")
    ;
}

struct Pixel32_to_python_tuple
{
    static PyObject* convert (avg::Pixel32 px)
    {
        return bp::incref(bp::make_tuple(
                px.getR(), px.getG(), px.getB(), px.getA()).ptr());
    }
};

static bp::object Bitmap_getPixels(Bitmap& bitmap, bool bCopyData=true)
{
    unsigned char* pBuffer = bitmap.getPixels();
    int buffSize = bitmap.getMemNeeded();
    if (bCopyData) {
        bp::object pyBuffer(handle<>(PyBuffer_New(buffSize)));
        void* pTargetBuffer;
        Py_ssize_t pyBuffSize  = buffSize;
        PyObject_AsWriteBuffer(pyBuffer.ptr(), &pTargetBuffer, &pyBuffSize);
        memcpy(pTargetBuffer, pBuffer, buffSize);
        return pyBuffer;
    } else {
#if PY_MAJOR_VERSION < 3
        return bp::object(handle<>(PyBuffer_FromMemory(pBuffer, buffSize)));
#else
        // TODO: This returns a memoryview and not a buffer, which will probably break
        // PIL interoperability.
        PyObject* py_memView = PyMemoryView_FromMemory((char*)pBuffer,
                buffSize, PyBUF_READ);
        return bp::object(handle<>(py_memView));
#endif
    }
}

BOOST_PYTHON_FUNCTION_OVERLOADS(Bitmap_getPixels_overloads, Bitmap_getPixels, 
        1, 2);


static void Bitmap_setPixels(Bitmap& bitmap, PyObject* exporter)
{
    Py_buffer bufferView;
//    PyObject_Print(PyObject_Type(exporter), stderr, 0);
    if (PyObject_CheckBuffer(exporter)) {
        PyObject_GetBuffer(exporter, &bufferView, PyBUF_READ);
        const unsigned char* pBuf = reinterpret_cast<unsigned char*>(bufferView.buf);
        bitmap.setPixels(pBuf);
        PyBuffer_Release(&bufferView);
    } else if (PyBuffer_Check(exporter)) {
        PyTypeObject * pType = exporter->ob_type;
        PyBufferProcs * pProcs = pType->tp_as_buffer;
        AVG_ASSERT(pProcs);
        Py_ssize_t numBytes;
        Py_ssize_t numSegments = pProcs->bf_getsegcount(exporter, &numBytes);
        if (numBytes != bitmap.getMemNeeded()) {
            throw Exception(AVG_ERR_INVALID_ARGS,
                    "Second parameter to Bitmap.setPixels must fit bitmap size.");
        }
        // TODO: check if bitmap size is correct
        unsigned char* pDestPixels = bitmap.getPixels();
        for (unsigned i=0; i<numSegments; ++i) {<commit_msg>Windows warning fix.<commit_after>//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 <NAME>
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "WrapHelper.h"

#include "../player/BoostPython.h"
#include "../player/BitmapManager.h"

#include "../graphics/Bitmap.h"
#include "../graphics/BitmapLoader.h"
#include "../graphics/FilterResizeBilinear.h"

#include "../base/CubicSpline.h"
#include "../base/GeomHelper.h"

#include "../glm/gtx/vector_angle.hpp"

#include <vector>
#include <sstream>

using namespace boost::python;
using namespace std;
using namespace avg;

namespace bp = boost::python;

template<class POINT>
class_<POINT> export_point(const string& sName)
{
    return class_<POINT>(sName.c_str(), no_init)
        .def("__len__", &Vec2Helper::len)
        .def("__getitem__", &Vec2Helper::getItem)
        .def("__str__", &Vec2Helper::str)
        .def("__repr__", &Vec2Helper::repr)
        .def("__hash__", &Vec2Helper::getHash)
        .def("getNormalized", &Vec2Helper::safeGetNormalized)
        .def("getNorm", &Vec2Helper::getNorm)
        .def("getRotated", &getRotated)
        .def("getRotated", &getRotatedPivot)
        .def(self == self)
        .def(self != self)
        .def(-self)
        .def(self + self)
        .def(self - self)
        .def(float() * self)
        .def(self * float())
        .def(self / float())
        .def("isInPolygon", &pointInPolygon)
        .def("getAngle", &getAngle)
        .def("fromPolar", &fromPolar)
        .staticmethod("fromPolar")
        .def("angle", &Vec2Helper::vecAngle)
        .staticmethod("angle")
    ;
}

struct Pixel32_to_python_tuple
{
    static PyObject* convert (avg::Pixel32 px)
    {
        return bp::incref(bp::make_tuple(
                px.getR(), px.getG(), px.getB(), px.getA()).ptr());
    }
};

static bp::object Bitmap_getPixels(Bitmap& bitmap, bool bCopyData=true)
{
    unsigned char* pBuffer = bitmap.getPixels();
    int buffSize = bitmap.getMemNeeded();
    if (bCopyData) {
        bp::object pyBuffer(handle<>(PyBuffer_New(buffSize)));
        void* pTargetBuffer;
        Py_ssize_t pyBuffSize  = buffSize;
        PyObject_AsWriteBuffer(pyBuffer.ptr(), &pTargetBuffer, &pyBuffSize);
        memcpy(pTargetBuffer, pBuffer, buffSize);
        return pyBuffer;
    } else {
#if PY_MAJOR_VERSION < 3
        return bp::object(handle<>(PyBuffer_FromMemory(pBuffer, buffSize)));
#else
        // TODO: This returns a memoryview and not a buffer, which will probably break
        // PIL interoperability.
        PyObject* py_memView = PyMemoryView_FromMemory((char*)pBuffer,
                buffSize, PyBUF_READ);
        return bp::object(handle<>(py_memView));
#endif
    }
}

BOOST_PYTHON_FUNCTION_OVERLOADS(Bitmap_getPixels_overloads, Bitmap_getPixels, 
        1, 2);


static void Bitmap_setPixels(Bitmap& bitmap, PyObject* exporter)
{
    Py_buffer bufferView;
//    PyObject_Print(PyObject_Type(exporter), stderr, 0);
    if (PyObject_CheckBuffer(exporter)) {
        PyObject_GetBuffer(exporter, &bufferView, PyBUF_READ);
        const unsigned char* pBuf = reinterpret_cast<unsigned char*>(bufferView.buf);
        bitmap.setPixels(pBuf);
        PyBuffer_Release(&bufferView);
    } else if (PyBuffer_Check(exporter)) {
        PyTypeObject * pType = exporter->ob_type;
        PyBufferProcs * pProcs = pType->tp_as_buffer;
        AVG_ASSERT(pProcs);
        Py_ssize_t numBytes;
        Py_ssize_t numSegments = pProcs->bf_getsegcount(exporter, &numBytes);
        if (numBytes != bitmap.getMemNeeded()) {
            throw Exception(AVG_ERR_INVALID_ARGS,
                    "Second parameter to Bitmap.setPixels must fit bitmap size.");
        }
        // TODO: check if bitmap size is correct
        unsigned char* pDestPixels = bitmap.getPixels();
        for (int i=0; i<numSegments; ++i) {
{'fraction_non_alphanumeric': 0.12630548302872063, 'fraction_numerical': 0.008703220191470844, 'mean_word_length': 3.0676991150442476, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3280535', 'n_tokens_mistral': 2951, 'n_tokens_neox': 2904, 'n_words': 732}