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);
        }