[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/flothesof/matplotlib/master/src/_image.cpp [Back]  [Original]

/* -*- mode: c++; c-basic-offset: 4 -*- */

#define NO_IMPORT_ARRAY

#include 

#include "agg_color_rgba.h"
#include "agg_conv_transform.h"
#include "agg_image_accessors.h"
#include "agg_path_storage.h"
#include "agg_pixfmt_rgb.h"
#include "agg_pixfmt_rgba.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_rasterizer_sl_clip.h"
#include "agg_renderer_scanline.h"
#include "agg_rendering_buffer.h"
#include "agg_scanline_bin.h"
#include "agg_scanline_bin.h"
#include "agg_scanline_u.h"
#include "agg_span_allocator.h"
#include "agg_span_image_filter_rgb.h"
#include "agg_span_image_filter_rgba.h"
#include "agg_span_interpolator_linear.h"
#include "util/agg_color_conv_rgb8.h"

#include "_image.h"
#include "mplutils.h"
#include "agg_workaround.h"

typedef fixed_blender_rgba_plain fixed_blender_rgba32_plain;
typedef agg::pixfmt_alpha_blend_rgba pixfmt;
typedef fixed_blender_rgba_pre fixed_blender_rgba32_pre;
typedef agg::pixfmt_alpha_blend_rgba pixfmt_pre;
typedef agg::renderer_base renderer_base;
typedef agg::span_interpolator_linear interpolator_type;
typedef agg::rasterizer_scanline_aa rasterizer;

Image::Image()
    : bufferIn(NULL),
      rbufIn(NULL),
      colsIn(0),
      rowsIn(0),
      bufferOut(NULL),
      rbufOut(NULL),
      colsOut(0),
      rowsOut(0),
      BPP(4),
      interpolation(BILINEAR),
      aspect(ASPECT_FREE),
      bg(1, 1, 1, 0),
      resample(true)
{

}

Image::Image(unsigned numrows, unsigned numcols, bool isoutput)
    : bufferIn(NULL),
      rbufIn(NULL),
      colsIn(0),
      rowsIn(0),
      bufferOut(NULL),
      rbufOut(NULL),
      colsOut(0),
      rowsOut(0),
      BPP(4),
      interpolation(BILINEAR),
      aspect(ASPECT_FREE),
      bg(1, 1, 1, 0),
      resample(true)
{
    if (isoutput) {
        rowsOut = numrows;
        colsOut = numcols;
        unsigned NUMBYTES(numrows * numcols * BPP);
        bufferOut = new agg::int8u[NUMBYTES];
        rbufOut = new agg::rendering_buffer;
        rbufOut->attach(bufferOut, colsOut, rowsOut, colsOut * BPP);
    } else {
        rowsIn = numrows;
        colsIn = numcols;
        unsigned NUMBYTES(numrows * numcols * BPP);
        bufferIn = new agg::int8u[NUMBYTES];
        rbufIn = new agg::rendering_buffer;
        rbufIn->attach(bufferIn, colsIn, rowsIn, colsIn * BPP);
    }
}

Image::~Image()
{
    delete[] bufferIn;
    bufferIn = NULL;
    delete rbufIn;
    rbufIn = NULL;
    delete rbufOut;
    rbufOut = NULL;
    delete[] bufferOut;
    bufferOut = NULL;
}

void Image::apply_rotation(double r)
{
    agg::trans_affine M = agg::trans_affine_rotation(r * agg::pi / 180.0);
    srcMatrix *= M;
    imageMatrix *= M;
}

void Image::set_bg(double r, double g, double b, double a)
{
    bg.r = r;
    bg.g = g;
    bg.b = b;
    bg.a = a;
}

void Image::apply_scaling(double sx, double sy)
{
    agg::trans_affine M = agg::trans_affine_scaling(sx, sy);
    srcMatrix *= M;
    imageMatrix *= M;
}

void Image::apply_translation(double tx, double ty)
{
    agg::trans_affine M = agg::trans_affine_translation(tx, ty);
    srcMatrix *= M;
    imageMatrix *= M;
}

void Image::as_rgba_str(agg::int8u *outbuf)
{
    agg::rendering_buffer rb;
    rb.attach(outbuf, colsOut, rowsOut, colsOut * 4);
    rb.copy_from(*rbufOut);
}

void Image::color_conv(int format, agg::int8u *outbuf)
{
    int row_len = colsOut * 4;

    agg::rendering_buffer rtmp;
    rtmp.attach(outbuf, colsOut, rowsOut, row_len);

    switch (format) {
    case 0:
        agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_bgra32());
        break;
    case 1:
        agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_argb32());
        break;
    default:
        throw "Image::color_conv unknown format";
    }
}

void Image::reset_matrix(void)
{
    srcMatrix.reset();
    imageMatrix.reset();
}

void Image::resize(int numcols, int numrows, int norm, double radius)
{
    if (bufferIn == NULL) {
        throw "You must first load the image";
    }

    if (numcols stride()) < 0;
    size_t ind = 0;
    for (unsigned j = 0; j < im.rowsOut; j++) {
        if (isflip) {
            thisy = im.rowsOut - j + oy;
        } else {
            thisy = j + oy;
        }

        for (unsigned i = 0; i < im.colsOut; i++) {
            thisx = i + ox;

            if (thisx >= colsOut || thisy >= rowsOut) {
                ind += 4;
                continue;
            }

            pixfmt::color_type p;
            p.r = *(im.bufferOut + ind++);
            p.g = *(im.bufferOut + ind++);
            p.b = *(im.bufferOut + ind++);
            if (apply_alpha) {
                p.a = (pixfmt::value_type) * (im.bufferOut + ind++) * alpha;
            } else {
                p.a = *(im.bufferOut + ind++);
            }
            pixf.blend_pixel(thisx, thisy, p, 255);
        }
    }
}

// utilities for irregular grids
void _bin_indices_middle(
    unsigned int *irows, int nrows, const float *ys1, unsigned long ny, float dy, float y_min)
{
    int i, j, j_last;
    unsigned int *rowstart = irows;
    const float *ys2 = ys1 + 1;
    const float *yl = ys1 + ny;
    float yo = y_min + dy / 2.0;
    float ym = 0.5f * (*ys1 + *ys2);
    // y/rows
    j = 0;
    j_last = j;
    for (i = 0; i < nrows; i++, yo += dy, rowstart++) {
        while (ys2 != yl && yo > ym) {
            ys1 = ys2;
            ys2 = ys1 + 1;
            ym = 0.5f * (*ys1 + *ys2);
            j++;
        }
        *rowstart = j - j_last;
        j_last = j;
    }
}

void _bin_indices_middle_linear(float *arows,
                                unsigned int *irows,
                                int nrows,
                                const float *y,
                                unsigned long ny,
                                float dy,
                                float y_min)
{
    int i;
    int ii = 0;
    int iilast = (int)ny - 1;
    float sc = 1 / dy;
    int iy0 = (int)floor(sc * (y[ii] - y_min));
    int iy1 = (int)floor(sc * (y[ii + 1] - y_min));
    float invgap = 1.0f / (iy1 - iy0);
    for (i = 0; i < nrows && i  iy1 && ii < iilast) {
            ii++;
            iy0 = iy1;
            iy1 = (int)floor(sc * (y[ii + 1] - y_min));
            invgap = 1.0f / (iy1 - iy0);
        }
        if (i >= iy0 && i  0) {
        int ii = 0;
        int iilast = (int)ny - 1;
        int iy0 = (int)floor(sc * (y[ii] - offs));
        int iy1 = (int)floor(sc * (y[ii + 1] - offs));
        for (i = 0; i < nrows && i < iy0; i++) {
            irows[i] = -1;
        }
        for (; i < nrows; i++) {
            while (i > iy1 && ii < iilast) {
                ii++;
                iy0 = iy1;
                iy1 = (int)floor(sc * (y[ii + 1] - offs));
            }
            if (i >= iy0 && i  iy1 && ii > 1) {
                ii--;
                iy0 = iy1;
                iy1 = (int)floor(sc * (y[ii - 1] - offs));
            }
            if (i >= iy0 && i  0) {
        int ii = 0;
        int iilast = (int)ny - 1;
        int iy0 = (int)floor(sc * (y[ii] - offs));
        int iy1 = (int)floor(sc * (y[ii + 1] - offs));
        float invgap = 1.0 / (iy1 - iy0);
        for (i = 0; i < nrows && i < iy0; i++) {
            irows[i] = -1;
        }
        for (; i < nrows; i++) {
            while (i > iy1 && ii < iilast) {
                ii++;
                iy0 = iy1;
                iy1 = (int)floor(sc * (y[ii + 1] - offs));
                invgap = 1.0 / (iy1 - iy0);
            }
            if (i >= iy0 && i  iy1 && ii > 1) {
                ii--;
                iy0 = iy1;
                iy1 = (int)floor(sc * (y[ii - 1] - offs));
                invgap = 1.0 / (iy1 - iy0);
            }
            if (i >= iy0 && i 

Web Proxy Viewer  |  New URL  |  Original Page