2

(I've made three posts in the last week on SO regarding a Java project I'm working on. I feel guilty, but what the hell, your answers are amazing.)

Here's a chunk of code in C#:

Bitmap bitmap = ...
    
int dstStride = bitmap.Stride;
        
byte* bdst = (byte*)bitmap.Scan0;

I want to make an equivalent algorithm in Java. I'm beginning to think this is impossible, based on other, similar questions.

I can actually replicate the stride info of my bitmap, but of course, that byte* is nigh impossible to reproduce. What happens later is that there's a for loop that manipulates the bitmap image, a la:

bdst[x * 3 + y * dstStride + 2] = (byte)(alpha * bsrc[dx * 3 + L * srcStride + 2]);

(x & y are iterators in a loop)

Naturally I'm unable to simply make bdst a byte array because that doesn't make sense. According to this totally awesome article, Scan0 is "[t]he address in memory of the fixed data array."

And judging by the above SO post, this is not possible in Java. Confirm / deny?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • Not sure why can't you treat bdst as a byte array? That's exactly what it is. C# Bitmap class is a wrapper over native bitmap object that obviously represents internal image data as pointer to first memory location. – VinayC Nov 10 '10 at 05:39

1 Answers1

0

You cannot get a direct pointer to memory in Java. This is obviously by design.

However you can get an array of pixels from an Image using the PixelGrabber class. Or, if you have a BufferedImage, you can work with the Raster directly. This may help you achieve your goal without accessing memory directly.

Grodriguez
  • 21,501
  • 10
  • 63
  • 107