예제 #1
0
        private static unsafe void FillSelectionMask(PixelMemoryDesc *destiniation, MaskSurface source, VRect srcRect)
        {
            byte *dstPtr = (byte *)destiniation->data.ToPointer();
            int   stride = destiniation->rowBits / 8;
            int   bpp    = destiniation->colBits / 8;
            int   offset = destiniation->bitOffset / 8;

            for (int y = srcRect.top; y < srcRect.bottom; y++)
            {
                byte *src = source.GetPointAddressUnchecked(srcRect.left, y);
                byte *dst = dstPtr + (y * stride) + offset;
                for (int x = srcRect.left; x < srcRect.right; x++)
                {
                    *dst = *src;

                    src++;
                    dst += bpp;
                }
            }
        }
예제 #2
0
        private static unsafe void FillChannelData(int channel, PixelMemoryDesc *destiniation, Surface source, VRect srcRect)
        {
            byte *dstPtr = (byte *)destiniation->data.ToPointer();
            int   stride = destiniation->rowBits / 8;
            int   bpp    = destiniation->colBits / 8;
            int   offset = destiniation->bitOffset / 8;

            for (int y = srcRect.top; y < srcRect.bottom; y++)
            {
                ColorBgra *src = source.GetPointAddressUnchecked(srcRect.left, y);
                byte *     dst = dstPtr + (y * stride) + offset;
                for (int x = srcRect.left; x < srcRect.right; x++)
                {
                    switch (channel)
                    {
                    case PSConstants.ChannelPorts.Red:
                        *dst = src->R;
                        break;

                    case PSConstants.ChannelPorts.Green:
                        *dst = src->G;
                        break;

                    case PSConstants.ChannelPorts.Blue:
                        *dst = src->B;
                        break;

                    case PSConstants.ChannelPorts.Alpha:
                        *dst = src->A;
                        break;
                    }
                    src++;
                    dst += bpp;
                }
            }
        }
예제 #3
0
        private unsafe short ReadPixelsProc(IntPtr port, PSScaling *scaling, VRect *writeRect, PixelMemoryDesc *destination, VRect *wroteRect)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.ChannelPorts, string.Format("port: {0}, rect: {1}", port.ToString(), DebugUtils.PointerToString(writeRect)));
#endif
            if (scaling == null || writeRect == null || destination == null)
            {
                return(PSError.paramErr);
            }

            if (destination->depth != 8)
            {
                return(PSError.errUnsupportedDepth);
            }

            if ((destination->bitOffset % 8) != 0)
            {
                return(PSError.errUnsupportedBitOffset);
            }

            if ((destination->colBits % 8) != 0)
            {
                return(PSError.errUnsupportedColBits);
            }

            if ((destination->rowBits % 8) != 0)
            {
                return(PSError.errUnsupportedRowBits);
            }

            int channel = port.ToInt32();

            if (channel < PSConstants.ChannelPorts.Red || channel > PSConstants.ChannelPorts.SelectionMask)
            {
                return(PSError.errUnknownPort);
            }

            VRect srcRect = scaling->sourceRect;
            VRect dstRect = scaling->destinationRect;

            int srcWidth  = srcRect.right - srcRect.left;
            int srcHeight = srcRect.bottom - srcRect.top;
            int dstWidth  = dstRect.right - dstRect.left;
            int dstHeight = dstRect.bottom - dstRect.top;

            if (channel == PSConstants.ChannelPorts.SelectionMask)
            {
                if (srcWidth == dstWidth && srcHeight == dstHeight)
                {
                    FillSelectionMask(destination, filterImageProvider.Mask, srcRect);
                }
                else if (dstWidth < srcWidth || dstHeight < srcHeight) // scale down
                {
                    if ((scaledSelectionMask == null) || scaledSelectionMask.Width != dstWidth || scaledSelectionMask.Height != dstHeight)
                    {
                        if (scaledSelectionMask != null)
                        {
                            scaledSelectionMask.Dispose();
                            scaledSelectionMask = null;
                        }

                        scaledSelectionMask = new MaskSurface(dstWidth, dstHeight);
                        scaledSelectionMask.SuperSampleFitSurface(filterImageProvider.Mask);
                    }

                    FillSelectionMask(destination, scaledSelectionMask, dstRect);
                }
                else if (dstWidth > srcWidth || dstHeight > srcHeight) // scale up
                {
                    if ((scaledSelectionMask == null) || scaledSelectionMask.Width != dstWidth || scaledSelectionMask.Height != dstHeight)
                    {
                        if (scaledSelectionMask != null)
                        {
                            scaledSelectionMask.Dispose();
                            scaledSelectionMask = null;
                        }

                        scaledSelectionMask = new MaskSurface(dstWidth, dstHeight);
                        scaledSelectionMask.BicubicFitSurface(filterImageProvider.Mask);
                    }

                    FillSelectionMask(destination, scaledSelectionMask, dstRect);
                }
            }
            else
            {
                if (srcWidth == dstWidth && srcHeight == dstHeight)
                {
                    FillChannelData(channel, destination, filterImageProvider.Source, srcRect);
                }
                else if (dstWidth < srcWidth || dstHeight < srcHeight) // scale down
                {
                    if ((scaledChannelSurface == null) || scaledChannelSurface.Width != dstWidth || scaledChannelSurface.Height != dstHeight)
                    {
                        if (scaledChannelSurface != null)
                        {
                            scaledChannelSurface.Dispose();
                            scaledChannelSurface = null;
                        }

                        scaledChannelSurface = new Surface(dstWidth, dstHeight);
                        scaledChannelSurface.SuperSampleFitSurface(filterImageProvider.Source);
                    }

                    FillChannelData(channel, destination, scaledChannelSurface, dstRect);
                }
                else if (dstWidth > srcWidth || dstHeight > srcHeight) // scale up
                {
                    if ((scaledChannelSurface == null) || scaledChannelSurface.Width != dstWidth || scaledChannelSurface.Height != dstHeight)
                    {
                        if (scaledChannelSurface != null)
                        {
                            scaledChannelSurface.Dispose();
                            scaledChannelSurface = null;
                        }

                        scaledChannelSurface = new Surface(dstWidth, dstHeight);
                        scaledChannelSurface.BicubicFitSurface(filterImageProvider.Source);
                    }

                    FillChannelData(channel, destination, scaledChannelSurface, dstRect);
                }
            }

            if (wroteRect != null)
            {
                *wroteRect = dstRect;
            }

            return(PSError.noErr);
        }
예제 #4
0
        private static unsafe void FillChannelData(int channel, PixelMemoryDesc *destiniation, SurfaceBase source, VRect srcRect, ImageModes mode)
        {
            byte *dstPtr = (byte *)destiniation->data.ToPointer();
            int   stride = destiniation->rowBits / 8;
            int   bpp    = destiniation->colBits / 8;
            int   offset = destiniation->bitOffset / 8;

            switch (mode)
            {
            case ImageModes.GrayScale:

                for (int y = srcRect.top; y < srcRect.bottom; y++)
                {
                    byte *src = source.GetPointAddressUnchecked(srcRect.left, y);
                    byte *dst = dstPtr + (y * stride) + offset;
                    for (int x = srcRect.left; x < srcRect.right; x++)
                    {
                        *dst = *src;

                        src++;
                        dst += bpp;
                    }
                }

                break;

            case ImageModes.RGB:

                for (int y = srcRect.top; y < srcRect.bottom; y++)
                {
                    byte *src = source.GetPointAddressUnchecked(srcRect.left, y);
                    byte *dst = dstPtr + (y * stride) + offset;
                    for (int x = srcRect.left; x < srcRect.right; x++)
                    {
                        switch (channel)
                        {
                        case PSConstants.ChannelPorts.Red:
                            *dst = src[2];
                            break;

                        case PSConstants.ChannelPorts.Green:
                            *dst = src[1];
                            break;

                        case PSConstants.ChannelPorts.Blue:
                            *dst = src[0];
                            break;

                        case PSConstants.ChannelPorts.Alpha:
                            *dst = src[3];
                            break;
                        }
                        src += 4;
                        dst += bpp;
                    }
                }

                break;

            case ImageModes.CMYK:

                for (int y = srcRect.top; y < srcRect.bottom; y++)
                {
                    byte *src = source.GetPointAddressUnchecked(srcRect.left, y);
                    byte *dst = dstPtr + (y * stride) + offset;
                    for (int x = srcRect.left; x < srcRect.right; x++)
                    {
                        switch (channel)
                        {
                        case PSConstants.ChannelPorts.Cyan:
                            *dst = src[0];
                            break;

                        case PSConstants.ChannelPorts.Magenta:
                            *dst = src[1];
                            break;

                        case PSConstants.ChannelPorts.Yellow:
                            *dst = src[2];
                            break;

                        case PSConstants.ChannelPorts.Black:
                            *dst = src[3];
                            break;
                        }
                        src += 4;
                        dst += bpp;
                    }
                }

                break;

            case ImageModes.Gray16:

                for (int y = srcRect.top; y < srcRect.bottom; y++)
                {
                    ushort *src = (ushort *)source.GetPointAddressUnchecked(srcRect.left, y);
                    ushort *dst = (ushort *)(dstPtr + (y * stride) + offset);
                    for (int x = srcRect.left; x < srcRect.right; x++)
                    {
                        *dst = *src;

                        src++;
                        dst += bpp;
                    }
                }

                break;

            case ImageModes.RGB48:

                for (int y = srcRect.top; y < srcRect.bottom; y++)
                {
                    ushort *src = (ushort *)source.GetPointAddressUnchecked(srcRect.left, y);
                    ushort *dst = (ushort *)(dstPtr + (y * stride) + offset);
                    for (int x = srcRect.left; x < srcRect.right; x++)
                    {
                        switch (channel)
                        {
                        case PSConstants.ChannelPorts.Red:
                            *dst = src[2];
                            break;

                        case PSConstants.ChannelPorts.Green:
                            *dst = src[1];
                            break;

                        case PSConstants.ChannelPorts.Blue:
                            *dst = src[0];
                            break;

                        case PSConstants.ChannelPorts.Alpha:
                            *dst = src[3];
                            break;
                        }
                        src += 4;
                        dst += bpp;
                    }
                }

                break;
            }
        }
예제 #5
0
        private unsafe short ReadPixelsProc(IntPtr port, PSScaling *scaling, VRect *writeRect, PixelMemoryDesc *destination, VRect *wroteRect)
        {
#if DEBUG
            DebugUtils.Ping(DebugFlags.ChannelPorts, string.Format("port: {0}, rect: {1}", port.ToString(), DebugUtils.PointerToString(writeRect)));
#endif

            if (scaling == null || writeRect == null || destination == null)
            {
                return(PSError.paramErr);
            }

            if (destination->depth != 8 && destination->depth != 16)
            {
                return(PSError.errUnsupportedDepth);
            }

            // The offsets must be aligned to a System.Byte.
            if ((destination->bitOffset % 8) != 0)
            {
                return(PSError.errUnsupportedBitOffset);
            }

            if ((destination->colBits % 8) != 0)
            {
                return(PSError.errUnsupportedColBits);
            }

            if ((destination->rowBits % 8) != 0)
            {
                return(PSError.errUnsupportedRowBits);
            }

            int channel = port.ToInt32();

            if (channel < PSConstants.ChannelPorts.Gray || channel > PSConstants.ChannelPorts.SelectionMask)
            {
                return(PSError.errUnknownPort);
            }

            VRect srcRect = scaling->sourceRect;
            VRect dstRect = scaling->destinationRect;

            int         srcWidth    = srcRect.right - srcRect.left;
            int         srcHeight   = srcRect.bottom - srcRect.top;
            int         dstWidth    = dstRect.right - dstRect.left;
            int         dstHeight   = dstRect.bottom - dstRect.top;
            bool        isSelection = channel == PSConstants.ChannelPorts.SelectionMask;
            SurfaceBase source      = filterImageProvider.Source;

            if ((source.BitsPerChannel == 8 || isSelection) && destination->depth == 16)
            {
                return(PSError.errUnsupportedDepthConversion); // converting 8-bit image data to 16-bit is not supported.
            }

            if (isSelection)
            {
                if (srcWidth == dstWidth && srcHeight == dstHeight)
                {
                    FillSelectionMask(destination, filterImageProvider.Mask, srcRect);
                }
                else if (dstWidth < srcWidth || dstHeight < srcHeight) // scale down
                {
                    if ((scaledSelectionMask == null) || scaledSelectionMask.Width != dstWidth || scaledSelectionMask.Height != dstHeight)
                    {
                        if (scaledSelectionMask != null)
                        {
                            scaledSelectionMask.Dispose();
                            scaledSelectionMask = null;
                        }

                        try
                        {
                            scaledSelectionMask = new SurfaceGray8(dstWidth, dstHeight);
                            scaledSelectionMask.FitSurface(filterImageProvider.Mask);
                        }
                        catch (OutOfMemoryException)
                        {
                            return(PSError.memFullErr);
                        }
                    }

                    FillSelectionMask(destination, scaledSelectionMask, dstRect);
                }
                else if (dstWidth > srcWidth || dstHeight > srcHeight) // scale up
                {
                    if ((scaledSelectionMask == null) || scaledSelectionMask.Width != dstWidth || scaledSelectionMask.Height != dstHeight)
                    {
                        if (scaledSelectionMask != null)
                        {
                            scaledSelectionMask.Dispose();
                            scaledSelectionMask = null;
                        }

                        try
                        {
                            scaledSelectionMask = new SurfaceGray8(dstWidth, dstHeight);
                            scaledSelectionMask.FitSurface(filterImageProvider.Mask);
                        }
                        catch (OutOfMemoryException)
                        {
                            return(PSError.memFullErr);
                        }
                    }

                    FillSelectionMask(destination, scaledSelectionMask, dstRect);
                }
            }
            else
            {
                ImageModes mode = imageMode;

                if (source.BitsPerChannel == 16 && destination->depth == 8)
                {
                    if (ditheredChannelSurface == null)
                    {
                        short err = CreateDitheredChannelPortSurface(source);
                        if (err != PSError.noErr)
                        {
                            return(err);
                        }
                    }
                    mode = ditheredChannelImageMode;
                }

                if (srcWidth == dstWidth && srcHeight == dstHeight)
                {
                    FillChannelData(channel, destination, ditheredChannelSurface ?? source, srcRect, mode);
                }
                else if (dstWidth < srcWidth || dstHeight < srcHeight) // scale down
                {
                    if ((scaledChannelSurface == null) || scaledChannelSurface.Width != dstWidth || scaledChannelSurface.Height != dstHeight)
                    {
                        if (scaledChannelSurface != null)
                        {
                            scaledChannelSurface.Dispose();
                            scaledChannelSurface = null;
                        }

                        try
                        {
                            scaledChannelSurface = SurfaceFactory.CreateFromImageMode(dstWidth, dstHeight, mode);
                            scaledChannelSurface.FitSurface(ditheredChannelSurface ?? source);
                        }
                        catch (OutOfMemoryException)
                        {
                            return(PSError.memFullErr);
                        }
                    }

                    FillChannelData(channel, destination, scaledChannelSurface, dstRect, mode);
                }
                else if (dstWidth > srcWidth || dstHeight > srcHeight) // scale up
                {
                    if ((scaledChannelSurface == null) || scaledChannelSurface.Width != dstWidth || scaledChannelSurface.Height != dstHeight)
                    {
                        if (scaledChannelSurface != null)
                        {
                            scaledChannelSurface.Dispose();
                            scaledChannelSurface = null;
                        }

                        try
                        {
                            scaledChannelSurface = SurfaceFactory.CreateFromImageMode(dstWidth, dstHeight, mode);
                            scaledChannelSurface.FitSurface(ditheredChannelSurface ?? source);
                        }
                        catch (OutOfMemoryException)
                        {
                            return(PSError.memFullErr);
                        }
                    }

                    FillChannelData(channel, destination, scaledChannelSurface, dstRect, mode);
                }
            }

            if (wroteRect != null)
            {
                *wroteRect = dstRect;
            }

            return(PSError.noErr);
        }