private unsafe IntPtr CreateReadChannelDesc(int channel, string name, int depth, VRect bounds) { IntPtr addressPtr = Memory.Allocate(Marshal.SizeOf(typeof(ReadChannelDesc)), true); IntPtr namePtr = IntPtr.Zero; try { namePtr = Marshal.StringToHGlobalAnsi(name); channelReadDescPtrs.Add(new ChannelDescPtrs(addressPtr, namePtr)); } catch (Exception) { Memory.Free(addressPtr); if (namePtr != IntPtr.Zero) { Marshal.FreeHGlobal(namePtr); namePtr = IntPtr.Zero; } throw; } ReadChannelDesc *desc = (ReadChannelDesc *)addressPtr.ToPointer(); desc->minVersion = PSConstants.kCurrentMinVersReadChannelDesc; desc->maxVersion = PSConstants.kCurrentMaxVersReadChannelDesc; desc->depth = depth; desc->bounds = bounds; desc->target = (channel < PSConstants.ChannelPorts.Alpha); desc->shown = (channel < PSConstants.ChannelPorts.SelectionMask); desc->tileOrigin.h = 0; desc->tileOrigin.v = 0; desc->tileSize.h = bounds.right - bounds.left; desc->tileSize.v = bounds.bottom - bounds.top; desc->port = new IntPtr(channel); switch (channel) { case PSConstants.ChannelPorts.Gray: desc->channelType = ChannelTypes.Black; break; case PSConstants.ChannelPorts.Red: desc->channelType = ChannelTypes.Red; break; case PSConstants.ChannelPorts.Green: desc->channelType = ChannelTypes.Green; break; case PSConstants.ChannelPorts.Blue: desc->channelType = ChannelTypes.Blue; break; case PSConstants.ChannelPorts.Alpha: desc->channelType = ChannelTypes.Transparency; break; case PSConstants.ChannelPorts.SelectionMask: desc->channelType = ChannelTypes.SelectionMask; break; } desc->name = namePtr; return(addressPtr); }
public unsafe IntPtr CreateReadImageDocumentPointer(bool ignoreAlpha, bool hasSelection) { IntPtr readDocumentPtr = Memory.Allocate(Marshal.SizeOf(typeof(ReadImageDocumentDesc)), true); try { ReadImageDocumentDesc *doc = (ReadImageDocumentDesc *)readDocumentPtr.ToPointer(); doc->minVersion = PSConstants.kCurrentMinVersReadImageDocDesc; doc->maxVersion = PSConstants.kCurrentMaxVersReadImageDocDesc; doc->imageMode = (int)imageMode; switch (imageMode) { case ImageModes.GrayScale: case ImageModes.RGB: doc->depth = 8; break; case ImageModes.Gray16: case ImageModes.RGB48: doc->depth = 16; break; } doc->bounds.top = 0; doc->bounds.left = 0; doc->bounds.right = documentWidth; doc->bounds.bottom = documentHeight; doc->hResolution = new Fixed16((int)(dpiX + 0.5)); doc->vResolution = new Fixed16((int)(dpiY + 0.5)); if (imageMode == ImageModes.RGB || imageMode == ImageModes.RGB48) { IntPtr channel = CreateReadChannelDesc(PSConstants.ChannelPorts.Red, Resources.RedChannelName, doc->depth, doc->bounds); ReadChannelDesc *ch = (ReadChannelDesc *)channel.ToPointer(); for (int i = PSConstants.ChannelPorts.Green; i <= PSConstants.ChannelPorts.Blue; i++) { string name = null; switch (i) { case PSConstants.ChannelPorts.Green: name = Resources.GreenChannelName; break; case PSConstants.ChannelPorts.Blue: name = Resources.BlueChannelName; break; } IntPtr ptr = CreateReadChannelDesc(i, name, doc->depth, doc->bounds); ch->next = ptr; ch = (ReadChannelDesc *)ptr.ToPointer(); } doc->targetCompositeChannels = doc->mergedCompositeChannels = channel; if (!ignoreAlpha) { IntPtr alphaPtr = CreateReadChannelDesc(PSConstants.ChannelPorts.Alpha, Resources.AlphaChannelName, doc->depth, doc->bounds); doc->targetTransparency = doc->mergedTransparency = alphaPtr; } } else { IntPtr channel = CreateReadChannelDesc(PSConstants.ChannelPorts.Gray, Resources.GrayChannelName, doc->depth, doc->bounds); doc->targetCompositeChannels = doc->mergedCompositeChannels = channel; } if (hasSelection) { IntPtr selectionPtr = CreateReadChannelDesc(PSConstants.ChannelPorts.SelectionMask, Resources.MaskChannelName, doc->depth, doc->bounds); doc->selection = selectionPtr; } } catch (Exception) { if (readDocumentPtr != IntPtr.Zero) { Memory.Free(readDocumentPtr); readDocumentPtr = IntPtr.Zero; } throw; } return(readDocumentPtr); }
public unsafe IntPtr CreateReadImageDocumentPointer(FilterCase filterCase, bool hasSelection) { IntPtr readDocumentPtr = Memory.Allocate(Marshal.SizeOf(typeof(ReadImageDocumentDesc)), true); try { ReadImageDocumentDesc *doc = (ReadImageDocumentDesc *)readDocumentPtr.ToPointer(); doc->minVersion = PSConstants.kCurrentMinVersReadImageDocDesc; doc->maxVersion = PSConstants.kCurrentMaxVersReadImageDocDesc; doc->imageMode = (int)imageMode; switch (imageMode) { case ImageModes.GrayScale: case ImageModes.RGB: doc->depth = 8; break; case ImageModes.Gray16: case ImageModes.RGB48: doc->depth = 16; break; } doc->bounds.top = 0; doc->bounds.left = 0; doc->bounds.right = documentWidth; doc->bounds.bottom = documentHeight; doc->hResolution = new Fixed16((int)(dpiX + 0.5)); doc->vResolution = new Fixed16((int)(dpiY + 0.5)); if (imageMode == ImageModes.RGB || imageMode == ImageModes.RGB48) { IntPtr channel = CreateReadChannelDesc(PSConstants.ChannelPorts.Red, Resources.RedChannelName, doc->depth, doc->bounds); ReadChannelDesc *ch = (ReadChannelDesc *)channel.ToPointer(); for (int i = PSConstants.ChannelPorts.Green; i <= PSConstants.ChannelPorts.Blue; i++) { string name = null; switch (i) { case PSConstants.ChannelPorts.Green: name = Resources.GreenChannelName; break; case PSConstants.ChannelPorts.Blue: name = Resources.BlueChannelName; break; } IntPtr ptr = CreateReadChannelDesc(i, name, doc->depth, doc->bounds); ch->next = ptr; ch = (ReadChannelDesc *)ptr.ToPointer(); } doc->targetCompositeChannels = doc->mergedCompositeChannels = channel; if (filterCase == FilterCase.EditableTransparencyNoSelection || filterCase == FilterCase.EditableTransparencyWithSelection) { IntPtr alphaPtr = CreateReadChannelDesc(PSConstants.ChannelPorts.Alpha, Resources.AlphaChannelName, doc->depth, doc->bounds); doc->targetTransparency = doc->mergedTransparency = alphaPtr; } } else if (imageMode == ImageModes.GrayScale || imageMode == ImageModes.Gray16) { IntPtr channel = CreateReadChannelDesc(PSConstants.ChannelPorts.Gray, Resources.GrayChannelName, doc->depth, doc->bounds); doc->targetCompositeChannels = doc->mergedCompositeChannels = channel; } else if (imageMode == ImageModes.CMYK) { IntPtr channel = CreateReadChannelDesc(PSConstants.ChannelPorts.Cyan, Resources.CyanChannelName, doc->depth, doc->bounds); ReadChannelDesc *ch = (ReadChannelDesc *)channel.ToPointer(); for (int i = PSConstants.ChannelPorts.Magenta; i <= PSConstants.ChannelPorts.Black; i++) { string name = null; switch (i) { case PSConstants.ChannelPorts.Magenta: name = Resources.MagentaChannelName; break; case PSConstants.ChannelPorts.Yellow: name = Resources.YellowChannelName; break; case PSConstants.ChannelPorts.Black: name = Resources.BlackChannelName; break; } IntPtr ptr = CreateReadChannelDesc(i, name, doc->depth, doc->bounds); ch->next = ptr; ch = (ReadChannelDesc *)ptr.ToPointer(); } doc->targetCompositeChannels = doc->mergedCompositeChannels = channel; } else { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Unsupported image mode: {0}", imageMode)); } if (hasSelection) { IntPtr selectionPtr = CreateReadChannelDesc(PSConstants.ChannelPorts.SelectionMask, Resources.MaskChannelName, doc->depth, doc->bounds); doc->selection = selectionPtr; } } catch (Exception) { if (readDocumentPtr != IntPtr.Zero) { Memory.Free(readDocumentPtr); } throw; } return(readDocumentPtr); }