コード例 #1
0
ファイル: LGraphics.cs プロジェクト: keppelcao/LGame
        public void DrawImage(LImage img, int dstX, int dstY, int dstWidth, int dstHeight,
                int srcX, int srcY, int srcWidth, int srcHeight)
        {
            if (isClose || img == null || img.IsClose())
            {
                return;
            }

            LGraphics pixel = img.GetLGraphics();

            pixel.UpdatePixels();

            dstX += translateX;
            dstY += translateY;
            srcX += translateX;
            srcY += translateY;

            if (pixel == null || dstWidth <= 0 || dstHeight <= 0 || srcWidth <= 0 || srcHeight <= 0)
            {
                return;
            }
            if (dstWidth == srcWidth && dstHeight == srcHeight)
            {
                DrawImage(pixel, dstX, dstY, dstWidth, dstHeight, srcX, srcY);
                return;
            }

            Color[] currentPixels = pixel.pixels;

            int spitch = pixel.width;
            int dpitch = this.width;

            float x_ratio = ((float)srcWidth - 1) / dstWidth;
            float y_ratio = ((float)srcHeight - 1) / dstHeight;
            float x_diff = 0F;
            float y_diff = 0F;

            int dx = dstX;
            int dy = dstY;
            int sx = srcX;
            int sy = srcY;
            int i = 0;
            int j = 0;

            for (; i < dstHeight; i++)
            {
                sy = (int)(i * y_ratio) + srcY;
                dy = i + dstY;
                y_diff = (y_ratio * i + srcY) - sy;
                if (sy < 0 || dy < 0)
                {
                    continue;
                }
                if (sy >= pixel.height || dy >= this.height)
                {
                    break;
                }

                for (j = 0; j < dstWidth; j++)
                {
                    sx = (int)(j * x_ratio) + srcX;
                    dx = j + dstX;
                    x_diff = (x_ratio * j + srcX) - sx;
                    if (sx < 0 || dx < 0)
                    {
                        continue;
                    }
                    if (sx >= pixel.width || dx >= this.width)
                    {
                        break;
                    }

                    int src_ptr = sx + sy * spitch;
                    int dst_ptr = dx + dy * dpitch;
                    Color src_color = currentPixels[src_ptr];
                    uint src_pixel = src_color.PackedValue;

                    if (src_pixel != LSystem.TRANSPARENT)
                    {
                        float ta = (1 - x_diff) * (1 - y_diff);
                        float tb = (x_diff) * (1 - y_diff);
                        float tc = (1 - x_diff) * (y_diff);
                        float td = (x_diff) * (y_diff);

                        uint a = (uint)(((src_pixel & 0xff000000) >> 24) * ta +
                                                ((src_pixel & 0xff000000) >> 24) * tb +
                                                ((src_pixel & 0xff000000) >> 24) * tc +
                                                ((src_pixel & 0xff000000) >> 24) * td) & 0xff;
                        uint b = (uint)(((src_pixel & 0xff0000) >> 16) * ta +
                                                ((src_pixel & 0xff0000) >> 16) * tb +
                                                ((src_pixel & 0xff0000) >> 16) * tc +
                                                ((src_pixel & 0xff0000) >> 16) * td) & 0xff;
                        uint g = (uint)(((src_pixel & 0xff00) >> 8) * ta +
                                                ((src_pixel & 0xff00) >> 8) * tb +
                                                ((src_pixel & 0xff00) >> 8) * tc +
                                                ((src_pixel & 0xff00) >> 8) * td) & 0xff;
                        uint r = (uint)((src_pixel & 0xff) * ta +
                                                (src_pixel & 0xff) * tb +
                                                (src_pixel & 0xff) * tc +
                                                (src_pixel & 0xff) * td) & 0xff;

                        Color dst_color = pixels[dst_ptr];
                        DrawPoint(pixels, dst_ptr, Blend(r, g, b, a, dst_color));
                    }
                    else
                    {
                        DrawPoint(pixels, dst_ptr, LSystem.TRANSPARENT);
                    }
                }
            }

        }