//---Include---
#include "Image.h"

#ifdef WIN32
#    include <objidl.h>
#endif

#include "AlphaField.h"
#include "Border.h"
#include "FileSystem.h"
#include "Globals.h"
#include "MouseEvent.h"
#include "Scroll.h"
#include "Text.h"
#include "ToolTip.h"

#ifdef WIN32
#    include <gdiplus.h>
#    pragma comment(lib, "gdiplus.lib")
#endif

#ifndef WIN32
#    include <math.h>
#    include <stdlib.h>
#    include <string.h>
#    ifndef max
#        define max(a, b) (((a) > (b)) ? (a) : (b))
#    endif
#    ifndef min
#        define min(a, b) (((a) < (b)) ? (a) : (b))
#    endif
#endif

using namespace Framework;

// Contents of the Image class from Image.h
// Constructor
Image::Image(bool options)
    : ReferenceCounter(),
      fc(0),
      delFc(1),
      size(0, 0),
      drawOff(options ? new Point[2000] : new Point[1]),
      dPosA(options ? new Point[2000] : new Point[1]),
      dSizeA(options ? new Point[2000] : new Point[1]),
      doa(0),
      alpha(options ? new unsigned char[1000] : new unsigned char[1]),
      alphaCount(0),
      rend(0),
      alpha3D(0)
{
    alpha[0] = 0;
}

// Destructor
Image::~Image()
{
    if (delFc)
    {
        delete[] fc;
        fc = 0;
    }
    delete[] dPosA;
    delete[] dSizeA;
    delete[] alpha;
    delete[] drawOff;
}

// privat
inline void Image::alphaPixelP(int x, int y, int f)
{
    alphaPixelP(fc[x + y * size.x], f);
}

inline void Image::alphaPixelP3D(int x, int y, int f)
{
    alphaPixelP3D(fc[x + y * size.x], f);
}

inline void Image::alphaPixelAssozP(int& fc, int f)
{
    unsigned char* fc1 = (unsigned char*)&fc;
    unsigned char* fc2 = (unsigned char*)&f;
    unsigned char na = (unsigned char)~fc2[3];
    unsigned char a = (unsigned char)(fc2[3] + ((na * fc1[3]) >> 8));
    if (a == 0) return;
    fc1[2] = (unsigned char)((fc2[2] * fc2[3] + ((fc1[2] * na * fc1[3]) >> 8))
                             / a);
    fc1[1] = (unsigned char)((fc2[1] * fc2[3] + ((fc1[1] * na * fc1[3]) >> 8))
                             / a);
    fc1[0] = (unsigned char)((fc2[0] * fc2[3] + ((fc1[0] * na * fc1[3]) >> 8))
                             / a);
    fc1[3] = a;
}

inline void Image::alphaPixelP3D(int& fc, int colorb)
{
    int alpha = ((colorb >> 24) & 0xFF);
    int oldAlpha = ((fc >> 24) & 0xFF);
    bool newAlpha = alpha > oldAlpha;
    int na = (0x100 - alpha);
    fc = (((((na * (fc & 0xFF00FF)) >> 8)
               + ((alpha * (colorb & 0xFF00FF)) >> 8))
              & 0xFF00FF)
             | ((((na * (fc & 0x00FF00)) >> 8)
                    + ((alpha * (colorb & 0x00FF00)) >> 8))
                 & 0x00FF00)
             | ((colorb & 0xFF000000) * newAlpha)
             | ((fc & 0xFF000000) * !newAlpha))
           * (fc != 0)
       | (fc == 0) * colorb;
}

inline void Image::alphaPixelP(int& fc, int colorb)
{
    int alpha = ((colorb >> 24) & 0xFF);
    int na = (0x100 - alpha);
    fc = ((((na * (fc & 0xFF00FF)) >> 8) + ((alpha * (colorb & 0xFF00FF)) >> 8))
             & 0xFF00FF)
       | ((((na * (fc & 0x00FF00)) >> 8) + ((alpha * (colorb & 0x00FF00)) >> 8))
           & 0x00FF00)
       | ((fc & 0xFF000000));
}

char Image::getOutCode(Point p) const
{
    char ret = 0;
    if (p.x < dPosA[doa].x) ret = 1;
    if (p.x >= dSizeA[doa].x) ret = 2;
    if (p.y < dPosA[doa].y) ret |= 4;
    if (p.y >= dSizeA[doa].y) ret |= 8;
    return ret;
}

void Image::drawFlatTriangle(
    int y1, int y2, float m1, float b1, float m2, float b2, int color)
{
    const int yStart = max(y1, dPosA[doa].y);
    const int yEnd = min(y2, dSizeA[doa].y);
    for (int y = yStart; y < yEnd; y++)
    {
        const int xStart = max((int)(m1 * (float)y + b1 + 0.5f), dPosA[doa].x);
        const int xEnd = min((int)(m2 * (float)y + b2 + 0.5f), dSizeA[doa].x);
        for (int x = xStart; x < xEnd; x++)
            fc[x + y * size.x] = color;
    }
}

void Image::drawFlatTriangleTexture(int y1,
    int y2,
    double m1,
    double b1,
    double m2,
    double b2,
    double tx1,
    double ty1,
    double tx2,
    double ty2,
    double tx_1o,
    double ty_1o,
    double tx_2o,
    double ty_2o,
    double txf,
    double tyf,
    const Image& textur)
{
    const double yStart = max(y1, dPosA[doa].y);
    const double yEnd = min(y2, dSizeA[doa].y);
    double tx_1 = tx1 + tx_1o * (yStart - y1),
           ty_1 = ty1 + ty_1o * (yStart - y1),
           tx_2 = tx2 + tx_2o * (yStart - y1),
           ty_2 = ty2 + ty_2o * (yStart - y1);
    for (double y = yStart; y < yEnd;
        y++, tx_1 += tx_1o, ty_1 += ty_1o, tx_2 += tx_2o, ty_2 += ty_2o)
    {
        const double xStart = m1 * y + b1;
        const double xEnd = m2 * y + b2;
        drawLineHTexture(Vec2<double>(xStart, y),
            xEnd - xStart,
            Vec2<double>(tx_1, ty_1),
            Vec2<double>(tx_2, ty_2),
            txf,
            tyf,
            textur);
    }
}

void Image::drawFlatTriangleAlpha(
    int y1, int y2, float m1, float b1, float m2, float b2, int color)
{
    const int yStart = max((int)(y1 + 0.5), dPosA[doa].y);
    const int yEnd = min((int)(y2 + 0.5), dSizeA[doa].y);
    if (alpha3D)
    {
        for (int y = yStart; y < yEnd; y++)
        {
            const int xStart
                = max((int)(m1 * ((float)y + 0.5f) + b1 + 0.5f), dPosA[doa].x);
            const int xEnd
                = min((int)(m2 * ((float)y + 0.5) + b2 + 0.5f), dSizeA[doa].x);
            for (int x = xStart; x < xEnd; x++)
                alphaPixelP3D(fc[x + y * size.x], color);
        }
    }
    else
    {
        for (int y = yStart; y < yEnd; y++)
        {
            const int xStart
                = max((int)(m1 * ((float)y + 0.5f) + b1 + 0.5f), dPosA[doa].x);
            const int xEnd
                = min((int)(m2 * ((float)y + 0.5) + b2 + 0.5f), dSizeA[doa].x);
            for (int x = xStart; x < xEnd; x++)
                alphaPixelP(fc[x + y * size.x], color);
        }
    }
}

void Image::drawFlatTriangleTextureAlpha(int y1,
    int y2,
    double m1,
    double b1,
    double m2,
    double b2,
    double tx1,
    double ty1,
    double tx2,
    double ty2,
    double tx_1o,
    double ty_1o,
    double tx_2o,
    double ty_2o,
    double txf,
    double tyf,
    const Image& textur)
{
    const double yStart = max(y1, dPosA[doa].y);
    const double yEnd = min(y2, dSizeA[doa].y);
    double tx_1 = tx1 + tx_1o * (yStart - y1),
           ty_1 = ty1 + ty_1o * (yStart - y1),
           tx_2 = tx2 + tx_2o * (yStart - y1),
           ty_2 = ty2 + ty_2o * (yStart - y1);
    for (double y = yStart; y < yEnd;
        y++, tx_1 += tx_1o, ty_1 += ty_1o, tx_2 += tx_2o, ty_2 += ty_2o)
    {
        const double xStart = m1 * y + b1;
        const double xEnd = m2 * y + b2;
        drawLineHTextureAlpha(Vec2<double>(xStart, y),
            xEnd - xStart,
            Vec2<double>(tx_1, ty_1),
            Vec2<double>(tx_2, ty_2),
            txf,
            tyf,
            textur);
    }
}

void Image::drawLineHTexture(Vec2<double> p,
    double len,
    Vec2<double> ta,
    Vec2<double> tb,
    double txo,
    double tyo,
    const Image& textur) // draws a horizontal line
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawLineHTextureAlpha(p, len, ta, tb, txo, tyo, textur);
        return;
    }
    if (len < 0)
    {
        p.x += len;
        len = -len;
        ta.Swap(tb);
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    if (p.y < dpy || p.y >= dgy) return;
    double off = 0;
    if (p.x < dpx)
    {
        off = dpx - p.x;
        len -= dpx - p.x;
        if (len <= 0) return;
        p.x = dpx;
    }
    if (p.x + len >= dgx)
    {
        len -= p.x - dgx + len;
        if (len <= 0) return;
    }
    int br = size.x;
    int* fc = this->fc + (int)(p.x + (int)p.y * br);
    double x = ta.x + txo * off, y = ta.y + tyo * off;
    int* buffer = textur.getBuffer();
    int txtBr = textur.getWidth();
    for (int i = 0; i < len; ++i, ++fc)
    {
        *fc = buffer[(int)((int)(x + 0.5) + (int)(y + 0.5) * txtBr)];
        x += txo, y += tyo;
    }
    rend = 1;
}

void Image::drawLineHTextureAlpha(Vec2<double> p,
    double len,
    Vec2<double> ta,
    Vec2<double> tb,
    double txo,
    double tyo,
    const Image& textur) // draws a horizontal line
{
    if (alpha[alphaCount] == 0xFF) return;
    if (len < 0)
    {
        p.x += len;
        len = -len;
        ta.Swap(tb);
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    if (p.y < dpy || p.y >= dgy) return;
    double off = 0;
    if (p.x < dpx)
    {
        off = dpx - p.x;
        len -= dpx - p.x;
        if (len <= 0) return;
        p.x = dpx;
    }
    if (p.x + len >= dgx)
    {
        len -= p.x - dgx + len;
        if (len <= 0) return;
    }
    int br = size.x;
    int* fc = this->fc + (int)(p.x + (int)p.y * br);
    double x = ta.x + txo * off, y = ta.y + tyo * off;
    int* buffer = textur.getBuffer();
    int txtBr = textur.getWidth();
    int f;
    if (alpha3D)
    {
        for (int i = 0; i < len; ++i, ++fc)
        {
            f = buffer[(int)((int)(x + 0.5) + (int)(y + 0.5) * txtBr)];
            if (alpha[alphaCount])
            {
                unsigned char* cf = (unsigned char*)&f;
                cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                        * (cf[3] - alpha[alphaCount]));
            }
            alphaPixelP3D(*fc, f);
            x += txo, y += tyo;
        }
    }
    else
    {
        for (int i = 0; i < len; ++i, ++fc)
        {
            f = buffer[(int)((int)(x + 0.5) + (int)(y + 0.5) * txtBr)];
            if (alpha[alphaCount])
            {
                unsigned char* cf = (unsigned char*)&f;
                cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                        * (cf[3] - alpha[alphaCount]));
            }
            alphaPixelP(*fc, f);
            x += txo, y += tyo;
        }
    }
    rend = 1;
}

// non-constant

// Checks if a rectangle is fully or partially within the drawing area.
//  return 0 if the rectangle is not in the drawing area, 1 otherwise
bool Image::isAreaDrawable(int x, int y, int b, int h)
{
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + b < dpx || y + h < dpy || x >= dgx || y >= dgy) return 0;
    return 1;
}

// When this flag is set, during alpha blending if the previous
// color is 0, only the new one is copied with its alpha value. This is useful
// for use in the 3D screen, where the drawn image is later displayed using
// alpha blending
void Image::setAlpha3D(bool erlaubt)
{
    alpha3D = erlaubt;
}

void Image::setAlpha(
    unsigned char alpha) // sets the transparency for subsequent drawings
{
    int last = this->alpha[alphaCount];
    ++alphaCount;
    assert(alphaCount < 1000);
    this->alpha[alphaCount]
        = (unsigned char)((255 - alpha) > last ? (255 - alpha) : last);
}

void Image::releaseAlpha() // Releases alpha
{
    --alphaCount;
}

void Image::setPixelBuffer(int* buffer,
    bool deleteBuffer,
    int Width,
    int height) // sets the pointer to the image pixels
{
    if (delFc) delete[] fc;
    fc = buffer;
    delFc = deleteBuffer;
    size.x = Width;
    size.y = height;
    drawOff[0].x = 0;
    drawOff[0].y = 0;
    dPosA[0].x = 0;
    dPosA[0].y = 0;
    dSizeA[0] = size;
    alphaCount = 0;
    alpha[0] = 0;
    doa = 0;
    rend = 1;
}

void Image::newImage(int Width, int height, int fillColor)
{
    if (fc && delFc) delete[] fc;
    size.x = Width;
    size.y = height;
    fc = new int[size.x * size.y];
    setColor(fillColor);
    drawOff[0].x = 0;
    drawOff[0].y = 0;
    dPosA[0].x = 0;
    dPosA[0].y = 0;
    dSizeA[0] = size;
    alphaCount = 0;
    alpha[0] = 0;
    doa = 0;
    rend = 1;
}

void Image::setColor(int f)
{
    if ((f & 0xFF) == ((f >> 8) & 0xFF) && (f & 0xFF) == ((f >> 16) & 0xFF)
        && (f & 0xFF) == ((f >> 24) & 0xFF))
        memset(fc, f, size.x * size.y * 4);
    else
    {
        for (int *i = fc, *end = i + size.x * size.y; i < end; i++)
            *i = f;
    }
    rend = 1;
}

void Image::fillRegion(int x, int y, int b, int h, int ff)
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaRegion(x, y, b, h, ff);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + b < dpx || y + h < dpy || x >= dgx || y >= dgy) return;
    if (x < dpx)
    {
        b -= dpx - x;
        x = dpx;
    }
    if (y < dpy)
    {
        h -= dpy - y;
        y = dpy;
    }
    b = (x + b) >= dgx ? (dgx - x) : b;
    h = (y + h) >= dgy ? (dgy - y) : h;
    int* pixel = fc + y * size.x + x;
    int* rowEnd = pixel + b;
    for (int i = 0; i < h; pixel += size.x - b, ++i, rowEnd += size.x)
    {
        for (; pixel < rowEnd; ++pixel)
            *pixel = ff;
    }
    rend = 1;
}

void Image::alphaRegion(int x, int y, int b, int h, int ff)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + b < dpx || y + h < dpy || x >= dgx || y >= dgy) return;
    if (x < dpx)
    {
        b -= dpx - x;
        x = dpx;
    }
    if (y < dpy)
    {
        h -= dpy - y;
        y = dpy;
    }
    b = (x + b) >= dgx ? (dgx - x) : b;
    h = (y + h) >= dgy ? (dgy - y) : h;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&ff;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    int* pixel = fc + y * size.x + x;
    int* rowEnd = pixel + b;
    if (alpha3D)
    {
        for (int i = 0; i < h; pixel += size.x - b, ++i, rowEnd += size.x)
        {
            for (; pixel < rowEnd; ++pixel)
            {
                alphaPixelP3D(*pixel, ff);
            }
        }
    }
    else
    {
        for (int i = 0; i < h; pixel += size.x - b, ++i, rowEnd += size.x)
        {
            for (; pixel < rowEnd; ++pixel)
            {
                alphaPixelP(*pixel, ff);
            }
        }
    }
    rend = 1;
}

void Image::alphaPixel2D(int i, int f)
{
    if (!alpha[alphaCount])
        alphaPixelP(fc[i], f);
    else
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
        alphaPixelP(fc[i], f);
        rend = 1;
    }
}

void Image::alphaPixel3D(int i, int f)
{
    if (!alpha[alphaCount])
        alphaPixelP3D(fc[i], f);
    else
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
        alphaPixelP3D(fc[i], f);
        rend = 1;
    }
}

void Image::alphaPixel2D(int x, int y, int f)
{
    if (!alpha[alphaCount]) alphaPixelP(fc[x + y * size.x], f);
    if (alpha[alphaCount] < 0xFF)
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
        alphaPixelP(fc[x + y * size.x], f);
        rend = 1;
    }
}

void Image::alphaPixel3D(int x, int y, int f)
{
    if (!alpha[alphaCount]) alphaPixelP3D(fc[x + y * size.x], f);
    if (alpha[alphaCount] < 0xFF)
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
        alphaPixelP3D(fc[x + y * size.x], f);
        rend = 1;
    }
}

void Image::alphaPixelDP2D(int x, int y, int f)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x < dpx || y < dpy || x >= dgx || y >= dgy) return;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    alphaPixelP(fc[x + y * size.x], f);
    rend = 1;
}

void Image::alphaPixelDP3D(int x, int y, int f)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x < dpx || y < dpy || x >= dgx || y >= dgy) return;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    alphaPixelP3D(fc[x + y * size.x], f);
    rend = 1;
}

void Image::alphaPixelDP2D(int i, int f)
{
    int x = i % size.x;
    int y = i / size.x;
    alphaPixelDP2D(x, y, f);
    rend = 1;
}

void Image::alphaPixelDP3D(int i, int f)
{
    int x = i % size.x;
    int y = i / size.x;
    alphaPixelDP3D(x, y, f);
    rend = 1;
}

void Image::setPixelDP(int x, int y, int f)
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        if (alpha3D)
            alphaPixelDP3D(x, y, f);
        else
            alphaPixelDP2D(x, y, f);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x < dpx || y < dpy || x >= dgx || y >= dgy) return;
    fc[x + y * size.x] = f;
    rend = 1;
}

void Image::setPixelDP(int i, int f)
{
    int x = i % size.x;
    int y = i / size.x;
    setPixelDP(x, y, f);
    rend = 1;
}

void Image::drawLineH(int x, int y, int len, int f) // draws a horizontal line
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawLineHAlpha(x, y, len, f);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (y < dpy || y >= dgy) return;
    if (x < dpx)
    {
        len -= dpx - x;
        if (len <= 0) return;
        x = dpx;
    }
    if (x + len >= dgx)
    {
        len -= x - dgx + len;
        if (len <= 0) return;
    }
    int br = size.x;
    int* fc = this->fc + x + y * br;
    int pval = len < 0 ? -1 : 1;
    len = len > 0 ? len : -len;
    for (int i = 0; i < len; ++i, fc += pval)
        *fc = f;
    rend = 1;
}

void Image::drawLineV(int x, int y, int len, int f) // draws a vertical line
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawLineVAlpha(x, y, len, f);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x < dpx || x >= dgx) return;
    if (y < dpy)
    {
        len -= dpy - y;
        if (len <= 0) return;
        y = dpy;
    }
    if (y + len >= dgy)
    {
        len -= y - dgy + len;
        if (len < 0) return;
    }
    int br = size.x;
    int* fc = this->fc + x + y * br;
    int pval = len < 0 ? -br : br;
    len = len > 0 ? len : -len;
    for (int i = 0; i < len; ++i, fc += pval)
        *fc = f;
    rend = 1;
}

void Image::drawLineHAlpha(
    int x, int y, int len, int f) // draws a horizontal line
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (y < dpy || y >= dgy) return;
    if (x < dpx)
    {
        len -= dpx - x;
        if (len <= 0) return;
        x = dpx;
    }
    if (x + len >= dgx)
    {
        len -= x - dgx + len;
        if (len <= 0) return;
    }
    int br = size.x;
    int pval = len < 0 ? -1 : 1;
    len = len > 0 ? len : -len;
    int end = 0;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    if (alpha3D)
    {
        for (int i = x + y * br; end < len; ++end, i += pval)
        {
            alphaPixelP3D(fc[i], f);
        }
    }
    else
    {
        for (int i = x + y * br; end < len; ++end, i += pval)
        {
            alphaPixelP(fc[i], f);
        }
    }
    rend = 1;
}

void Image::drawLineVAlpha(
    int x, int y, int len, int f) // draws a vertical line
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x < dpx || x >= dgx) return;
    if (y < dpy)
    {
        len -= dpy - y;
        if (len <= 0) return;
        y = dpy;
    }
    if (y + len >= dgy)
    {
        len -= y - dgy + len;
        if (len < 0) return;
    }
    int br = size.x;
    int pval = len < 0 ? -br : br;
    len = len > 0 ? len : -len;
    int end = 0;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&f;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    if (alpha3D)
    {
        for (int i = x + y * br; end < len; ++end, i += pval)
        {
            alphaPixelP3D(fc[i], f);
        }
    }
    else
    {
        for (int i = x + y * br; end < len; ++end, i += pval)
        {
            alphaPixelP(fc[i], f);
        }
    }
    rend = 1;
}

void Image::drawLineBordered(Point a, Point b, int bc, int fc)
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawLineBorderedAlpha(a, b, bc, fc);
        return;
    }
    a += drawOff[doa];
    b += drawOff[doa];
    char outCode1 = getOutCode(a);
    char outCode2 = getOutCode(b);
    bool ok = 0;
    while (1)
    {
        int xMax = dSizeA[doa].x - 1;
        int yMax = dSizeA[doa].y - 1;
        if (!(outCode1 | outCode2))
        {
            ok = 1;
            break;
        }
        else if (outCode1 & outCode2)
            break;
        else
        {
            int x = 0, y = 0;
            char outCodeOut = outCode1 ? outCode1 : outCode2;
            if (outCodeOut & 8)
            {
                x = (int)(a.x + (b.x - a.x) * (yMax - a.y) / (b.y - a.y) + 0.5);
                y = yMax;
            }
            else if (outCodeOut & 4)
            {
                x = (int)(a.x + (b.x - a.x) * (dPosA[doa].y - a.y) / (b.y - a.y)
                          + 0.5);
                y = dPosA[doa].y;
            }
            else if (outCodeOut & 2)
            {
                y = (int)(a.y + (b.y - a.y) * (xMax - a.x) / (b.x - a.x) + 0.5);
                x = xMax;
            }
            else if (outCodeOut & 1)
            {
                y = (int)(a.y + (b.y - a.y) * (dPosA[doa].x - a.x) / (b.x - a.x)
                          + 0.5);
                x = dPosA[doa].x;
            }
            if (outCodeOut == outCode1)
            {
                a.x = x;
                a.y = y;
                outCode1 = getOutCode(a);
            }
            else
            {
                b.x = x;
                b.y = y;
                outCode2 = getOutCode(b);
            }
        }
    }
    if (ok)
    {
        int xlen = b.x - a.x, axlen = abs(xlen);
        int ylen = b.y - a.y, aylen = abs(ylen);
        double xf = (double)xlen / (aylen ? aylen : 1);
        double yf = (double)ylen / (axlen ? axlen : 1);
        if (axlen > aylen)
            xf = xf < 0 ? -1 : 1;
        else
            yf = yf < 0 ? -1 : 1;
        double x = (double)a.x, y = (double)a.y;
        int maxP = (int)(sqrt((float)(xlen * xlen + ylen * ylen)) + 0.5);
        int count = 0;
        int maxPixel = size.x * size.y;
        while (
            !((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y) && count < maxP)
        {
            ++count;
            this->fc[(int)((int)(x + 0.5) + (int)(y + 0.5) * size.x)] = fc;
            if ((int)((int)(x - 0.5) + (int)(y + 0.5) * size.x) < maxPixel
                && this->fc[(int)((int)(x - 0.5) + (int)(y + 0.5) * size.x)]
                       != fc)
                this->fc[(int)((int)(x - 0.5) + (int)(y + 0.5) * size.x)] = bc;
            if ((int)((int)(x + 1.5) + (int)(y + 0.5) * size.x) < maxPixel
                && this->fc[(int)((int)(x + 1.5) + (int)(y + 0.5) * size.x)]
                       != fc)
                this->fc[(int)((int)(x + 1.5) + (int)(y + 0.5) * size.x)] = bc;
            if ((int)((int)(x + 0.5) + (int)(y - 0.5) * size.x) < maxPixel
                && this->fc[(int)((int)(x + 0.5) + (int)(y - 0.5) * size.x)]
                       != fc)
                this->fc[(int)((int)(x + 0.5) + (int)(y - 0.5) * size.x)] = bc;
            if ((int)((int)(x + 0.5) + (int)(y + 1.5) * size.x) < maxPixel
                && this->fc[(int)((int)(x + 0.5) + (int)(y + 1.5) * size.x)]
                       != fc)
                this->fc[(int)((int)(x + 0.5) + (int)(y + 1.5) * size.x)] = bc;
            x += xf, y += yf;
        }
        rend = 1;
    }
}

void Image::drawLineBorderedAlpha(Point a, Point b, int bc, int fc)
{
    if (alpha[alphaCount] == 0xFF) return;
    a += drawOff[doa];
    b += drawOff[doa];
    char outCode1 = getOutCode(a);
    char outCode2 = getOutCode(b);
    bool ok = 0;
    while (1)
    {
        int xMax = dSizeA[doa].x - 1;
        int yMax = dSizeA[doa].y - 1;
        if (!(outCode1 | outCode2))
        {
            ok = 1;
            break;
        }
        else if (outCode1 & outCode2)
            break;
        else
        {
            int x = 0, y = 0;
            char outCodeOut = outCode1 ? outCode1 : outCode2;
            if (outCodeOut & 8)
            {
                x = (int)(a.x + (b.x - a.x) * (yMax - a.y) / (b.y - a.y) + 0.5);
                y = yMax;
            }
            else if (outCodeOut & 4)
            {
                x = (int)(a.x + (b.x - a.x) * (dPosA[doa].y - a.y) / (b.y - a.y)
                          + 0.5);
                y = dPosA[doa].y;
            }
            else if (outCodeOut & 2)
            {
                y = (int)(a.y + (b.y - a.y) * (xMax - a.x) / (b.x - a.x) + 0.5);
                x = xMax;
            }
            else if (outCodeOut & 1)
            {
                y = (int)(a.y + (b.y - a.y) * (dPosA[doa].x - a.x) / (b.x - a.x)
                          + 0.5);
                x = dPosA[doa].x;
            }
            if (outCodeOut == outCode1)
            {
                a.x = x;
                a.y = y;
                outCode1 = getOutCode(a);
            }
            else
            {
                b.x = x;
                b.y = y;
                outCode2 = getOutCode(b);
            }
        }
    }
    if (ok)
    {
        int xlen = b.x - a.x, axlen = abs(xlen);
        int ylen = b.y - a.y, aylen = abs(ylen);
        double xf = (double)xlen / (aylen ? aylen : 1);
        double yf = (double)ylen / (axlen ? axlen : 1);
        if (axlen > aylen)
            xf = xf < 0 ? -1 : 1;
        else
            yf = yf < 0 ? -1 : 1;
        double x = (double)a.x, y = (double)a.y;
        if (alpha[alphaCount])
        {
            unsigned char* cf = (unsigned char*)&fc;
            cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                    * (cf[3] - alpha[alphaCount]));
        }
        int maxP = (int)(sqrt((float)(xlen * xlen + ylen * ylen)) + 0.5);
        int count = 0;
        int maxPixel = size.x * size.y;
        if (alpha3D)
        {
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                if ((int)((int)(x - 0.5) + (int)(y + 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x - 0.5) + (int)(y + 0.5) * size.x];
                    alphaPixelP3D(pixel, bc);
                }
                if ((int)((int)(x + 1.5) + (int)(y + 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 1.5) + (int)(y + 0.5) * size.x];
                    alphaPixelP3D(pixel, bc);
                }
                if ((int)((int)(x + 0.5) + (int)(y - 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 0.5) + (int)(y - 0.5) * size.x];
                    alphaPixelP3D(pixel, bc);
                }
                if ((int)((int)(x + 0.5) + (int)(y + 1.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 0.5) + (int)(y + 1.5) * size.x];
                    alphaPixelP3D(pixel, bc);
                }
                x += xf, y += yf;
            }
            count = 0;
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
                alphaPixelP3D(pixel, fc);
                x += xf, y += yf;
            }
        }
        else
        {
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                if ((int)((int)(x - 0.5) + (int)(y + 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x - 0.5) + (int)(y + 0.5) * size.x];
                    alphaPixelP(pixel, bc);
                }
                if ((int)((int)(x + 1.5) + (int)(y + 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 1.5) + (int)(y + 0.5) * size.x];
                    alphaPixelP(pixel, bc);
                }
                if ((int)((int)(x + 0.5) + (int)(y - 0.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 0.5) + (int)(y - 0.5) * size.x];
                    alphaPixelP(pixel, bc);
                }
                if ((int)((int)(x + 0.5) + (int)(y + 1.5) * size.x) < maxPixel)
                {
                    int& pixel
                        = this->fc[(int)(x + 0.5) + (int)(y + 1.5) * size.x];
                    alphaPixelP(pixel, bc);
                }
                x += xf, y += yf;
            }
            count = 0;
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
                alphaPixelP(pixel, fc);
                x += xf, y += yf;
            }
        }
        rend = 1;
    }
}

void Image::drawLine(Point a,
    Point b,
    int fc) // draws a line from Point(x1, y1) to Point(x2, y2)
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawLineAlpha(a, b, fc);
        return;
    }
    a += drawOff[doa];
    b += drawOff[doa];
    char outCode1 = getOutCode(a);
    char outCode2 = getOutCode(b);
    bool ok = 0;
    while (1)
    {
        int xMax = dSizeA[doa].x - 1;
        int yMax = dSizeA[doa].y - 1;
        if (!(outCode1 | outCode2))
        {
            ok = 1;
            break;
        }
        else if (outCode1 & outCode2)
            break;
        else
        {
            int x = 0, y = 0;
            char outCodeOut = outCode1 ? outCode1 : outCode2;
            if (outCodeOut & 8)
            {
                x = (int)(a.x + (b.x - a.x) * (yMax - a.y) / (b.y - a.y) + 0.5);
                y = yMax;
            }
            else if (outCodeOut & 4)
            {
                x = (int)(a.x + (b.x - a.x) * (dPosA[doa].y - a.y) / (b.y - a.y)
                          + 0.5);
                y = dPosA[doa].y;
            }
            else if (outCodeOut & 2)
            {
                y = (int)(a.y + (b.y - a.y) * (xMax - a.x) / (b.x - a.x) + 0.5);
                x = xMax;
            }
            else if (outCodeOut & 1)
            {
                y = (int)(a.y + (b.y - a.y) * (dPosA[doa].x - a.x) / (b.x - a.x)
                          + 0.5);
                x = dPosA[doa].x;
            }
            if (outCodeOut == outCode1)
            {
                a.x = x;
                a.y = y;
                outCode1 = getOutCode(a);
            }
            else
            {
                b.x = x;
                b.y = y;
                outCode2 = getOutCode(b);
            }
        }
    }
    if (ok)
    {
        int xlen = b.x - a.x, axlen = abs(xlen);
        int ylen = b.y - a.y, aylen = abs(ylen);
        double xf = (double)xlen / (aylen ? aylen : 1);
        double yf = (double)ylen / (axlen ? axlen : 1);
        if (axlen > aylen)
            xf = xf < 0 ? -1 : 1;
        else
            yf = yf < 0 ? -1 : 1;
        double x = (double)a.x, y = (double)a.y;
        int maxP = (int)(sqrt((float)(xlen * xlen + ylen * ylen)) + 0.5);
        int count = 0;
        while (
            !((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y) && count < maxP)
        {
            ++count;
            this->fc[(int)((int)(x + 0.5) + (int)(y + 0.5) * size.x)] = fc;
            x += xf, y += yf;
        }
        this->fc[(int)((int)(x + 0.5) + (int)(y + 0.5) * size.x)] = fc;
        rend = 1;
    }
}

void Image::drawLineAlpha(Point a, Point b, int fc)
{
    if (alpha[alphaCount] == 0xFF) return;
    a += drawOff[doa];
    b += drawOff[doa];
    char outCode1 = getOutCode(a);
    char outCode2 = getOutCode(b);
    bool ok = 0;
    while (1)
    {
        int xMax = dSizeA[doa].x - 1;
        int yMax = dSizeA[doa].y - 1;
        if (!(outCode1 | outCode2))
        {
            ok = 1;
            break;
        }
        else if (outCode1 & outCode2)
            break;
        else
        {
            int x = 0, y = 0;
            char outCodeOut = outCode1 ? outCode1 : outCode2;
            if (outCodeOut & 8)
            {
                x = (int)(a.x + (b.x - a.x) * (yMax - a.y) / (b.y - a.y) + 0.5);
                y = yMax;
            }
            else if (outCodeOut & 4)
            {
                x = (int)(a.x + (b.x - a.x) * (dPosA[doa].y - a.y) / (b.y - a.y)
                          + 0.5);
                y = dPosA[doa].y;
            }
            else if (outCodeOut & 2)
            {
                y = (int)(a.y + (b.y - a.y) * (xMax - a.x) / (b.x - a.x) + 0.5);
                x = xMax;
            }
            else if (outCodeOut & 1)
            {
                y = (int)(a.y + (b.y - a.y) * (dPosA[doa].x - a.x) / (b.x - a.x)
                          + 0.5);
                x = dPosA[doa].x;
            }
            if (outCodeOut == outCode1)
            {
                a.x = x;
                a.y = y;
                outCode1 = getOutCode(a);
            }
            else
            {
                b.x = x;
                b.y = y;
                outCode2 = getOutCode(b);
            }
        }
    }
    if (ok)
    {
        int xlen = b.x - a.x, axlen = abs(xlen);
        int ylen = b.y - a.y, aylen = abs(ylen);
        double xf = (double)xlen / (aylen ? aylen : 1);
        double yf = (double)ylen / (axlen ? axlen : 1);
        if (axlen > aylen)
            xf = xf < 0 ? -1 : 1;
        else
            yf = yf < 0 ? -1 : 1;
        double x = (double)a.x, y = (double)a.y;
        if (alpha[alphaCount])
        {
            unsigned char* cf = (unsigned char*)&fc;
            cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                    * (cf[3] - alpha[alphaCount]));
        }
        int maxP = (int)(sqrt((float)(xlen * xlen + ylen * ylen)) + 0.5);
        int count = 0;
        if (alpha3D)
        {
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
                alphaPixelP3D(pixel, fc);
                x += xf, y += yf;
            }
            int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
            alphaPixelP3D(pixel, fc);
        }
        else
        {
            while (!((int)(x + 0.5) == b.x && (int)(y + 0.5) == b.y)
                   && count < maxP)
            {
                ++count;
                int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
                alphaPixelP(pixel, fc);
                x += xf, y += yf;
            }
            int& pixel = this->fc[(int)(x + 0.5) + (int)(y + 0.5) * size.x];
            alphaPixelP(pixel, fc);
        }
        rend = 1;
    }
}

void Image::fillCircle(int xOff,
    int yOff,
    int r,
    int fc) // fills a circle around Point(xOff, yOff) with radius r
{
    if (alpha[alphaCount] == 0xFF) return;
    for (int i = r; i > 0; i--)
        drawCircle(xOff, yOff, i, fc);
}

void Image::drawCircle(int xOff,
    int yOff,
    int r,
    int fc) // draws a circle around Point(xOff, yOff) with radius r
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawCircleAlpha(xOff, yOff, r, fc);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    xOff += drawOff[doa].x;
    yOff += drawOff[doa].y;
    if (xOff + r < dpx || xOff - r >= dgx || yOff + r < dpy || yOff - r >= dgy)
        return;
    for (int a = 0; a < r; ++a)
    {
        int b = (int)(sqrt((float)(long)(r * r - a * a)) + 0.5);
        if (xOff + a < dgx && xOff + a > dpx && yOff + b < dgy
            && yOff + b > dpy)
            this->fc[xOff + a + (yOff + b) * size.x] = fc;
        if (xOff - a < dgx && xOff - a > dpx && yOff + b < dgy
            && yOff + b > dpy)
            this->fc[xOff - a + (yOff + b) * size.x] = fc;
        if (xOff + a < dgx && xOff + a > dpx && yOff - b < dgy
            && yOff - b > dpy)
            this->fc[xOff + a + (yOff - b) * size.x] = fc;
        if (xOff - a < dgx && xOff - a > dpx && yOff - b < dgy
            && yOff - b > dpy)
            this->fc[xOff - a + (yOff - b) * size.x] = fc;
        if (xOff + b < dgx && xOff + b > dpx && yOff + a < dgy
            && yOff + a > dpy)
            this->fc[xOff + b + (yOff + a) * size.x] = fc;
        if (xOff - b < dgx && xOff - b > dpx && yOff + a < dgy
            && yOff + a > dpy)
            this->fc[xOff - b + (yOff + a) * size.x] = fc;
        if (xOff + b < dgx && xOff + b > dpx && yOff - a < dgy
            && yOff - a > dpy)
            this->fc[xOff + b + (yOff - a) * size.x] = fc;
        if (xOff - b < dgx && xOff - b > dpx && yOff - a < dgy
            && yOff - a > dpy)
            this->fc[xOff - b + (yOff - a) * size.x] = fc;
    }
    rend = 1;
}

void Image::drawCircleAlpha(int xOff, int yOff, int r, int fc)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    xOff += drawOff[doa].x;
    yOff += drawOff[doa].y;
    if (xOff + r < dpx || xOff - r >= dgx || yOff + r < dpy || yOff - r >= dgy)
        return;
    if (alpha[alphaCount] < 0xFF)
    {
        unsigned char* cf = (unsigned char*)&fc;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    if (alpha3D)
    {
        for (int a = 0; a < r; ++a)
        {
            int b = (int)(sqrt((float)(long)(r * r - a * a)) + 0.5);
            int* pixel = 0;
            if (xOff + a < dgx && xOff + a > dpx && yOff + b < dgy
                && yOff + b > dpy)
            {
                pixel = &this->fc[xOff + a + (yOff + b) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff - a < dgx && xOff - a > dpx && yOff + b < dgy
                && yOff + b > dpy)
            {
                pixel = &this->fc[xOff - a + (yOff + b) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff + a < dgx && xOff + a > dpx && yOff - b < dgy
                && yOff - b > dpy)
            {
                pixel = &this->fc[xOff + a + (yOff - b) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff - a < dgx && xOff - a > dpx && yOff - b < dgy
                && yOff - b > dpy)
            {
                pixel = &this->fc[xOff - a + (yOff - b) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff + b < dgx && xOff + b > dpx && yOff + a < dgy
                && yOff + a > dpy)
            {
                pixel = &this->fc[xOff + b + (yOff + a) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff - b < dgx && xOff - b > dpx && yOff + a < dgy
                && yOff + a > dpy)
            {
                pixel = &this->fc[xOff - b + (yOff + a) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff + b < dgx && xOff + b > dpx && yOff - a < dgy
                && yOff - a > dpy)
            {
                pixel = &this->fc[xOff + b + (yOff - a) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
            if (xOff - b < dgx && xOff - b > dpx && yOff - a < dgy
                && yOff - a > dpy)
            {
                pixel = &this->fc[xOff - b + (yOff - a) * size.x];
                alphaPixelP3D(*pixel, fc);
            }
        }
    }
    else
    {
        for (int a = 0; a < r; ++a)
        {
            int b = (int)(sqrt((float)(long)(r * r - a * a)) + 0.5);
            int* pixel = 0;
            if (xOff + a < dgx && xOff + a > dpx && yOff + b < dgy
                && yOff + b > dpy)
            {
                pixel = &this->fc[xOff + a + (yOff + b) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff - a < dgx && xOff - a > dpx && yOff + b < dgy
                && yOff + b > dpy)
            {
                pixel = &this->fc[xOff - a + (yOff + b) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff + a < dgx && xOff + a > dpx && yOff - b < dgy
                && yOff - b > dpy)
            {
                pixel = &this->fc[xOff + a + (yOff - b) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff - a < dgx && xOff - a > dpx && yOff - b < dgy
                && yOff - b > dpy)
            {
                pixel = &this->fc[xOff - a + (yOff - b) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff + b < dgx && xOff + b > dpx && yOff + a < dgy
                && yOff + a > dpy)
            {
                pixel = &this->fc[xOff + b + (yOff + a) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff - b < dgx && xOff - b > dpx && yOff + a < dgy
                && yOff + a > dpy)
            {
                pixel = &this->fc[xOff - b + (yOff + a) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff + b < dgx && xOff + b > dpx && yOff - a < dgy
                && yOff - a > dpy)
            {
                pixel = &this->fc[xOff + b + (yOff - a) * size.x];
                alphaPixelP(*pixel, fc);
            }
            if (xOff - b < dgx && xOff - b > dpx && yOff - a < dgy
                && yOff - a > dpy)
            {
                pixel = &this->fc[xOff - b + (yOff - a) * size.x];
                alphaPixelP(*pixel, fc);
            }
        }
    }
    rend = 1;
}

void Image::drawImage(
    int x, int y, int br, int hi, const Image& zImage) // draws zImage
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaImage(x, y, br, hi, zImage);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getWidth());
    hi = minInt(hi, zImage.getHeight());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int xx, ygr, ygr2;
    for (int yy = yst2; yy < dgy; ++yy)
    {
        ygr = yy * size.x;
        ygr2 = (yy - yst2 + yst) * bb;
        for (xx = xst2; xx < dgx; ++xx)
            fc[xx + ygr] = ff[(xx - xst2 + xst) + ygr2];
    }
    rend = 1;
}

void Image::alphaImageAssoc(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getWidth());
    hi = minInt(hi, zImage.getHeight());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    if (!alpha[alphaCount])
    {
        int xx, ygr, ygr2;
        for (int yy = yst2; yy < dgy; ++yy)
        {
            ygr = yy * size.x;
            ygr2 = (yy - yst2 + yst) * bb;
            for (xx = xst2; xx < dgx; ++xx)
                alphaPixelAssozP(fc[xx + ygr], ff[(xx - xst2 + xst) + ygr2]);
        }
    }
    else
    {
        int xx, ygr, ygr2;
        for (int yy = yst2; yy < dgy; ++yy)
        {
            ygr = yy * size.x;
            ygr2 = (yy - yst2 + yst) * bb;
            for (xx = xst2; xx < dgx; ++xx)
            {
                int fc = ff[(xx - xst2 + xst) + ygr2];
                unsigned char* cf = (unsigned char*)&fc;
                cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                        * (cf[3] - alpha[alphaCount]));
                alphaPixelAssozP(this->fc[xx + ygr], fc);
            }
        }
    }
    rend = 1;
}

void Image::alphaImage(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getWidth());
    hi = minInt(hi, zImage.getHeight());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    if (!alpha[alphaCount])
    {
        int xx, ygr, ygr2;
        if (alpha3D)
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ygr2 = (yy - yst2 + yst) * bb;
                int fci = xst2 + ygr;
                int ffi = xst + ygr2;
                for (xx = xst2; xx < dgx; ++xx, ++fci, ++ffi)
                    alphaPixelP3D(fc[fci], ff[ffi]);
            }
        }
        else
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ygr2 = (yy - yst2 + yst) * bb;
                int fci = xst2 + ygr;
                int ffi = xst + ygr2;
                for (xx = xst2; xx < dgx; ++xx, ++fci, ++ffi)
                    alphaPixelP(fc[fci], ff[ffi]);
            }
        }
    }
    else
    {
        int xx, ygr, ygr2;
        if (alpha3D)
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ygr2 = (yy - yst2 + yst) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                {
                    int fc = ff[(xx - xst2 + xst) + ygr2];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP3D(this->fc[xx + ygr], fc);
                }
            }
        }
        else
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ygr2 = (yy - yst2 + yst) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                {
                    int fc = ff[(xx - xst2 + xst) + ygr2];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP(this->fc[xx + ygr], fc);
                }
            }
        }
    }
    rend = 1;
}

void Image::drawImage90(int x,
    int y,
    int br,
    int hi,
    const Image& zImage) // Draws an image rotated 90 degrees to the right
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaImage90(x, y, br, hi, zImage);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + hi < dpx || y + br < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getHeight());
    hi = minInt(hi, zImage.getWidth());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int yy, xbb;
    for (int xx = xst2; xx < dgx; ++xx)
    {
        xbb = (zImage.getHeight() - (xx - xst2 + xst + 1)) * bb;
        for (yy = yst2; yy < dgy; ++yy)
            fc[xx + yy * size.x] = ff[(yy - yst2 + yst) + xbb];
    }
    rend = 1;
}

void Image::alphaImage90(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + hi < dpx || y + br < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getHeight());
    hi = minInt(hi, zImage.getWidth());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    if (!alpha[alphaCount])
    {
        int yy, xbb;
        if (alpha3D)
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (zImage.getHeight() - (xx - xst2 + xst + 1)) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                    alphaPixelP3D(xx, yy, ff[(yy - yst2 + yst) + xbb]);
            }
        }
        else
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (zImage.getHeight() - (xx - xst2 + xst + 1)) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                    alphaPixelP(xx, yy, ff[(yy - yst2 + yst) + xbb]);
            }
        }
    }
    else
    {
        int yy, xbb;
        if (alpha3D)
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (zImage.getHeight() - (xx - xst2 + xst + 1)) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                {
                    int fc = ff[(yy - yst2 + yst) + xbb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP3D(xx, yy, fc);
                }
            }
        }
        else
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (zImage.getHeight() - (xx - xst2 + xst + 1)) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                {
                    int fc = ff[(yy - yst2 + yst) + xbb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP(xx, yy, fc);
                }
            }
        }
    }
    rend = 1;
}

void Image::drawImage180(int x,
    int y,
    int br,
    int hi,
    const Image& zImage) // Draws an image rotated 180 degrees to the right
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaImage180(x, y, br, hi, zImage);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getWidth());
    hi = minInt(hi, zImage.getHeight());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int xx, ygr, ybb;
    for (int yy = yst2; yy < dgy; ++yy)
    {
        ygr = yy * size.x;
        ybb = (zImage.getHeight() - (yy - yst2 + yst + 1)) * bb;
        for (xx = xst2; xx < dgx; ++xx)
            fc[xx + ygr] = ff[(bb - (xx - xst2 + xst + 1)) + ybb];
    }
    rend = 1;
}

void Image::alphaImage180(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getHeight());
    hi = minInt(hi, zImage.getWidth());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    if (!alpha[alphaCount])
    {
        int xx, ygr, ybb;
        if (alpha3D)
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ybb = (zImage.getHeight() - (yy - yst2 + yst + 1)) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                    alphaPixelP3D(
                        fc[xx + ygr], ff[(bb - (xx - xst2 + xst + 1)) + ybb]);
            }
        }
        else
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ybb = (zImage.getHeight() - (yy - yst2 + yst + 1)) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                    alphaPixelP(
                        fc[xx + ygr], ff[(bb - (xx - xst2 + xst + 1)) + ybb]);
            }
        }
    }
    else
    {
        int xx, ygr, ybb;
        if (alpha3D)
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ybb = (zImage.getHeight() - (yy - yst2 + yst + 1)) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                {
                    int fc = ff[(bb - (xx - xst2 + xst + 1)) + ybb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP3D(this->fc[xx + ygr], fc);
                }
            }
        }
        else
        {
            for (int yy = yst2; yy < dgy; ++yy)
            {
                ygr = yy * size.x;
                ybb = (zImage.getHeight() - (yy - yst2 + yst + 1)) * bb;
                for (xx = xst2; xx < dgx; ++xx)
                {
                    int fc = ff[(bb - (xx - xst2 + xst + 1)) + ybb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP(this->fc[xx + ygr], fc);
                }
            }
        }
    }
    rend = 1;
}

void Image::drawImage270(int x,
    int y,
    int br,
    int hi,
    const Image& zImage) // Draws an image rotated 270 degrees to the right
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaImage270(x, y, br, hi, zImage);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + hi < dpx || y + br < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getHeight());
    hi = minInt(hi, zImage.getWidth());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int yy, xbb;
    for (int xx = xst2; xx < dgx; ++xx)
    {
        xbb = (xx - xst2 + xst) * bb;
        for (yy = yst2; yy < dgy; ++yy)
            fc[xx + yy * size.x] = ff[(bb - (yy - yst2 + yst + 1)) + xbb];
    }
    rend = 1;
}

void Image::alphaImage270(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + hi < dpx || y + br < dpy || x >= dgx || y >= dgy) return;
    br = minInt(br, zImage.getHeight());
    hi = minInt(hi, zImage.getWidth());
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    if (!alpha[alphaCount])
    {
        int yy, xbb;
        if (alpha3D)
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (xx - xst2 + xst) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                    alphaPixelP3D(
                        xx, yy, ff[(bb - (yy - yst2 + yst + 1)) + xbb]);
            }
        }
        else
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (xx - xst2 + xst) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                    alphaPixelP(xx, yy, ff[(bb - (yy - yst2 + yst + 1)) + xbb]);
            }
        }
    }
    else
    {
        int yy, xbb;
        if (alpha3D)
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (xx - xst2 + xst) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                {
                    int fc = ff[(bb - (yy - yst2 + yst + 1)) + xbb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP3D(xx, yy, fc);
                }
            }
        }
        else
        {
            for (int xx = xst2; xx < dgx; ++xx)
            {
                xbb = (xx - xst2 + xst) * bb;
                for (yy = yst2; yy < dgy; ++yy)
                {
                    int fc = ff[(bb - (yy - yst2 + yst + 1)) + xbb];
                    unsigned char* cf = (unsigned char*)&fc;
                    cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                            * (cf[3] - alpha[alphaCount]));
                    alphaPixelP(xx, yy, fc);
                }
            }
        }
    }
    rend = 1;
}

void Image::drawImageScaled(
    int x, int y, int br, int hi, const Image& zImage) // draws zImage scaled
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        alphaImageScaled(x, y, br, hi, zImage);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    double xo = zImage.getWidth() / (double)br;
    double yo = zImage.getHeight() / (double)hi;
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int xx, ygr, ygr2;
    double xb = 0, yb = yst * yo;
    for (int yy = yst2; yy < dgy; ++yy, yb += yo)
    {
        ygr = yy * size.x;
        ygr2 = (int)((yy - yst2 + yst) * yo) * bb;
        for (xx = xst2, xb = xst * xo; xx < dgx; ++xx, xb += xo)
            fc[xx + ygr] = ff[(int)xb + ygr2];
    }
    rend = 1;
}

void Image::alphaImageScaled(int x, int y, int br, int hi, const Image& zImage)
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    x += drawOff[doa].x;
    y += drawOff[doa].y;
    if (x + br < dpx || y + hi < dpy || x >= dgx || y >= dgy) return;
    double xo = zImage.getWidth() / (double)br;
    double yo = zImage.getHeight() / (double)hi;
    int xst = maxInt(dpx - x, 0);
    int yst = maxInt(dpy - y, 0);
    int xst2 = maxInt(x, dpx);
    int yst2 = maxInt(y, dpy);
    dgx = minInt(x + br, dgx);
    dgy = minInt(y + hi, dgy);
    int bb = zImage.getWidth();
    int* ff = zImage.getBuffer();
    int xx, ygr, ygr2;
    double xb = 0;
    if (alpha3D)
    {
        for (int yy = yst2; yy < dgy; ++yy)
        {
            ygr = yy * size.x;
            ygr2 = (int)((yy - yst2 + yst) * yo) * bb;
            for (xx = xst2, xb = xst * xo; xx < dgx; ++xx, xb += xo)
            {
                int f = ff[(int)xb + ygr2];
                unsigned char* cf = (unsigned char*)&f;
                cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                        * (cf[3] - alpha[alphaCount]));
                alphaPixelP3D(fc[xx + ygr], f);
            }
        }
    }
    else
    {
        for (int yy = yst2; yy < dgy; ++yy)
        {
            ygr = yy * size.x;
            ygr2 = (int)((yy - yst2 + yst) * yo) * bb;
            for (xx = xst2, xb = xst * xo; xx < dgx; ++xx, xb += xo)
            {
                int f = ff[(int)xb + ygr2];
                unsigned char* cf = (unsigned char*)&f;
                cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                        * (cf[3] - alpha[alphaCount]));
                alphaPixelP(fc[xx + ygr], f);
            }
        }
    }
    rend = 1;
}

void Image::drawTriangle(
    Point a, Point b, Point c, int color) // fills a triangle
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawTriangleAlpha(a, b, c, color);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    a += drawOff[doa];
    b += drawOff[doa];
    c += drawOff[doa];
    if ((a.x < dpx && b.x < dpx && c.x < dpx)
        || (a.y < dpy && b.y < dpy && c.y < dpy)
        || (a.x >= dgx && b.x >= dgx && c.x >= dgx)
        || (a.y >= dgy && b.y >= dgy && c.y >= dgy))
        return;
    if (b.y < a.y) a.Swap(b);
    if (c.y < b.y) b.Swap(c);
    if (b.y < a.y) a.Swap(b);
    if (a.y == b.y)
    {
        if (b.x < a.x) a.Swap(b);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        const float m3 = (float)(b.x - c.x) / (float)(b.y - c.y);
        float b2 = (float)a.x - m2 * (float)a.y;
        float b3 = (float)b.x - m3 * (float)b.y;
        drawFlatTriangle(b.y, c.y, m2, b2, m3, b3, color);
    }
    else if (b.y == c.y)
    {
        if (c.x < b.x) b.Swap(c);
        const float m1 = (float)(a.x - b.x) / (float)(a.y - b.y);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        float b1 = (float)a.x - m1 * (float)a.y;
        float b2 = (float)a.x - m2 * (float)a.y;
        drawFlatTriangle(a.y, b.y, m1, b1, m2, b2, color);
    }
    else
    {
        const float m1 = (float)(a.x - b.x) / (float)(a.y - b.y);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        const float m3 = (float)(b.x - c.x) / (float)(b.y - c.y);
        float b1 = (float)a.x - m1 * (float)a.y;
        float b2 = (float)a.x - m2 * (float)a.y;
        float b3 = (float)b.x - m3 * (float)b.y;
        const float qx = m2 * (float)b.y + b2;
        if (qx < (float)b.x)
        {
            drawFlatTriangle(a.y, b.y, m2, b2, m1, b1, color);
            drawFlatTriangle(b.y, c.y, m2, b2, m3, b3, color);
        }
        else
        {
            drawFlatTriangle(a.y, b.y, m1, b1, m2, b2, color);
            drawFlatTriangle(b.y, c.y, m3, b3, m2, b2, color);
        }
    }
    rend = 1;
}

void Image::drawTriangleTexture(Point a,
    Point b,
    Point c,
    Point ta,
    Point tb,
    Point tc,
    const Image& textur) // fills a triangle
{
    if (alpha[alphaCount] == 0xFF) return;
    if (alpha[alphaCount])
    {
        drawTriangleTextureAlpha(a, b, c, ta, tb, tc, textur);
        return;
    }
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    a += drawOff[doa];
    b += drawOff[doa];
    c += drawOff[doa];
    if ((a.x < dpx && b.x < dpx && c.x < dpx)
        || (a.y < dpy && b.y < dpy && c.y < dpy)
        || (a.x >= dgx && b.x >= dgx && c.x >= dgx)
        || (a.y >= dgy && b.y >= dgy && c.y >= dgy))
        return;
    if (b.y < a.y)
    {
        a.Swap(b);
        ta.Swap(tb);
    }
    if (c.y < b.y)
    {
        b.Swap(c);
        tb.Swap(tc);
    }
    if (b.y < a.y)
    {
        a.Swap(b);
        ta.Swap(tb);
    }
    const double m1 = (double)(a.x - b.x) / (a.y - b.y);
    const double m2 = (double)(a.x - c.x) / (a.y - c.y);
    const double m3 = (double)(b.x - c.x) / (b.y - c.y);
    double b1 = a.x - m1 * a.y;
    double b2 = a.x - m2 * a.y;
    double b3 = b.x - m3 * b.y;
    const double qx = m2 * b.y + b2;
    if (qx < b.x)
    {
        double tx1o, ty1o, tx2o, ty2o;
        if (c.y - a.y)
        {
            tx1o = (double)(tc.x - ta.x) / (c.y - a.y);
            ty1o = (double)(tc.y - ta.y) / (c.y - a.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        if (b.y - a.y)
        {
            tx2o = (double)(tb.x - ta.x) / (b.y - a.y);
            ty2o = (double)(tb.y - ta.y) / (b.y - a.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        Vec2<double> q(ta.x + tx1o * (b.y - a.y), ta.y + ty1o * (b.y - a.y));
        double txf, tyf;
        if (b.x - qx)
        {
            txf = (tb.x - q.x) / (b.x - qx);
            tyf = (tb.y - q.y) / (b.x - qx);
        }
        else
        {
            txf = 0;
            tyf = 0;
        }
        drawFlatTriangleTexture(a.y,
            b.y,
            m2,
            b2,
            m1,
            b1,
            ta.x,
            ta.y,
            ta.x,
            ta.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
        if (c.y - b.y)
        {
            tx2o = (double)(tc.x - tb.x) / (c.y - b.y);
            ty2o = (double)(tc.y - tb.y) / (c.y - b.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        drawFlatTriangleTexture(b.y,
            c.y,
            m2,
            b2,
            m3,
            b3,
            q.x,
            q.y,
            tb.x,
            tb.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
    }
    else
    {
        double tx1o, ty1o, tx2o, ty2o;
        if (b.y - a.y)
        {
            tx1o = (double)(tb.x - ta.x) / (b.y - a.y);
            ty1o = (double)(tb.y - ta.y) / (b.y - a.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        if (c.y - a.y)
        {
            tx2o = (double)(tc.x - ta.x) / (c.y - a.y);
            ty2o = (double)(tc.y - ta.y) / (c.y - a.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        Vec2<double> q(ta.x + tx2o * (b.y - a.y), ta.y + ty2o * (b.y - a.y));
        double txf, tyf;
        if (qx - b.x)
        {
            txf = (q.x - tb.x) / (qx - b.x);
            tyf = (q.y - tb.y) / (qx - b.x);
        }
        else
        {
            txf = 0;
            tyf = 0;
        }
        drawFlatTriangleTexture(a.y,
            b.y,
            m1,
            b1,
            m2,
            b2,
            ta.x,
            ta.y,
            ta.x,
            ta.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
        if (c.y - b.y)
        {
            tx1o = (double)(tc.x - tb.x) / (c.y - b.y);
            ty1o = (double)(tc.y - tb.y) / (c.y - b.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        drawFlatTriangleTexture(b.y,
            c.y,
            m3,
            b3,
            m2,
            b2,
            tb.x,
            tb.y,
            q.x,
            q.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
    }
    rend = 1;
}

void Image::drawTriangleAlpha(
    Point a, Point b, Point c, int color) // fills a triangle
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    a += drawOff[doa];
    b += drawOff[doa];
    c += drawOff[doa];
    if ((a.x < dpx && b.x < dpx && c.x < dpx)
        || (a.y < dpy && b.y < dpy && c.y < dpy)
        || (a.x >= dgx && b.x >= dgx && c.x >= dgx)
        || (a.y >= dgy && b.y >= dgy && c.y >= dgy))
        return;
    if (alpha[alphaCount])
    {
        unsigned char* cf = (unsigned char*)&color;
        cf[3] = (unsigned char)((cf[3] > alpha[alphaCount])
                                * (cf[3] - alpha[alphaCount]));
    }
    if (b.y < a.y) a.Swap(b);
    if (c.y < b.y) b.Swap(c);
    if (b.y < a.y) a.Swap(b);
    if (a.y == b.y)
    {
        if (b.x < a.x) a.Swap(b);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        const float m3 = (float)(b.x - c.x) / (float)(b.y - c.y);
        float b2 = (float)a.x - m2 * (float)a.y;
        float b3 = (float)b.x - m3 * (float)b.y;
        drawFlatTriangleAlpha(b.y, c.y, m2, b2, m3, b3, color);
    }
    else if (b.y == c.y)
    {
        if (c.x < b.x) b.Swap(c);
        const float m1 = (float)(a.x - b.x) / (float)(a.y - b.y);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        float b1 = (float)a.x - m1 * (float)a.y;
        float b2 = (float)a.x - m2 * (float)a.y;
        drawFlatTriangleAlpha(a.y, b.y, m1, b1, m2, b2, color);
    }
    else
    {
        const float m1 = (float)(a.x - b.x) / (float)(a.y - b.y);
        const float m2 = (float)(a.x - c.x) / (float)(a.y - c.y);
        const float m3 = (float)(b.x - c.x) / (float)(b.y - c.y);
        float b1 = (float)a.x - m1 * (float)a.y;
        float b2 = (float)a.x - m2 * (float)a.y;
        float b3 = (float)b.x - m3 * (float)b.y;
        const float qx = m2 * (float)b.y + b2;
        if (qx < (float)b.x)
        {
            drawFlatTriangleAlpha(a.y, b.y, m2, b2, m1, b1, color);
            drawFlatTriangleAlpha(b.y, c.y, m2, b2, m3, b3, color);
        }
        else
        {
            drawFlatTriangleAlpha(a.y, b.y, m1, b1, m2, b2, color);
            drawFlatTriangleAlpha(b.y, c.y, m3, b3, m2, b2, color);
        }
    }
    rend = 1;
}

void Image::drawTriangleTextureAlpha(Point a,
    Point b,
    Point c,
    Point ta,
    Point tb,
    Point tc,
    const Image& textur) // fills a triangle
{
    if (alpha[alphaCount] == 0xFF) return;
    int dpx = dPosA[doa].x;
    int dpy = dPosA[doa].y;
    int dgx = dSizeA[doa].x;
    int dgy = dSizeA[doa].y;
    a += drawOff[doa];
    b += drawOff[doa];
    c += drawOff[doa];
    if ((a.x < dpx && b.x < dpx && c.x < dpx)
        || (a.y < dpy && b.y < dpy && c.y < dpy)
        || (a.x >= dgx && b.x >= dgx && c.x >= dgx)
        || (a.y >= dgy && b.y >= dgy && c.y >= dgy))
        return;
    if (b.y < a.y)
    {
        a.Swap(b);
        ta.Swap(tb);
    }
    if (c.y < b.y)
    {
        b.Swap(c);
        tb.Swap(tc);
    }
    if (b.y < a.y)
    {
        a.Swap(b);
        ta.Swap(tb);
    }
    const double m1 = (double)(a.x - b.x) / (a.y - b.y);
    const double m2 = (double)(a.x - c.x) / (a.y - c.y);
    const double m3 = (double)(b.x - c.x) / (b.y - c.y);
    double b1 = a.x - m1 * a.y;
    double b2 = a.x - m2 * a.y;
    double b3 = b.x - m3 * b.y;
    const double qx = m2 * b.y + b2;
    if (qx < b.x)
    {
        double tx1o, ty1o, tx2o, ty2o;
        if (c.y - a.y)
        {
            tx1o = (double)(tc.x - ta.x) / (c.y - a.y);
            ty1o = (double)(tc.y - ta.y) / (c.y - a.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        if (b.y - a.y)
        {
            tx2o = (double)(tb.x - ta.x) / (b.y - a.y);
            ty2o = (double)(tb.y - ta.y) / (b.y - a.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        Vec2<double> q(ta.x + tx1o * (b.y - a.y), ta.y + ty1o * (b.y - a.y));
        double txf, tyf;
        if (b.x - qx)
        {
            txf = (tb.x - q.x) / (b.x - qx);
            tyf = (tb.y - q.y) / (b.x - qx);
        }
        else
        {
            txf = 0;
            tyf = 0;
        }
        drawFlatTriangleTextureAlpha(a.y,
            b.y,
            m2,
            b2,
            m1,
            b1,
            ta.x,
            ta.y,
            ta.x,
            ta.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
        if (c.y - b.y)
        {
            tx2o = (double)(tc.x - tb.x) / (c.y - b.y);
            ty2o = (double)(tc.y - tb.y) / (c.y - b.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        drawFlatTriangleTextureAlpha(b.y,
            c.y,
            m2,
            b2,
            m3,
            b3,
            q.x,
            q.y,
            tb.x,
            tb.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
    }
    else
    {
        double tx1o, ty1o, tx2o, ty2o;
        if (b.y - a.y)
        {
            tx1o = (double)(tb.x - ta.x) / (b.y - a.y);
            ty1o = (double)(tb.y - ta.y) / (b.y - a.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        if (c.y - a.y)
        {
            tx2o = (double)(tc.x - ta.x) / (c.y - a.y);
            ty2o = (double)(tc.y - ta.y) / (c.y - a.y);
        }
        else
        {
            tx2o = 0;
            ty2o = 0;
        }
        Vec2<double> q(ta.x + tx2o * (b.y - a.y), ta.y + ty2o * (b.y - a.y));
        double txf, tyf;
        if (qx - b.x)
        {
            txf = (q.x - tb.x) / (qx - b.x);
            tyf = (q.y - tb.y) / (qx - b.x);
        }
        else
        {
            txf = 0;
            tyf = 0;
        }
        drawFlatTriangleTextureAlpha(a.y,
            b.y,
            m1,
            b1,
            m2,
            b2,
            ta.x,
            ta.y,
            ta.x,
            ta.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
        if (c.y - b.y)
        {
            tx1o = (double)(tc.x - tb.x) / (c.y - b.y);
            ty1o = (double)(tc.y - tb.y) / (c.y - b.y);
        }
        else
        {
            tx1o = 0;
            ty1o = 0;
        }
        drawFlatTriangleTextureAlpha(b.y,
            c.y,
            m3,
            b3,
            m2,
            b2,
            tb.x,
            tb.y,
            q.x,
            q.y,
            tx1o,
            ty1o,
            tx2o,
            ty2o,
            txf,
            tyf,
            textur);
    }
    rend = 1;
}

void Image::replaceColorWithAlpha(int color)
{
    int r = (color & 0xFF0000) >> 16;
    int g = (color & 0xFF00) >> 8;
    int b = color & 0xFF;
    int dx = drawOff[doa].x, dy = drawOff[doa].y;
    int xx = dPosA[doa].x, yy = dPosA[doa].y;
    int bb = dSizeA[doa].x, hh = dSizeA[doa].y;
    for (int y = dy + yy; y < hh; y++)
    {
        int ygr = y * size.x;
        for (int x = dx + xx; x < bb; x++)
        {
            unsigned char* cf = (unsigned char*)&(fc[x + ygr]);
            int distance = (int)sqrt(
                (float)((r - cf[2]) * (r - cf[2]) + (g - cf[1]) * (g - cf[1])
                        + (b - cf[0]) * (b - cf[0])));
            if (distance > 255) distance = 255;
            cf[3] = (unsigned char)(distance);
        }
    }
}

bool Image::setDrawOptions(
    const Point& pos, const Point& gr) // sets the draw options
{
    int dx = drawOff[doa].x, dy = drawOff[doa].y;
    int xx = dPosA[doa].x, yy = dPosA[doa].y;
    int bb = dSizeA[doa].x, hh = dSizeA[doa].y;
    if (dx + pos.x + gr.x < 0 || dy + pos.y + gr.y < 0 || dx + pos.x >= size.x
        || dy + pos.y >= size.y)
        return 0;
    if (pos.x + gr.x + dx < xx || pos.y + gr.y + dy < yy || dx + pos.x >= bb
        || dy + pos.y >= hh)
        return 0;
    ++doa;
    assert(doa < 2000);
    dPosA[doa].x = maxInt(pos.x + dx, xx);
    dPosA[doa].y = maxInt(pos.y + dy, yy);
    dSizeA[doa].x = minInt(pos.x + gr.x + dx, bb);
    dSizeA[doa].y = minInt(pos.y + gr.y + dy, hh);
    drawOff[doa].x = dx + pos.x;
    drawOff[doa].y = dy + pos.y;
    return 1;
}

bool Image::setDrawOptions(int x, int y, int br, int hi)
{
    int dx = drawOff[doa].x, dy = drawOff[doa].y;
    int xx = dPosA[doa].x, yy = dPosA[doa].y;
    int bb = dSizeA[doa].x, hh = dSizeA[doa].y;
    if (dx + x + br < 0 || dy + y + hi < 0 || dx + x >= size.x
        || dy + y >= size.y)
        return 0;
    if (x + br + dx < xx || y + hi + dy < yy || dx + x >= bb || dy + y >= hh)
        return 0;
    ++doa;
    assert(doa < 2000);
    dPosA[doa].x = maxInt(x + dx, xx);
    dPosA[doa].y = maxInt(y + dy, yy);
    dSizeA[doa].x = minInt(x + br + dx, bb);
    dSizeA[doa].y = minInt(y + hi + dy, hh);
    drawOff[doa].x = dx + x;
    drawOff[doa].y = dy + y;
    return 1;
}

bool Image::setDrawOptionsErzwingen(
    const Point& pos, const Point& gr) // sets the draw options
{
    int dx = drawOff[doa].x, dy = drawOff[doa].y;
    if (dx + pos.x + gr.x < 0 || dy + pos.y + gr.y < 0 || dx + pos.x >= size.x
        || dy + pos.y >= size.y)
        return 0;
    ++doa;
    assert(doa < 2000);
    dPosA[doa].x = maxInt(pos.x + dx, 0);
    dPosA[doa].y = maxInt(pos.y + dy, 0);
    dSizeA[doa].x = minInt(pos.x + gr.x + dx, size.x);
    dSizeA[doa].y = minInt(pos.y + gr.y + dy, size.y);
    drawOff[doa].x = dx + pos.x;
    drawOff[doa].y = dy + pos.y;
    return 1;
}

bool Image::setDrawOptionsErzwingen(
    int x, int y, int br, int hi) // sets the draw options
{
    int dx = drawOff[doa].x, dy = drawOff[doa].y;
    if (dx + x + br < 0 || dy + y + hi < 0 || dx + x >= size.x
        || dy + y >= size.y)
        return 0;
    ++doa;
    assert(doa < 2000);
    dPosA[doa].x = maxInt(x + dx, 0);
    dPosA[doa].y = maxInt(y + dy, 0);
    dSizeA[doa].x = minInt(x + br + dx, size.x);
    dSizeA[doa].y = minInt(y + hi + dy, size.y);
    drawOff[doa].x = dx + x;
    drawOff[doa].y = dy + y;
    return 1;
}

void Image::setDrawOptionsReset()
{
    ++doa;
    dPosA[doa].x = 0;
    dPosA[doa].y = 0;
    dSizeA[doa].x = size.x;
    dSizeA[doa].y = size.y;
    drawOff[doa].x = 0;
    drawOff[doa].y = 0;
}

void Image::addScrollOffset(int xOff, int yOff) // sets scroll offset
{
    drawOff[doa].x -= xOff;
    drawOff[doa].y -= yOff;
}

void Image::releaseDrawOptions() // resets the draw options
{
    --doa;
}

bool Image::getNeedRender()
{
    bool ret = rend;
    rend = 0;
    return ret;
}

// constant
int* Image::getBuffer() const // returns the buffer
{
    return fc;
}

int Image::getPixel(int x, int y) const
{
    if (x < 0 || y < 0 || x >= size.x || y >= size.y) return 0;
    return fc[x + y * size.x];
}

const Point& Image::getSize() const // returns the size
{
    return size;
}

int Image::getWidth() const // returns the width
{
    return size.x;
}

int Image::getHeight() const // returns the height
{
    return size.y;
}

unsigned char Image::getAlpha() const // returns the alpha value
{
    return (unsigned char)(255 - alpha[alphaCount]);
}

const Point& Image::getDrawPos() const
{
    return dPosA[doa];
}

const Point& Image::getDrawGr() const
{
    return dSizeA[doa];
}

const Point& Image::getDrawOff() const
{
    return drawOff[doa];
}

bool Image::hasAlpha3D() const
{
    return alpha3D;
}

int Image::getAverageColor() const
{
    double a = 0;
    double r = 0;
    double g = 0;
    double b = 0;
    for (int i = 0; i < size.x * size.y; i++)
    {
        float currentA = (float)(fc[i] >> 24) / 255.f;
        a = a + currentA;
        r = r + (double)((float)((fc[i] >> 16) & 0xFF) * currentA);
        g = g + (double)((float)((fc[i] >> 8) & 0xFF) * currentA);
        b = b + (double)((float)(fc[i] & 0xFF) * currentA);
    }
    a = a / (size.x * size.y);
    r = (r / (size.x * size.y)) / a;
    g = (g / (size.x * size.y)) / a;
    b = (b / (size.x * size.y)) / a;
    a = a * 255;
    return ((((int)a) & 0xFF) << 24) | ((((int)r) & 0xFF) << 16)
         | ((((int)g) & 0xFF) << 8) | (((int)b) & 0xFF);
}

// Contents of the ImageView class from Image.h
// Constructor
ImageView::ImageView()
    : DrawableBackground(),
      bild(0)
{
    style = 0;
    mak = _ret1ME;
}

// Destructor
ImageView::~ImageView()
{
    if (bild) bild->release();
}

void ImageView::doMouseEvent(MouseEvent& me, bool userRet) // calls Mak
{
    if (userRet)
    {
        int rbr = 0;
        if (hasStyle(Style::Border) && border) rbr = border->getRWidth();
        bool vs = hasStyle(Style::VScroll) && vertikalScrollBar;
        bool hs = hasStyle(Style::HScroll) && horizontalScrollBar;
        if (vs)
        {
            if (hs)
                horizontalScrollBar->doMouseMessage(
                    rbr, gr.y - rbr - 15, gr.x - rbr * 2 - 15, 15, me);
            vertikalScrollBar->doMouseMessage(
                gr.x - rbr - 15, rbr, 15, gr.y - rbr * 2, me);
        }
        else if (hs)
            horizontalScrollBar->doMouseMessage(
                rbr, gr.y - rbr - 15, gr.x - rbr * 2, 15, me);
    }
    me.processed = userRet;
}

// non-constant
void ImageView::setImageZ(Image* b) // sets the image
{
    if (bild) bild->release();
    bild = b;
    if (!vertikalScrollBar) vertikalScrollBar = new VScrollBar();
    if (!horizontalScrollBar) horizontalScrollBar = new HScrollBar();
    if (b)
    {
        horizontalScrollBar->getScrollData()->max = b->getWidth();
        vertikalScrollBar->getScrollData()->max = b->getHeight();
    }
    rend = 1;
}

void ImageView::setImage(Image* b)
{
    if (!bild) bild = new Image();
    bild->newImage(b->getWidth(), b->getHeight(), 0);
    bild->drawImage(0, 0, b->getWidth(), b->getHeight(), *b);
    if (!vertikalScrollBar) vertikalScrollBar = new VScrollBar();
    if (!horizontalScrollBar) horizontalScrollBar = new HScrollBar();
    horizontalScrollBar->getScrollData()->max = b->getWidth();
    vertikalScrollBar->getScrollData()->max = b->getHeight();
    b->release();
    rend = 1;
}

bool ImageView::tick(double tickVal) // tick
{
    return DrawableBackground::tick(tickVal);
}

void ImageView::render(Image& zRObj) // draws to zRObj
{
    if (hasStyle(Style::Visible))
    {
        DrawableBackground::render(zRObj);
        rwLock.lockRead();
        if (!zRObj.setDrawOptions(innenPosition, innenSize))
        {
            rwLock.unlockRead();
            return;
        }
        if (bild)
        {
            int x = 0;
            int y = 0;
            int br = innenSize.x;
            int hi = innenSize.y;
            if (!(vertikalScrollBar && hasStyle(Style::VScroll))
                && !(horizontalScrollBar && hasStyle(Style::HScroll)))
            {
                if (hasStyle(Style::Alpha))
                {
                    if (hasStyle(Style::Scaled))
                        zRObj.alphaImageScaled(x, y, br, hi, *bild);
                    else
                        zRObj.alphaImage(x, y, br, hi, *bild);
                }
                else
                {
                    if (hasStyle(Style::Scaled))
                        zRObj.drawImageScaled(x, y, br, hi, *bild);
                    else
                        zRObj.drawImage(x, y, br, hi, *bild);
                }
            }
            else
            {
                if (!zRObj.setDrawOptions(x, y, br, hi))
                {
                    zRObj.releaseDrawOptions();
                    rwLock.unlockRead();
                    return;
                }
                if (hasStyle(Style::Alpha))
                    zRObj.alphaImage(-horizontalScrollBar->getScroll(),
                        -vertikalScrollBar->getScroll(),
                        bild->getWidth(),
                        bild->getHeight(),
                        *bild);
                else
                    zRObj.drawImage(-horizontalScrollBar->getScroll(),
                        -vertikalScrollBar->getScroll(),
                        bild->getWidth(),
                        bild->getHeight(),
                        *bild);
                zRObj.releaseDrawOptions();
            }
        }
        zRObj.releaseDrawOptions();
        rwLock.unlockRead();
    }
}

// constant
Image* ImageView::getImage() const // returns the image
{
    if (bild) return dynamic_cast<Image*>(bild->getThis());
    return 0;
}

Image* ImageView::zImage() const
{
    return bild;
}

Drawable* ImageView::duplicate() const // creates a copy of the drawing
{
    ImageView* obj = new ImageView();
    obj->setPosition(pos);
    obj->setSize(gr);
    obj->setMouseEventParameter(makParam);
    obj->setKeyboardEventParameter(takParam);
    obj->setMouseEvent(mak);
    obj->setKeyboardEvent(tak);
    if (toolTip) obj->setToolTipZ((ToolTip*)toolTip->duplicate());
    obj->setStyle(style);
    obj->setBackgroundColor(backgroundColor);
    if (backgroundFeld)
        obj->setAlphaFieldZ((AlphaField*)backgroundFeld->duplicate());
    if (border) obj->setBorderZ((Border*)border->duplicate());
    if (backgroundImage)
        obj->setBackgroundImage(
            dynamic_cast<Image*>(backgroundImage->getThis()));
    if (bild) obj->setImage(dynamic_cast<Image*>(bild->getThis()));
    obj->setStyle(style);
    return obj;
}

#ifdef WIN32
Image* Framework::loadImage(const char* pfad, Text* zError)
{
    Text p = pfad;
    Text* txt = p.getTeilText(p.positionOf('.', p.countOf('.') - 1));
    if (!(txt->isEqual(".bmp") || txt->isEqual(".jpg") || txt->isEqual(".gif")
            || txt->isEqual(".png")))
    {
        zError->setText("Die Angegebene File ist keine gueltige ImageFile!");
        txt->release();
        return 0;
    }
    txt->release();
    wchar_t* name = new wchar_t[p.getLength() + 1];
    for (int i = 0; i < p.getLength(); i++)
        name[i] = (wchar_t)p.getText()[i];
    name[p.getLength()] = '\0';

    Gdiplus::Bitmap bitmap(name);
    Gdiplus::Color pix;
    delete[] name;

    Image* ret = new Image();
    ret->newImage(bitmap.GetWidth(), bitmap.GetHeight(), 0);
    int* buff = ret->getBuffer();
    for (unsigned int i = 0; i < bitmap.GetWidth() * bitmap.GetHeight(); i++)
    {
        bitmap.GetPixel(i % bitmap.GetWidth(), i / bitmap.GetWidth(), &pix);
        buff[i] = pix.GetValue();
    }
    return ret;
}

#endif