I'm working on a Photoshop plugin using c# (with Adobe Photoshop Object Library). I'm trying to loop through all the pixels in my active document, but with the code I currently have it takes a very long time to even go through 1 row of pixels in a 256x256 file. I was wondering if there might be a better (faster) way than using ColorSampler for this? (c++ plugin?)
This is what I currently have: (http://forums.adobe.com/thread/290211) (C# Equivalent to this code)
static void Main(string[] args)
{
ps.Application app = new ps.Application();
app.Preferences.RulerUnits = ps.PsUnits.psPixels;
ps.Document activeDoc = app.ActiveDocument;
double docWidth = activeDoc.Width;
double docHeight = activeDoc.Height;
double xPos = 0.0D;
double yPos = 0.0D;
activeDoc.ColorSamplers.RemoveAll();
var pixelLoc = new object[] { 0.0D, 0.0D };
ps.ColorSampler myColorSampler = activeDoc.ColorSamplers.Add(pixelLoc);
var stopwatch = Stopwatch.StartNew();
ps.SolidColor myColor;
double red;
double green;
double blue;
for (xPos = 0.0D; xPos <=docWidth - 1; xPos++)
{
myColor = myColorSampler.Color;
red = myColor.RGB.Red;
green = myColor.RGB.Green;
blue = myColor.RGB.Blue;
pixelLoc[0] = xPos;
myColorSampler.Move(pixelLoc);
}
Console.WriteLine(stopwatch.ElapsedMilliseconds); //11000 ms for 1 row of 256 pixels
Console.ReadLine();
}
I have not worked with this API before, but I found this website: Adobe Photoshop CS5 Object Library.
I did some looking and there is a SaveAs method that can be used on the Document object. You can use this method to save the document as an image (let's say a BITMAP).
From there, you can use several sources online that show how to extract pixel information quickly from a BMP.
Here is one particular article: Work with Bitmaps faster in C#
I realize that I am not providing copy/pasteable code for you to use, but I hope this points you in the correct direction.