[solved] Read in and analyze Image Files?

Hey all,

is there a way to read in an image file and to analyze it? I only need to read in grey-scale images, doesn't matter if png, bmp or jpg ..

I want to make it possible for Game Designers to create such images to use them in my Plugin, like in old RMXP they could draw Transition Fades. So I want to do something like this:

Code:
var image = readInImage('greyscale.png')

for (var x = 0; x < image.width; x++) {
    for (var y = 0; y < image.height; y++) {
        var value = image[x][y]
        // ...
    }
}
Are you saying like read the pixel data?
If so, you want a bitmap.
JavaScript:
let bmp = Bitmap.load('img/pictures/myPicture.png')

bmp.getAlphaPixel(x, y) //Will return pixel alpha, 0 for no data or transparent, up to 255.
bmp.getPixel(x, y) //will return the hex color code for this pixel

After that yeah, you'd pretty much want a nested loop to check the x and y of each pixel. It can be a lot, so good coding is a must. I've done quite a bit with this, use good technique and keep your fps window open while testing
Are you saying like read the pixel data?
If so, you want a bitmap.
JavaScript:
let bmp = Bitmap.load('img/pictures/myPicture.png')

bmp.getAlphaPixel(x, y) //Will read pixel alpha, 0 for no data or transparent, up to 255.
bmp.getPixel(x, y) //will return the hex color code for this pixel
Perfect, exactly what I was looking for! Thank you!
No problem, and I don't want to sound condescending by what I'm about to say. It's honestly bc I've done enough with this to know about the headaches.
We all forget to do stuff when coding, even if we know it already. But please make sure you do the following....
-If there is ANY way - for whatever you're trying to do with this - for you to specify when you got what you came for in that nested loop - return or break. These loops are executing the bmp pixel function each iteration. You absolutely don't want to do it any longer than you have to or performance is gonna take a big hit on weaker systems especially.
-Declare your loop iterators (in your case x and y) before the loop, and initialize them with a value of 0 (or any regular integer). More than anything so you don't recreate the nested y variable on each x iteration.
- If it's possible to store the data you're getting out of this sometime before you need it, like during premap loading, do it. I used this method to make perfect outlines of the player sprite during map execution, and fps would take an 8 frame hit on my system. I ran the same function 4 times, once for each direction, during the preMap loading phase, and you never knew the difference. They were already available when I needed them, later.
@standardplayer makes a great point here!

Also, I like to contribute ^^

You can do this in your loop code, to improve performance.
Instead of this:
JavaScript:
for (var x = 0; x < image.width; x++) {
    for (var y = 0; y < image.height; y++) {
        var value = image[x][y]
        // ...
    }
}
You can use this:
JavaScript:
for (let x = 0, w = image.width; x < w; x++) {
    for (let y = 0, h = image.height; y < h; y++) {
        var value = image[x][y]
        // ...
    }
}
If you don't need to access the values of X and Y out of the scope of the loop, use let instead of var. Because let will only "live" inside the loop. Otherwise, you can keep the var.

Another thing is to store the value for the width and height of your image, before the loop condition( x < image.width). Otherwise, in all your loops condition you will be calculating image.width or image.height. With that, you will only calculate the width and height once.

Original thread: https://forums.rpgmakerweb.com/threads/solved-read-in-and-analyze-image-files.122550/