public static Bitmap LoadImageFromCache(string sFile) { Bitmap zBitmap; string sKey = sFile.ToLower(); if (!s_dictionaryImages.TryGetValue(sKey, out zBitmap)) { // only buffer 20 images max (make this an option?) if (s_dictionaryImages.Count > IMAGE_CACHE_MAX) { DumpImages(); } if (!File.Exists(sFile)) { sFile = ProjectManager.Instance.ProjectPath + sFile; if (!File.Exists(sFile)) { return(null); } } Bitmap zSourceImage; try { switch (Path.GetExtension(sFile).ToLower()) { case ".psd": { var zFile = new PhotoshopFile.PsdFile(); zFile.Load(sFile); zSourceImage = PhotoshopFile.ImageDecoder.DecodeImage(zFile); } break; default: zSourceImage = new Bitmap(sFile); break; } } catch (Exception) { // return a purple bitmap to indicate an error zBitmap = new Bitmap(1, 1); Graphics.FromImage(zBitmap).FillRectangle(Brushes.Purple, 0, 0, zBitmap.Width, zBitmap.Height); return(zBitmap); } zBitmap = new Bitmap(zSourceImage.Width, zSourceImage.Height); // copy the contents into the image Graphics zGraphics = Graphics.FromImage(zBitmap); zGraphics.DrawImage(zSourceImage, new Rectangle(0, 0, zBitmap.Width, zBitmap.Height), 0, 0, zBitmap.Width, zBitmap.Height, GraphicsUnit.Pixel); zSourceImage.Dispose(); // allow the file to change zBitmap.Tag = (float)zBitmap.Width / (float)zBitmap.Height; // backup the aspect ratio // cache it! s_dictionaryImages.Add(sKey, zBitmap); } return(zBitmap); }
public static void ConvertTestPSD() { var stream = System.IO.File.OpenRead("Content/Entities/Dwarf/Layers/test.psd"); var psd = new PhotoshopFile.PsdFile(stream, new PhotoshopFile.LoadContext()); if (FindPalette("Hair 02").HasValue(out var conversionPalette)) { foreach (var layer in psd.Layers) { var channels = new List <PhotoshopFile.Channel>(); channels.Add(layer.Channels.Where(c => c.ID == 0).FirstOrDefault()); channels.Add(layer.Channels.Where(c => c.ID == 1).FirstOrDefault()); channels.Add(layer.Channels.Where(c => c.ID == 2).FirstOrDefault()); channels.Add(layer.AlphaChannel); var rawMemText = new MemoryTexture(layer.Rect.Width, layer.Rect.Height); for (var index = 0; index < layer.Rect.Width * layer.Rect.Height; ++index) { if (channels[3].ImageData[index] == 0) { rawMemText.Data[index] = new Color(0, 0, 0, 0); } else { rawMemText.Data[index] = new Color(channels[0].ImageData[index], channels[1].ImageData[index], channels[2].ImageData[index], channels[3].ImageData[index]); } } var memTex = new MemoryTexture(psd.ColumnCount, psd.RowCount); TextureTool.Blit(rawMemText, new Rectangle(0, 0, layer.Rect.Width, layer.Rect.Height), memTex, new Point(layer.Rect.X, layer.Rect.Y)); var decomposed = TextureTool.DecomposeTexture(memTex, conversionPalette.CachedPalette); var composed = TextureTool.ComposeTexture(decomposed, BasePalette.CachedPalette); TextureTool.Blit(composed, new Rectangle(layer.Rect.X, layer.Rect.Y, layer.Rect.Width, layer.Rect.Height), rawMemText, new Rectangle(0, 0, rawMemText.Width, rawMemText.Height)); for (var index = 0; index < rawMemText.Width * rawMemText.Height; ++index) { channels[0].ImageData[index] = rawMemText.Data[index].R; channels[1].ImageData[index] = rawMemText.Data[index].G; channels[2].ImageData[index] = rawMemText.Data[index].B; channels[3].ImageData[index] = rawMemText.Data[index].A; } foreach (var channel in layer.Channels) { channel.ImageDataRaw = null; } } } psd.PrepareSave(); psd.Save("processed.psd", Encoding.Unicode); }
public static List <PhotoshopLayer> LoadPSD(System.IO.FileStream Stream) { var psd = new PhotoshopFile.PsdFile(Stream, new PhotoshopFile.LoadContext()); var r = new List <PhotoshopLayer>(); foreach (var layer in psd.Layers) { var entry = new PhotoshopLayer(); entry.LayerName = layer.Name; // Need to expand the PSD layer to the size of the image as transparent rows/columns are trimmed off in PSD. entry.Data = new MemoryTexture(psd.ColumnCount, psd.RowCount); TextureTool.Blit(PSDLayerToMemoryTexture(layer), new Rectangle(0, 0, layer.Rect.Width, layer.Rect.Height), entry.Data, new Point(layer.Rect.X, layer.Rect.Y)); r.Add(entry); } return(r); }
public static Bitmap LoadImageFromCache(string sFile) { Bitmap zBitmap; string sKey = sFile.ToLower(); if (!s_dictionaryImages.TryGetValue(sKey, out zBitmap)) { // only buffer 20 images max (make this an option?) if (s_dictionaryImages.Count > IMAGE_CACHE_MAX) { DumpImages(); } if (!File.Exists(sFile)) { sFile = CardMakerMDI.ProjectPath + sFile; if (!File.Exists(sFile)) return null; } Bitmap zSourceImage; try { switch (Path.GetExtension(sFile).ToLower()) { case ".psd": { var zFile = new PhotoshopFile.PsdFile(); zFile.Load(sFile); zSourceImage = PhotoshopFile.ImageDecoder.DecodeImage(zFile); } break; default: zSourceImage = new Bitmap(sFile); break; } } catch (Exception) { // return a purple bitmap to indicate an error zBitmap = new Bitmap(1, 1); Graphics.FromImage(zBitmap).FillRectangle(Brushes.Purple, 0, 0, zBitmap.Width, zBitmap.Height); return zBitmap; } zBitmap = new Bitmap(zSourceImage.Width, zSourceImage.Height); // copy the contents into the image Graphics zGraphics = Graphics.FromImage(zBitmap); zGraphics.DrawImage(zSourceImage, new Rectangle(0, 0, zBitmap.Width, zBitmap.Height), 0, 0, zBitmap.Width, zBitmap.Height, GraphicsUnit.Pixel); zSourceImage.Dispose(); // allow the file to change zBitmap.Tag = (float)zBitmap.Width / (float)zBitmap.Height; // backup the aspect ratio // cache it! s_dictionaryImages.Add(sKey, zBitmap); } return zBitmap; }