From 49c77e5904decef9c99ce20af8b86697e23ec4e6 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Tue, 27 Apr 2021 08:30:36 -0400 Subject: [PATCH] add new DataFormats and reorganize directory structure --- .../Palette/Adobe/ACO/ACOColorSpace.cs | 73 ++++++++++++ .../Palette/Adobe/{ => ACO}/ACODataFormat.cs | 55 +++++++-- .../Palette/Adobe/ACT/ACTDataFormat.cs | 106 ++++++++++++++++++ .../{ => SwatchExchange}/ASEDataFormat.cs | 30 ++++- 4 files changed, 253 insertions(+), 11 deletions(-) create mode 100644 Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACOColorSpace.cs rename Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/{ => ACO}/ACODataFormat.cs (67%) create mode 100644 Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACT/ACTDataFormat.cs rename Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/{ => SwatchExchange}/ASEDataFormat.cs (76%) diff --git a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACOColorSpace.cs b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACOColorSpace.cs new file mode 100644 index 00000000..df5a9f6e --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACOColorSpace.cs @@ -0,0 +1,73 @@ +// +// ACOColorSpace.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe +{ + public enum ACOColorSpace : ushort + { + /// + /// The first three values in the color data are red, green, and blue. + /// They are full unsigned 16-bit values as in Apple's RGBColor data + /// structure. Pure red = 65535, 0, 0. + /// + RGB = 0, + /// + /// The first three values in the color data are hue, saturation, and + /// brightness. They are full unsigned 16-bit values as in Apple's + /// HSVColor data structure. Pure red = 0, 65535, 65535. + /// + HSB = 1, + /// + /// The four values in the color data are cyan, magenta, yellow, and + /// black. They are full unsigned 16-bit values. 0 = 100% ink. + /// + CMYK = 2, + /// + /// Pantone matching system (custom colorspace) + /// + Pantone = 3, + /// + /// Focoltone colour system (custom colorspace) + /// + Focoltone = 4, + /// + /// Trumatch color (custom colorspace) + /// + Trumatch = 5, + /// + /// Toyo 88 colorfinder 1050 (custom colorspace) + /// + Toyo88 = 6, + /// + /// The first three values in the color data are lightness, + /// a-chrominance, and b-chrominance. + /// + LAB = 7, + /// + /// The first value in the color data is the gray value, from 0...10000. + /// + Grayscale = 8, + /// + /// HKS colors (custom colorspace) + /// + HKS = 10 + } +} diff --git a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACODataFormat.cs b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACODataFormat.cs similarity index 67% rename from Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACODataFormat.cs rename to Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACODataFormat.cs index 00f49a48..de24c679 100644 --- a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACODataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACO/ACODataFormat.cs @@ -41,11 +41,19 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe _dfr.Capabilities.Add(typeof(PaletteObjectModel), DataFormatCapabilities.All); _dfr.Sources.Add("http://www.nomodes.com/aco.html"); _dfr.ExportOptions.Add(new CustomOptionNumber(nameof(Version), "_Version", 1, 1, ushort.MaxValue)); + /* + _dfr.ExportOptions.Add(new CustomOptionChoice(nameof(ColorSpace), "Color _space", true, new CustomOptionFieldChoice[] + { + new CustomOptionFieldChoice("RGB", ACOColorSpace.RGB, true), + new CustomOptionFieldChoice("CMYK", ACOColorSpace.CMYK) + })); + */ } return _dfr; } public ushort Version { get; set; } = 1; + // public ACOColorSpace ColorSpace { get; set; } = ACOColorSpace.RGB; protected override void LoadInternal(ref ObjectModel objectModel) { @@ -61,7 +69,7 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe // occupies five words: for (ushort i = 0; i < colorCount; i++) { - ushort colorSpace = br.ReadUInt16(); + ACOColorSpace colorSpace = (ACOColorSpace)br.ReadUInt16(); ushort w = br.ReadUInt16(); ushort x = br.ReadUInt16(); ushort y = br.ReadUInt16(); @@ -76,9 +84,8 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe switch (colorSpace) { - case 0: + case ACOColorSpace.RGB: { - // RGB int R = (w / 256); int G = (x / 256); int B = (y / 256); @@ -88,6 +95,36 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe palette.Entries.Add(entry); break; } + case ACOColorSpace.HSB: + { + // The first three values in the color data are hue, + // saturation, and brightness. They are full unsigned + // 16-bit values as in Apple's HSVColor data structure. + // Pure red = 0,65535, 65535. + double H = ((double)w / 256); + double S = ((double)x / 256); + double B = ((double)y / 256); + + PaletteEntry entry = new PaletteEntry(); + entry.Color = Color.FromHSL(H, S, B); + palette.Entries.Add(entry); + break; + } + case ACOColorSpace.CMYK: + { + // The four values in the color data are cyan, magenta, + // yellow, and black. They are full unsigned 16-bit + // values. + double C = ((double)(UInt16.MaxValue - w) / 256); + double M = ((double)(UInt16.MaxValue - x) / 256); + double Y = ((double)(UInt16.MaxValue - y) / 256); + double K = ((double)(UInt16.MaxValue - z) / 256); + + PaletteEntry entry = new PaletteEntry(); + entry.Color = Color.FromCMYKDouble(C, M, Y, K); + palette.Entries.Add(entry); + break; + } } } } @@ -107,16 +144,16 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe // occupies five words: foreach (PaletteEntry color in palette.Entries) { - ushort colorSpace = 0; // RGB - bw.WriteUInt16(colorSpace); + ACOColorSpace colorSpace = ACOColorSpace.RGB; + bw.WriteUInt16((ushort)colorSpace); switch (colorSpace) { - case 0: + case ACOColorSpace.RGB: { - ushort w = (ushort)((color.Color.R * 255) * 256); - ushort x = (ushort)((color.Color.G * 255) * 256); - ushort y = (ushort)((color.Color.B * 255) * 256); + ushort w = (ushort)(color.Color.GetRedByte() * 256); + ushort x = (ushort)(color.Color.GetGreenByte() * 256); + ushort y = (ushort)(color.Color.GetBlueByte() * 256); ushort z = 0; bw.WriteUInt16(w); diff --git a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACT/ACTDataFormat.cs b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACT/ACTDataFormat.cs new file mode 100644 index 00000000..90c5f733 --- /dev/null +++ b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ACT/ACTDataFormat.cs @@ -0,0 +1,106 @@ +// +// ACTDataFormat.cs +// +// Author: +// Michael Becker +// +// Copyright (c) 2021 Mike Becker's Software +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +using MBS.Framework.Drawing; +using UniversalEditor.IO; +using UniversalEditor.ObjectModels.Multimedia.Palette; + +namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe.ACT +{ + public class ACTDataFormat : DataFormat + { + public bool IncludeExtendedInformation { get; set; } = false; + + public ushort UsableColorCount { get; set; } = 0; + public ushort TransparencyColorIndex { get; set; } = 0; + + private static DataFormatReference _dfr; + protected override DataFormatReference MakeReferenceInternal() + { + if (_dfr == null) + { + _dfr = base.MakeReferenceInternal(); + _dfr.Capabilities.Add(typeof(PaletteObjectModel), DataFormatCapabilities.All); + } + return _dfr; + } + + protected override void LoadInternal(ref ObjectModel objectModel) + { + PaletteObjectModel palette = (objectModel as PaletteObjectModel); + if (palette == null) throw new ObjectModelNotSupportedException(); + + Reader reader = Accessor.Reader; + + int nColors = 0; + while (!reader.EndOfStream) + { + byte r = reader.ReadByte(); + byte g = reader.ReadByte(); + byte b = reader.ReadByte(); + palette.Entries.Add(new PaletteEntry(Color.FromRGBAByte(r, g, b))); + + nColors++; + if (nColors == 256) + { + if (!reader.EndOfStream) + { + UsableColorCount = reader.ReadUInt16(); + TransparencyColorIndex = reader.ReadUInt16(); + + IncludeExtendedInformation = true; + } + } + } + } + + protected override void SaveInternal(ObjectModel objectModel) + { + PaletteObjectModel palette = (objectModel as PaletteObjectModel); + if (palette == null) throw new ObjectModelNotSupportedException(); + + Writer writer = Accessor.Writer; + for (int i = 0; i < Math.Min(palette.Entries.Count, 768); i++) + { + writer.WriteByte(palette.Entries[i].Color.GetRedByte()); + writer.WriteByte(palette.Entries[i].Color.GetGreenByte()); + writer.WriteByte(palette.Entries[i].Color.GetBlueByte()); + } + + int remaining = 768 - palette.Entries.Count; + if (remaining > 0) + { + for (int i = 0; i < remaining; i++) + { + writer.WriteByte(0); + writer.WriteByte(0); + writer.WriteByte(0); + } + } + + if (IncludeExtendedInformation) + { + writer.WriteUInt16(UsableColorCount); + writer.WriteUInt16(TransparencyColorIndex); + } + } + } +} diff --git a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ASEDataFormat.cs b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/SwatchExchange/ASEDataFormat.cs similarity index 76% rename from Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ASEDataFormat.cs rename to Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/SwatchExchange/ASEDataFormat.cs index 6f16ecbb..21f36fdb 100644 --- a/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/ASEDataFormat.cs +++ b/Plugins/UniversalEditor.Plugins.Multimedia/DataFormats/Multimedia/Palette/Adobe/SwatchExchange/ASEDataFormat.cs @@ -26,7 +26,7 @@ using MBS.Framework.Drawing; using UniversalEditor.IO; using UniversalEditor.ObjectModels.Multimedia.Palette; -namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe +namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe.SwatchExchange { /// /// Provides a for manipulating color palettes in Adobe ASE format. @@ -128,7 +128,33 @@ namespace UniversalEditor.DataFormats.Multimedia.Palette.Adobe protected override void SaveInternal(ObjectModel objectModel) { - throw new NotImplementedException(); + PaletteObjectModel palette = (objectModel as PaletteObjectModel); + if (palette == null) throw new ObjectModelNotSupportedException(); + + IO.Writer writer = base.Accessor.Writer; + writer.Endianness = IO.Endianness.BigEndian; + writer.Accessor.DefaultEncoding = Encoding.UTF8; + writer.WriteFixedLengthString("ASEF"); + + writer.WriteInt16(1); + writer.WriteInt16(0); + + writer.WriteInt32(palette.Entries.Count); + foreach (PaletteEntry entry in palette.Entries) + { + writer.WriteUInt16(0x0001); + writer.WriteInt32(16 + entry.Name.Length); // colorName.Length + colorModel.Length + R G B + writer.WriteUInt16((ushort)entry.Name.Length); + writer.WriteFixedLengthString(entry.Name); + + // only RGB supported right now + writer.WriteFixedLengthString("RGB "); + writer.WriteSingle((float)entry.Color.R); + writer.WriteSingle((float)entry.Color.G); + writer.WriteSingle((float)entry.Color.B); + + writer.WriteUInt16(2); // colorType = Normal + } } } }