GetPixel() method
Hi,
Is there any way to get a color channel from a pixel? Or how
can I get the R, G, B values in different variables?
Regards,
Carlos
this might speed it up a bit. having that method call in the comparison slows things down.
int height = source.getHeight();
int width = source.getWidth();
for (y=0; y<height; y++)
for (x=0; x<width; x++)
val = source.getPixel(x,y);
val = (val<threshold) ? 0 :
d) ? 0 : background.WHITE;
dest.putPixel(x,y,val);
//System.out.println(count++);
///////////getPixel method called through
source////////////////
public int getPixel (int x, int y) {
return pixels [(y*width)+x];
//////////puPixel method called through dest
public void putPixel (int x, int y, int value) {
pixels [(y*width)+x] = value;
Similar Messages
-
Why does GetPixel stop working in my program?
Hi All
I have a strange problem. I am using GetPixel to perform a screen scrape which takes about half an hour to get all the data I need once a week. At least it WOULD take half an hour if it ever finished! It always crashes after about 20 minutes, but I can then
restart from the point of the crash and it completes fine.
Here is my code:
Do
lColor = GetPixel(DC, x, y)
If lColor >= 0 Then Exit Do
DC = GetDC(0)
Loop
I know that GetPixel / DC can sometimes drop out and return -1 hence the above loop, but it seems that after about 8 million calls (about 20 minutes in my program) it just starts returning zero every single time. This causes an exception elsewhere in my program
but the underlying problem is GetPixel returning zero. I have watched this happen on several occasions now and the pixels being interrogated are most definitely not black and the screensaver has not come on ar anything like that.
Does anybody have any ideas why this might be happening and more importantly, how I can fix it?
Many thanks
RichCan I ask you if you are getting more than one pixel -at a time-? If you are, then I have some good news for you, you can do this considerably faster if you were to use BitMap.Lockbits. The reason being that each time you call the GetPixel method, internally
lockbits gets called to first lock the resource, and then again to unlock it, all for a single GetPixel operation. Implementing your own routine to gather pixels can be considerably faster, but admittedly more difficult. Below the following example will show
you how to get large collections of pixels with a single call to Bitmap.Lock, and Bitmap.Unlock, which will DRASTICALLY increase your overall performance. Please be advised that this example only supports 32 bit per pixel images, and if you wish to add support,
you would have to first understand how this works.
Example:
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Width = 101
PictureBox1.Height = 101
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim rTopLeft As New Rectangle(0, 0, 50, 50)
Dim rTopRight As New Rectangle(50, 0, 50, 50)
Dim rBottomLeft As New Rectangle(0, 50, 50, 50)
Dim rBottomRight As New Rectangle(50, 50, 50, 50)
g.FillRectangle(Brushes.Red, rTopLeft)
g.FillRectangle(Brushes.Blue, rTopRight)
g.FillRectangle(Brushes.Yellow, rBottomLeft)
g.FillRectangle(Brushes.Green, rBottomRight)
PictureBox1.Image = bmp
Dim getPixel1 As New Point(0, 0)
Dim getPixel2 As New Point(50, 0)
Dim getPixel3 As New Point(0, 50)
Dim getPixel4 As New Point(50, 50)
Dim pixelsToGet As New List(Of Point)
pixelsToGet.Add(getPixel1)
pixelsToGet.Add(getPixel2)
pixelsToGet.Add(getPixel3)
pixelsToGet.Add(getPixel4)
Dim pixels As List(Of Pixel) = GetPixels(pixelsToGet, bmp)
pbPix1.BackColor = pixels(0).Color
pbPix2.BackColor = pixels(1).Color
pbPix3.BackColor = pixels(2).Color
pbPix4.BackColor = pixels(3).Color
End Sub
Function GetPixels(pixelsToGet As List(Of Point), source As Bitmap) As List(Of Pixel)
Dim results As New List(Of Pixel)
Dim sourceRect As New Rectangle(0, 0, source.Width - 1, source.Height - 1)
Dim bmd As System.Drawing.Imaging.BitmapData = _
source.LockBits(sourceRect, Imaging.ImageLockMode.ReadOnly, source.PixelFormat)
Dim ptr As IntPtr = bmd.Scan0
Dim bytes As Integer = Math.Abs(bmd.Stride) * bmd.Height
Dim allPixelBytes(bytes - 1) As Byte
System.Runtime.InteropServices.Marshal.Copy(ptr, allPixelBytes, 0, bytes)
Dim bytesPerPixel As Integer = 0
Select Case bmd.PixelFormat
Case Imaging.PixelFormat.Format32bppArgb
bytesPerPixel = 4
Case Else
Throw New NotImplementedException("Currently only 32bits per pixel is supported!")
End Select
For currentIndex As Integer = 0 To allPixelBytes.Count - 1 Step bytesPerPixel
Select Case bmd.PixelFormat
Case Imaging.PixelFormat.Format32bppArgb
For Each Loc As Point In pixelsToGet
If PixelLocationToIndex(Loc, bmd.PixelFormat, source.Width) = currentIndex Then
Dim blueComponent As Byte = allPixelBytes(currentIndex)
Dim greenComponent As Byte = allPixelBytes(currentIndex + 1)
Dim redComponent As Byte = allPixelBytes(currentIndex + 2)
Dim alphaComponent As Byte = allPixelBytes(currentIndex + 3)
Dim pixelColor As Color = Color.FromArgb(alphaComponent, redComponent, greenComponent, blueComponent)
Dim pixel As New Pixel(Loc, pixelColor)
results.Add(pixel)
End If
Next
Case Else
Throw New NotImplementedException("Currently only 32bits per pixel is supported!")
End Select
Next
source.UnlockBits(bmd)
Return results
End Function
Public Function PixelLocationToIndex(loc As Point, pixelFormat As System.Drawing.Imaging.PixelFormat, bitmapWidth As Integer) As Integer
Dim bytesPerPixel As Integer = 0
Select Case pixelFormat
Case Imaging.PixelFormat.Format32bppArgb : bytesPerPixel = 4
Case Else : Throw New NotImplementedException("Currently only 32bits per pixel is supported!")
End Select
Dim bytesPerRowOfPixel As Integer = bytesPerPixel * bitmapWidth
Dim yPixels As Integer = bytesPerRowOfPixel * loc.Y
Dim xPixels As Integer = bytesPerPixel * loc.X
Return xPixels + yPixels
End Function
Class Pixel
Public Property Location As Point
Public Property Color As Color
Sub New(location As Point, color As Color)
Me.Location = location
Me.Color = color
End Sub
End Class
End Class
“If you want something you've never had, you need to do something you've never done.”
Don't forget to mark
helpful posts and answers
! Answer an interesting question? Write a
new article
about it! My Articles
*This post does not reflect the opinion of Microsoft, or its employees. -
Useless code in java.awt.image.SampleModel.java?
Hey there,
i just looked up the sourcecode of java.awt.image.SampleModel.java in JDK 6
I discovered two issues i'd like to discuss.
1) on lines 736 to 739 this code is stated:
if (iArray != null)
pixels = iArray;
else
pixels = new int[numBands * w * h];
I asked myself, why does this code exist? while the getPixels() method is overwritten twice by double[] getPixels() and float[] getPixels, it is impossible to reach the part of the java code that initializes the pixels-array. One could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO.
the java developers could get a little more performance out of this method if the if statement would be cut out - especially when reading a lot of very small rasters
or, on the other hand, they could replace this piece of code by an explicit bounds check.
When somebody touches this code, i would appreciate it if the errormessage "index out of bounds!" could be rewritten to be a little more verbose, like: Index out of bounds(x=123; y=456, maxX=100, maxY=400)!(numbers are just examples)
I hope i didn't miss something very basic and could help improving this class a little bit.
2) the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention.
best regards
kdotOne could only step into that line if "null" is given for the i/d/fArray-parameter. but if one would do so, the Java parser couldn't determine, which method is to use. so this part of code is just useless IMHO. You can have
sampleModel.getPixels(x,y,w,h,(int[]) null, dataBuffer);No ambiguity on which method to use.
the local variable Offset(line 734) is coded against code conventions which say, variables shall start with a lowercase letter. Offset obviously doesn't fit that convention. You're correct, offset is against coding conventions. So are many other examples scattered throughout the jdk source code. For example, Hashtable should be called HashTable. In some cases the coding conventions might not have been established when the original code was written. In other cases it might have been human error. In yet other cases the conventions were probably ignored. The person who wrote the SampleModel class did so some 10+ years ago (Java 1.2). Who knows what he/she was thinking at the time, and in all honesty - does it really matter in this case?
Did you know there are some classes that declare unused local variables (ahem ColorConvertOp)? Some also have unused imports ( *** cough *** BufferedImage *** cough *** ). In essence, the jdk source code is not the epidemy of code correctness. But it's still pretty good. -
MIDP 1.0: Image to byte[] or serializing...
I would like to extract pixels from a MIDP 1.0 "Image" object through any means possible. I am currently under the impression that "Image.toString()" would just do something lame, like giving me a string like "Image@1234" that identifies the class and the instance -- which could in principle be used for mere instance comparison. Anyhow, if I could serialize an Image object to a file, and then read back the file in to a byte array, then I would be delighted. I want to stress that I don't care how convoluted the approach may be, as long as it is in plain-vanilla J2ME Java code for MIDP 1.0 and is capable of working on an actual device (as opposed to an emulator). Can I cast the Image to Object, and then somehow convert to byte[]? I don't care if I have to get down and dirty, hacking the platform-specific bytes of the full Object manifestation of the Image. Anyhow, I want to make it clear that any hard core hack, doing all kinds of things that would break on different VMs, etc, is totally fine. Messy is fine. I just want access to the binary data of the entire Image object, or, even better (but only icing on the proverbial cake), access to pixel data. If I can get the byte[] representation of Image, then I can hack the rest myself.
My goal was do a capture of the way things actually rendered on a device, kind of like a "screen capture", only a little more indirect (since I don't think there is a way to get access to the pixels of the LCD). What I have to do is render to an off-screen Image object and then retrieve pixel data from it using the platform-dependent getPixels() supplied by Motorola. You're going to think I'm crazy, but I send this image data to a web server via POSTs, which get collected and stored as a single file on my web server. Voila! A screen capture of how things actually render to an Image object on an actual device.
Everything works, except...
I can't get the Motorola "getPixels()" method to work in their emulator, so I'm worried
that it won't work on my Motorola T720 (something I haven't actually tried, though).
I get a "Uncaught Exception: java/lang/Error" message from the T720 emulator
when I run one of the Motorola demos (built using their scripts) when I add the
following code to the Canvas paint() method:
if (0 == iDidThis) { iDidThis = 1;
try
{ javax.microedition.lcdui.Image temp = Image.createImage( 120, 160 );
int [] rgbData = new int [120*160];
com.motorola.game.ImageUtil.getPixels ( temp, 0, 0, 1, 1, rgbData );
System.out.println("Captured Data");
catch( java.lang.ArrayIndexOutOfBoundsException e )
{ e.printStackTrace(); } }
This code runs only once, when the paint() method of the Canvas object is called for
the first time. Okay, I get the error message, but the "Bounce" demo (to which I added
the code block above) goes on to run just fine. (NOTE: The message "Captured Data"
is NOT printed in this scenario.)
I don't understand why the public static "com.motorola.game.ImageUtil.getPixels()"
method causes the "java/lang/Error" exception. (By the way, if I comment out the
call to this method, the code totally works without error, and I see the printed
statement "Captured Data" in the command window associated with the emulator.)
If my getPixels() call resulted in bad array access, then I would get the
java.lang.ArrayIndexOutOfBoundsException exception. But apparently this is
not what is going on.
I am really rusty with J2ME, so I don't even know the meaning of "java/lang/Error".
I assume it is different from a "class not found" exception, but I suspect that my
problem has to do with a fault in the emulator trying to execute the code for the
"com.motorola.game.ImageUtil.getPixels()" method.
My Motorola emulator (v7.5) is from around December, 2002, so it may be really old, and
maybe this was a known issue at that time that has since been resolved. I'm just never
tried to use platform-specific J2ME APIs before, so I don't know if what I am observing
is my fault, or is an emulator issue.
I'd be grateful for any ideas about what might be going wrong and for ideas about
possible solutions. -
Can I render a Flash frame as a bitmap and send it over TCP/IP?
I want to draw some combination of bitmaps, flash shapes, vectors, text etc and draw them programmatically into my movie using ActionScript, and then get every "rendered" pixel of my movie (at 100% view) into an array that I send to another program over TCP/IP. Can anyone help me here?
The first part of the question is if its possible to render a frame that is a collection of Flash elements as a bitmap? I know that I can iterate through a bitmap and get every pixel using the getPixel method but I'm trying to access the final rendered frame displayed on my screen, including, as I said Flash shapes and text.
The second question is what would be the best way to send this "video frame" over TCP/IP? Would I use an XML socket connection? That is the only way I know how to send data out of Flash over TCP/IP but I don't know if it is the only way - the help page says the data needs to be formatted as XML, which seems unwieldly for this application.
I'm doing this now by using Max/MSP/Jitter to do a screen capture the size and location of my Flash movie and then send out the resulting matrix using a "jit.net.send" object (which lets you send frames of video over TCP/IP), but this is too clumsy for the installation I am building.
Any help would be greatly appreciated!
-bobThanks! That was exactly what I was looking for in the first half of the question. And I guess the draw() method works in AS2 as well as AS3. I will test it but from what I understand, I can use a Bitmap as kind of my bottom-level container, then create (in ActionScript) a movie clip that can contain other movie clips or drawing API commands or whatever, animate these various elements then finally draw() them into the bitmap. I see that in AS3 I can even use getPixels or getVector to capture the full "video frame" in one line of code.
Thanks rothrock, for the helpful links. My application is kind of unusual, in that I'm sending "video" data to a proprietary LED controller box for display on a low resolution LED display. I connect to the box over TCP/IP and send it "frames of video" 20 times a second. I got this to work with this other content and software (Max/MSP/Jitter) and I want to see how I could capture all the rendered pixel data in successive frames of Flash animation, format it properly, then send it over TCP/IP to this controller box. Don't really now enough about the various flavors of TCP/IP connections to figure out how to do this in Flash, and I know there are security limitations built in to Flash that have to be worked around. But my movies are small (say 96 pixels by 72 pixels) and I need to send uncompressed data directly to my controller box, so these interesting links showing how to use server-side scripts to create jpgs or pngs are probably not going to help me. I will hopefully find a higher-level programmer than myself to work this out, and I think some of the techniques here will hopefull prove to be helpful.
Thanks again for your help! -
Hi to every one..
m new at j2me and at this forum too... and have started working on face
recognition in j2me..
but for now i want to do a pixel by pixel matching of two images..
can i do it with immutable images? or necessary to do it with mutable ones?
and one more question plz..
if i've displayed the image like this
image = Image.createImage("/randy_orton.png");
imageItem = new ImageItem(null, image,
ImageItem.LAYOUT_NEWLINE_BEFORE
| ImageItem.LAYOUT_LEFT
| ImageItem.LAYOUT_NEWLINE_AFTER,null);
form.append(imageItem);
how can i now get the pixels of it or bytes of this image..
plz gime ur suggestions ..
thanks
a small piece of code with a bit of discription would be of greater help..
thanks in advance,,i went thorugh that API ahve searched some more things as well.. I have listened about the getRGB() and getPixels methods for it.. If you found a getPixels method, you're looking at the wrong API. I told you to
Read the API for javax.microedition.lcdui.Image
Not any of the classes in java.awt.image.
but dont know how to use those...Do you know what an array is? How to access and assign values of array elements? If not, then go back to basics and go through some tutorials. Start here:
{color:#0000ff}http://java.sun.com/docs/books/tutorial/{color}
db -
Hi
i am creating flash project .Its a Drawing API project where
i need eyedropper ,which can trace color code from stage just like
flash eyedropper work .Please give some idea about that .How can i
create it.
thanks with regards
shobha1you can retrieve color data from displayobjects using the
bitmapdata class, its draw() method and getPixel() method. create a
custom cursor that looks like the eyedropper and hide the cursor
for the full effect. -
Get the color of a pixel on the Stage
Is there a way to retrieve the color of a generic pixel on the screen using the mouse pointer inside the Stage of Flash Player? With the getPixel method of the BitmapData class you can get the color of a pixel only if the mouse is over a bitmap image, but what if I need the color a generic pixel on the screen?
this takes a snapshot of the pixel below the mouse:
function eventHandler(event:MouseEvent):void
var bmd:BitmapData = new BitmapData(1, 1, false, 0x000000);
var matrix:Matrix = new Matrix();
matrix.translate(event.stageX, event.stageY);
//can never remember which way translate works so it could be:
//matrix.translate(-event.stageX, -event.stageY);
bmd.draw(stage, matrix);
var pixelColour:uint = bmd.getPixel(0, 0);
my blog has more info: http://blog.leeburrows.com/2010/09/bitmapdata-basics-1/ -
Gettin Pixel data from png image
Hi,
Has anyone worked with the byte data returned my the getSnapshot() method of the VideoControl (MMAPI). I believe the default is png( i have tested this on Nokia 3650 and 6600 and on Siemens SX1).
In all the devices that support Nokia UI library i can get the pixel values from the byte data by using the com.nokia.mid.ui library, specifically the DirectGraphics.getPixels() method.
In MIDP 2.0 devices i can do this using the Image.getRGB method.
But what about MIDP 1.0.3 devices that do not have the Nokia library ? i believe that the byte data returned by the camera is of raw png format. I have never done this before and so far my search on the web has not been fruitful.
Can anyone point me to information abt the raw png Image (number of bytes of header ... etc) so that i can write my own code to extract the information or better yet point me to the code that does this.http://www.wotsit.org/
shmoove -
Click to scroll to a point of the stage on a parallax website using edge commons
Hi Edge community,
I need help to finish a parallax website that I created using the edgecommons library.
this is what I've done up till now:
Le Papillon - Singapore
I'd like to add the possibility to smoothly scroll to a certain point/percentage of the scrollbar by clicking on a link in a (future) fixed navbar instead of using the mouse wheel.
Which is the best way to achive this?
Thank you,
Ettorelol, I went and tried the getPixel() method for it, but using Stage.getPixel(100,100) or _root.getPixel(100,100) returned undefined.
So I did more research, and I found bitmap.draw(), too bad I could have just checked the forum again. :3
its exactly what I wanted, thanks for your help, kglad. -
Hi there. :)
I've successfully loaded a bunch of images into Image objects in my HelloWorld applet; my next problem is getting that magic pink background to not show up.
I looked through the methods in Java2D, and can't seem to find any that do what I want? Is there a setPixel() or getPixel() method that I'm missing? I just need to check if a pixel is pink, and then change the alpha from 1 to 0, I think.
But if I can't check the pixel colour, I'm not sure how I do that. I looked for a replaceColour() method, but couldn't seem to find one of those either.
Thanks. :D
-KramyThis method will redraw a buffered image, setting the color you specified to be transparent.
public BufferedImage makeImageTransparent (BufferedImage image, Color color) {
BufferedImage dimg = new BufferedImage (image.getWidth (), image.getHeight (), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = dimg.createGraphics ();
g.setComposite (AlphaComposite.Src);
g.drawImage (image, null, 0, 0);
g.dispose ();
for (int i = 0; i < dimg.getHeight (); i++) {
for (int j = 0; j < dimg.getWidth (); j++) {
if (dimg.getRGB (j, i) == color.getRGB ()) {
dimg.setRGB (j, i, 0x8F1C1C);
return dimg;
} -
Hi,
I want to create animation. I want to slice image into random number of rectangles (it can't be squares) and I want to animate all of this rectangles at once.
Any ideas?
Best whishes,
sledzUse BitmapData class:
read original BitmapData --> read pixels that fit into rectangles (getPixels method) --> create Bitmaps out of individual rectangle's BitmapData --> animate Bitmaps.
Documentation:
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.htm l
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/display/Bitmap.html
Also search for BotmapData tutorials - there are a lot of them. -
Another trick of using array and getpixel
Hi,
this is my reference to get Median 5 x 5px (Horizontal and Vertical Diverence Value)
this is my code,.
public static int[] limitMed(Bitmap myBmp)
//Input: Bitmap 68 x 68px (Grayscale)
//Output: Array of Median 5 x 5px (Horizontal and Vertical Diverence Value) - Just like the picture
//Ignore 2px to each sides (i just need 64 x 64px inside)
int[] myValue = new int[(myBmp.Width - 4) * (myBmp.Height - 4)];//Array for Output Value
int[] tempHValue = new int[20];//Array for Horizontal Diverence Value
int[] tempVValue = new int[20];//Array for Vertical Diverence Value
int countV = 0;// Count Value
for (int j = 2; j < myBmp.Height - 2; j++)//Ignore 2px top and bottom side
for (int i = 2; i < myBmp.Width - 2; i++)//Ignore 2px left and right side
int countHT = 0;
for (int y = j - 2; y <= j + 2; y++)
for (int x = i - 2; x < i + 2; x++)
Color myClr1 = myBmp.GetPixel(x, y);
Color myClr2 = myBmp.GetPixel(x + 1, y);
tempHValue[countHT] = Math.Abs(myClr1.R - myClr2.R);//Get horizontal diverence value
countHT++;
List<int> listH = new List<int>(tempHValue);//Add horizontal diverence value to List
int countVT = 0;
for (int y = i - 2; y <= i + 2; y++)
for (int x = j - 2; x < j + 2; x++)
Color myClr1 = myBmp.GetPixel(y, x);
Color myClr2 = myBmp.GetPixel(y, x + 1);
tempVValue[countVT] = Math.Abs(myClr1.R - myClr2.R);//Get vertical diverence value
countVT++;
List<int> listV = new List<int>(tempVValue);//Add vertical diverence value to List
var result = listH.Concat(listV);//Combine Lists
List<int> resultList = result.ToList();
resultList.Sort();//Sort List
List<int> rangeList = resultList.GetRange(19, 2);//Get 19th and 20th value
myValue[countV] = (int)rangeList.Average();//Add Median to Array
countV++;
return myValue;// return Array
this code is running but not too fast,
there is any idea to repair this code?
Thanks!//////////////////////// // TODO: Your per-pixel code here. // The pixel contains 8-bit non-linear (sRGB) data. // See wikipedia: http://en.wikipedia.org/wiki/SRGB // for conversions betwen sRGB and Linear RGB. // Do your arithmetic on linear RGB values
only.
// pixel[0]: (sRGB) RED value
// pixel[1]: (sRGB) GREEN value
// pixel[2]: (sRGB) BLUE value
//////////////////////// pixel += bytesPerPixel;
} } ); } myBitmap.UnlockBits( bmdata );
Hi,
afaik the order is reversed BGR format so p[0] is blue - at least for 32bppArgb locked bmps.
Here's my Median Filter for Bitmaps using unsafe code (This does something else than the OPs code, it doesnt "middle" the differences but the color channel values - as a Filter for images, can be optimized)
private void button1_Click(object sender, EventArgs e)
MedianFilter((Bitmap)this.pictureBox1.Image, 9 /* 9x9 Kernel */, true /* if pic doesnt contain transparecy, set this to false */, 255, null);
this.pictureBox1.Refresh();
//Median Filter, processing all pixels (edge/near-edge values too)
//Conveniant, fast, but not fastest implementation (due to the variable size of the kernel to imput into the method).
//params:
//InputBitmap,
//KernelSize,
//take median for alpha channel,
//maxSize of KernelRow/Column,
//a reference to a BackgroundWorker to cancel/stop the opertation
public static bool MedianFilter(Bitmap bmp, int size, bool doTrans, int maxSize, System.ComponentModel.BackgroundWorker bgw)
if ((size & 0x1) != 1)
throw new Exception("Kernelrows Length must be Odd.");
if (size < 3)
throw new Exception("Kernelrows Length must be in the range from 3 to" + (Math.Min(bmp.Width - 1, bmp.Height - 1)).ToString() + ".");
if (size > maxSize)
throw new Exception("Kernelrows Length must be in the range from 3 to" + maxSize.ToString() + ".");
if (size > Math.Min(bmp.Width - 1, bmp.Height - 1))
throw new Exception("Kernelrows Length must be in the range from 3 to" + (Math.Min(bmp.Width - 1, bmp.Height - 1)).ToString() + ".");
int h = size / 2;
Bitmap bSrc = null;
BitmapData bmData = null;
BitmapData bmSrc = null;
try
bSrc = (Bitmap)bmp.Clone();
bmData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;
unsafe
int nWidth = bmp.Width;
int nHeight = bmp.Height;
int llh = h * stride;
int lh = h * 4;
# region First Part
for (int l = 0; l < h; l++)
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
int lf = l * stride;
int ll = l * 4;
byte[] red = new byte[(h + 1) * (h + l + 1)];
byte[] green = new byte[(h + 1) * (h + l + 1)];
byte[] blue = new byte[(h + 1) * (h + l + 1)];
byte[] alpha = new byte[(h + 1) * (h + l + 1)];
int mid = red.Length / 2;
for (int r = h - l, rc = 0; r < size; r++, rc++)
int lr = rc * stride;
for (int c = h, cc = 0; c < size; c++, cc++)
int lcc = cc * 4;
blue[rc * h + cc] = pSrc[lr + lcc];
green[rc * h + cc] = pSrc[1 + lr + lcc];
red[rc * h + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * h + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += lf;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
for (int x = 1, ccc = 1; x < nWidth - 1; x++, ccc++)
p = (byte*)(void*)Scan0;
pSrc = (byte*)(void*)SrcScan0;
if (x > size - (h + 1))
p += (x - size + (h + 1)) * 4;
pSrc += (x - size + (h + 1)) * 4;
ccc = (bmp.Width - x >= h + 1) ? Math.Min(ccc, h) : bmp.Width - x - 1;
int fc = (h + ccc + 1);
red = new byte[fc * (h + l + 1)];
green = new byte[fc * (h + l + 1)];
blue = new byte[fc * (h + l + 1)];
alpha = new byte[fc * (h + l + 1)];
mid = red.Length / 2;
for (int r = h - l, rc = 0; r < size; r++, rc++)
int lr = rc * stride;
for (int c = Math.Max(h - x, 0), cc = 0; (x - h + size <= bmp.Width) ? c < size : c < (size - (x - h + size - bmp.Width)); c++, cc++)
int lcc = cc * 4;
blue[rc * fc + cc] = pSrc[lr + lcc];
green[rc * fc + cc] = pSrc[1 + lr + lcc];
red[rc * fc + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * fc + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p = (byte*)(void*)Scan0;
p += lf + (x * 4);
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
p = (byte*)(void*)Scan0;
p += (nWidth - h - 1) * 4;
pSrc = (byte*)(void*)SrcScan0;
pSrc += (nWidth - h - 1) * 4;
red = new byte[(h + 1) * (h + l + 1)];
green = new byte[(h + 1) * (h + l + 1)];
blue = new byte[(h + 1) * (h + l + 1)];
alpha = new byte[(h + 1) * (h + l + 1)];
mid = red.Length / 2;
for (int r = h - l, rc = 0; r < size; r++, rc++)
int lr = rc * stride;
for (int c = 0, cc = 0; c < size - h; c++, cc++)
int lcc = cc * 4;
blue[rc * h + cc] = pSrc[lr + lcc];
green[rc * h + cc] = pSrc[1 + lr + lcc];
red[rc * h + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * h + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += lf + lh;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
# endregion
# region Main Body
//for (int y = 0; y < nHeight - size + 1; y++)
Parallel.For(0, nHeight - size + 1, (y, loopState) =>
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
if (bgw != null && bgw.CancellationPending)
loopState.Break();
# region First Pixels
byte[] red = new byte[(h + 1) * size];
byte[] green = new byte[(h + 1) * size];
byte[] blue = new byte[(h + 1) * size];
byte[] alpha = new byte[(h + 1) * size];
int mid = red.Length / 2;
for (int l = 0; l < h; l++)
p = (byte*)(void*)Scan0;
p += y * stride;
pSrc = (byte*)(void*)SrcScan0;
pSrc += y * stride;
int ll = l * 4;
for (int r = 0, rc = 0; r < size; r++, rc++)
int lr = rc * stride;
for (int c = 0, cc = 0; c < (h + 1); c++, cc++)
int lcc = cc * 4;
blue[r * (h + 1) + cc] = pSrc[lr + lcc];
green[r * (h + 1) + cc] = pSrc[1 + lr + lcc];
red[r * (h + 1) + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[r * (h + 1) + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += llh + ll;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
# endregion
# region Standard
red = new byte[size * size];
green = new byte[size * size];
blue = new byte[size * size];
alpha = new byte[size * size];
mid = red.Length / 2;
for (int x = 0; x < nWidth - size + 1; x++)
p = (byte*)(void*)Scan0;
p += y * stride + x * 4;
pSrc = (byte*)(void*)SrcScan0;
pSrc += y * stride + x * 4;
for (int r = 0; r < size; r++)
int llr = r * stride;
for (int c = 0; c < size; c++)
int lc = c * 4;
blue[r * size + c] = pSrc[llr + lc];
green[r * size + c] = pSrc[1 + llr + lc];
red[r * size + c] = pSrc[2 + llr + lc];
if (doTrans)
alpha[r * size + c] = pSrc[3 + llr + lc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += llh + lh;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
# endregion
# region Last Pixels
red = new byte[(h + 1) * size];
green = new byte[(h + 1) * size];
blue = new byte[(h + 1) * size];
alpha = new byte[(h + 1) * size];
mid = red.Length / 2;
for (int l = nWidth - size + 1; l < nWidth - h; l++)
int ll = l * 4;
p = (byte*)(void*)Scan0;
p += (y * stride) + ll;
pSrc = (byte*)(void*)SrcScan0;
pSrc += (y * stride) + ll;
for (int r = 0, rc = 0; r < size; r++, rc++)
int lr = rc * stride;
for (int c = 0, cc = 0; c < h + 1; c++, cc++)
int lcc = cc * 4;
blue[r * (h + 1) + cc] = pSrc[lr + lcc];
green[r * (h + 1) + cc] = pSrc[1 + lr + lcc];
red[r * (h + 1) + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[r * (h + 1) + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += llh + lh;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
# endregion
# endregion
# region Last Part
for (int l = 0; l < h; l++)
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
p += (nHeight - size + l + 1) * stride;
pSrc += (nHeight - size + l + 1) * stride;
int lf = l * stride;
int ll = l * 4;
byte[] red = new byte[(h + 1) * (size - l - 1)];
byte[] green = new byte[(h + 1) * (size - l - 1)];
byte[] blue = new byte[(h + 1) * (size - l - 1)];
byte[] alpha = new byte[(h + 1) * (size - l - 1)];
int mid = red.Length / 2;
for (int r = 0, rc = 0; r < size - (l + 1); r++, rc++)
int lr = rc * stride;
for (int c = h, cc = 0; c < size; c++, cc++)
int lcc = cc * 4;
blue[rc * h + cc] = pSrc[lr + lcc];
green[rc * h + cc] = pSrc[1 + lr + lcc];
red[rc * h + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * h + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += llh;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
for (int x = 1, ccc = 1; x < nWidth - 1; x++, ccc++)
p = (byte*)(void*)Scan0;
p += (nHeight - size + l + 1) * stride;
pSrc = (byte*)(void*)SrcScan0;
pSrc += (nHeight - size + l + 1) * stride;
if (x > size - (h + 1))
p += (x - size + (h + 1)) * 4;
pSrc += (x - size + (h + 1)) * 4;
ccc = (bmp.Width - x >= h + 1) ? Math.Min(ccc, h) : bmp.Width - x - 1;
int fc = (h + ccc + 1);
red = new byte[fc * (size - l - 1)];
green = new byte[fc * (size - l - 1)];
blue = new byte[fc * (size - l - 1)];
alpha = new byte[fc * (size - l - 1)];
mid = red.Length / 2;
for (int r = 0, rc = 0; r < size - (l + 1); r++, rc++)
int lr = rc * stride;
for (int c = Math.Max(h - x, 0), cc = 0; (x - h + size <= bmp.Width) ? c < size : c < (size - (x - h + size - bmp.Width)); c++, cc++)
int lcc = cc * 4;
blue[rc * fc + cc] = pSrc[lr + lcc];
green[rc * fc + cc] = pSrc[1 + lr + lcc];
red[rc * fc + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * fc + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p = (byte*)(void*)Scan0;
p += (nHeight - size + l + 1) * stride;
p += llh + (x * 4);
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
p = (byte*)(void*)Scan0;
p += (nHeight - size + l + 1) * stride;
p += (nWidth - h - 1) * 4;
pSrc = (byte*)(void*)SrcScan0;
pSrc += (nHeight - size + l + 1) * stride;
pSrc += (nWidth - h - 1) * 4;
red = new byte[(h + 1) * (size - l - 1)];
green = new byte[(h + 1) * (size - l - 1)];
blue = new byte[(h + 1) * (size - l - 1)];
alpha = new byte[(h + 1) * (size - l - 1)];
mid = red.Length / 2;
for (int r = 0, rc = 0; r < size - (l + 1); r++, rc++)
int lr = rc * stride;
for (int c = 0, cc = 0; c < size - h; c++, cc++)
int lcc = cc * 4;
blue[rc * h + cc] = pSrc[lr + lcc];
green[rc * h + cc] = pSrc[1 + lr + lcc];
red[rc * h + cc] = pSrc[2 + lr + lcc];
if (doTrans)
alpha[rc * h + cc] = pSrc[3 + lr + lcc];
Array.Sort(red);
Array.Sort(green);
Array.Sort(blue);
Array.Sort(alpha);
p += llh + lh;
p[0] = blue[mid];
p[1] = green[mid];
p[2] = red[mid];
if (doTrans)
p[3] = alpha[mid];
# endregion
bmp.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);
bSrc.Dispose();
return true;
catch
try
bmp.UnlockBits(bmData);
catch
try
bSrc.UnlockBits(bmSrc);
catch
if (bSrc != null)
bSrc.Dispose();
bSrc = null;
return false;
Regards,
Thorsten -
Using .getPixel() to Read Through a MovieClip
Hello Smart People!
I have two images, one overlayed exactly over the other. The image in front is what the user needs to see, but the user behind can be considered to be like a map with different fields of color on it representing different things.
What I am trying to do is that when the user mouses over the image, it reads (via .getPixel) the color info of the image behind which prompts certain messages to be displayed.
I am not having a problem with getting .getPixel() to work since when I just have the behind image (colored "map") showing, the color data for each pixel comes up as expected. But, when I overlay the top image, it shields the back image from being able to be "read" by my .getPixel function.
Is there a way to have a movieClip not "interfere" with movieClips that lay behind it in the stack?
Any help would be appreciated.
-johngetPixel handles BitmapData, it has nothing to do with MovieClips. All you need to do is save this color map clip as a BitmapData object at some point, and then use getPixel to access this data. I am not even sure how you could be calling this method with a MovieClip.
-
Why doesn't javac find getPixels
I took out the other code to simplify, it's importing about every package getPixels might be I think:
import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.awt.image.DataBuffer.*;
import java.awt.Image;
import java.awt.image.*;
import java.awt.image.SampleModel.*;
import java.awt.image.ComponentSampleModel;
import java.awt.image.PixelInterleavedSampleModel;
import java.awt.image.Raster.*;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class M2 extends Applet
private Graphics g;
private DataBufferInt dbuffer=new DataBufferInt(3,2700);//TYPE_INT=3
private int[]pieces=new int[3000];
public void init(){setBackground(Color.cyan);}
public void start()
g=getGraphics();
getPixels(0,0,60,45,pieces,dbuffer);
public void paint(Graphics g)
//g.setColor(Color.yellow);
//g.fillRect(0,0,30,20);
}Find a class that does have getPixels. It will almost definietly be an instance method. Look up the Java Tutorials (to the left) and start reading from page one, and eventually move through the Graphics section. At that point search the API for Image and classes related to it...
Maybe you are looking for
-
What is the right bios for Satellite A100-749?
Hi I bought a A100-749 (PSAARE) in France and I would like to update a firmware. I found a firmware version 5.90 for model PSAAR (there is no PSAARE so i think this model match) and inside the archive there are two files: s10pm590.rom s10pm590.rom Wh
-
Can I Backup My Time Machine Backups?
I am currently using Time Machine to archive my iMac to a 1TB USB drive. The Backups.backupdb folder is just part of the disk contents but currently comes in at over 500GB and contains archived data back to May 2009. Because I was in need of a little
-
Airport Express: speakers go silent
I have a very simple setup: an Airport Express is connected to an old-fashioned stereo box, which leads to a pair of non-digital speakers. I play music from iTunes on a MacBook Pro. My problem is that, every once in a while -- usually after a few h
-
Hello, I have a question about parsing HTML files. Usually when I get an HTML file and I need to find all the text in it I do this. This stuff just collects all of the hyperlinks and ignores all the html tags just keeping the actual text. It's fine f
-
Best practice for identifying BPEL process boundary
Hi Want to know if there is any best practices / guidelines available to identify how many BPEL processes are required.. I assume once we do a process decomposition we might arrive at an abstract business process boundary. However, I also think those