Re: Drawing a single pixel
Re: Drawing a single pixel
- Subject: Re: Drawing a single pixel
- From: "Dennis C. De Mars" <email@hidden>
- Date: Mon, 28 May 2001 19:35:37 -0700
on 5/28/01 1:06 PM, Scott at email@hidden wrote:
>
on Sat, 26 May 2001 21:05:23 -0700, Dennis C. De Mars at
>
email@hidden wrote:
>
>
> I could post the source code if anybody's interested as a simple
>
> example of how to generate an arbitrary bitmap image and display it
>
> using NSBitmapImageRep.
>
>
>
> - Dennis D.
>
>
Yes, please post the code! Seems like a lot of overhead just to draw a
>
pixel, though.
Rob Rix was nice enough to help post the code on the CocoaDev site. Please
see:
http://www.cocoadev.com/index.pl?MakingFractalsUsingBitmapImageRep
As for the overhead to draw a pixel, that's probably why you don't want to
redraw your NSBitmapImageRep every time you change a pixel. In the example,
the entire image is written to the bitmap memory buffer before being drawn
to a window.
>
I have a neural network I'm working on and will need to plot error points as
>
a network learns. Obviously, I'll want any screen draws to be as quick as
>
possible, but I'd also like them real-time. IOW, I don't want to have to
>
wait until the whole map is drawn (off screen) to look at it.
>
>
Any more hints?
Yes. The example draws the entire image once after all the pixels have been
written to the bitmap, but this is not practical in a situation where you
want to be constantly updating the image in real time. In my own
application, a fractal program, a more general program would have the same
problem, since a fractal image usually takes a while to generate (minutes,
hours, occasionally days). My example is atypical because I drew a top-level
Mandelbrot set plot which takes only a split second to calculate.
Although I haven't written a program to handle the more general case where
you would want to draw partial updates to the window (that is, I haven't
written it in Cocoa yet), it should be simple enough. The NSBitmapImageRep
object will belong to an NSImage object, which will belong to some NSView
object that uses it for drawing (I just use an NSImageView in my example,
but you could use a custom NSView). You can simply call the
"-setNeedsDisplay" on the NSView whenever you need to update it.
So, you would simply write into the bitmap buffer (just direct assignments
to memory) and then periodcally command the NSView to redraw itself. You
could either do this on the basis of how much new image data is available (I
might do it for every new row of pixels, for example). Or, you could do it
based on a timer.
In your case, I don't know if you want to literally plot pixels or if you
would actually be drawing small rectangles for your "error points." If the
latter, you might want to draw rectangles using NSBezierPath instead of
using a bitmap.
- Dennis D.