    // Copyright (C) 2014  Davis E. King (davis@dlib.net)
    // License: Boost Software License   See LICENSE.txt for the full license.
    #ifndef DLIB_PYTHON_NuMPY_IMAGE_Hh_
    #define DLIB_PYTHON_NuMPY_IMAGE_Hh_

    #include "numpy.h"
    #include <dlib/pixel.h>
    #include <dlib/matrix.h>
    #include <dlib/array.h>

    // ----------------------------------------------------------------------------------------

    class numpy_gray_image
    {
    public:

        numpy_gray_image() : _data(0), _nr(0), _nc(0) {}
        numpy_gray_image (boost::python::object& img)
        {
            long shape[2];
            get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
            _nr = shape[0];
            _nc = shape[1];
        }

        friend inline long num_rows(const numpy_gray_image& img) { return img._nr; }
        friend inline long num_columns(const numpy_gray_image& img) { return img._nc; }
        friend inline void* image_data(numpy_gray_image& img) { return img._data; }
        friend inline const void* image_data(const numpy_gray_image& img) { return img._data; }
        friend inline long width_step(const numpy_gray_image& img) { return img._nc*sizeof(unsigned char); }

    private:

        unsigned char* _data;
        dlib::array<unsigned char> _contig_buf;
        long _nr;
        long _nc;
    };

    namespace dlib
    {
        template <>
        struct image_traits<numpy_gray_image >
        {
            typedef unsigned char pixel_type;
        };
    }

    // ----------------------------------------------------------------------------------------

    inline bool is_gray_python_image (boost::python::object& img)
    {
        try
        {
            long shape[2];
            get_numpy_ndarray_shape(img, shape);
            return true;
        }
        catch (dlib::error&)
        {
            return false;
        }
    }

    // ----------------------------------------------------------------------------------------

    class numpy_rgb_image
    {
    public:

        numpy_rgb_image() : _data(0), _nr(0), _nc(0) {}
        numpy_rgb_image (boost::python::object& img)
        {
            long shape[3];
            get_numpy_ndarray_parts(img, _data, _contig_buf, shape);
            _nr = shape[0];
            _nc = shape[1];
            if (shape[2] != 3)
                throw dlib::error("Error, python object is not a three band image and therefore can't be a RGB image.");
        }

        friend inline long num_rows(const numpy_rgb_image& img) { return img._nr; }
        friend inline long num_columns(const numpy_rgb_image& img) { return img._nc; }
        friend inline void* image_data(numpy_rgb_image& img) { return img._data; }
        friend inline const void* image_data(const numpy_rgb_image& img) { return img._data; }
        friend inline long width_step(const numpy_rgb_image& img) { return img._nc*sizeof(dlib::rgb_pixel); }

    private:

        dlib::rgb_pixel* _data;
        dlib::array<dlib::rgb_pixel> _contig_buf;
        long _nr;
        long _nc;
    };

    namespace dlib
    {
        template <>
        struct image_traits<numpy_rgb_image >
        {
            typedef rgb_pixel pixel_type;
        };
    }

    // ----------------------------------------------------------------------------------------

    inline bool is_rgb_python_image (boost::python::object& img)
    {
        try
        {
            long shape[3];
            get_numpy_ndarray_shape(img, shape);
            if (shape[2] == 3)
                return true;
            return false;
        }
        catch (dlib::error&)
        {
            return false;
        }
    }

    // ----------------------------------------------------------------------------------------

    #endif // DLIB_PYTHON_NuMPY_IMAGE_Hh_