Invert bitmap colors

前端 未结 4 1389
悲&欢浪女
悲&欢浪女 2021-02-06 11:10

I have the following problem. I have a charting program, and it\'s design is black, but the charts (that I get from the server as images) are light (it actually uses only 5 colo

4条回答
  •  Happy的楠姐
    2021-02-06 11:50

    OK seen that you're really only using 5 colors it's quite easy.

    Regarding performances, I don't know about Android but I can tell you that in Java using setRGB is amazingly slower than getting back the data buffer and writing directly in the int[].

    When I write "amazingly slower", to give you an idea, on OS X 10.4 the following code:

    for ( int x = 0; x < width; x++ ) {
        for ( int y = 0; y < height; y++ ) {
            img.setRGB(x,y,0xFFFFFFFF);
        }
    }
    

    can be 100 times (!) slower than:

    for ( int x = 0; x < width; x++ ) {
        for ( int y = 0; y < height; y++ ) {
            array[y*width+x] = 0xFFFFFFFF;
        }
    }
    

    You read correctly: one hundred time. Measured on a Core 2 Duo / Mac Mini / OS X 10.4.

    (of course you need to first get access to the underlying int[] array but hopefully this shouldn't be difficult)

    I cannot stress enough that the problem ain't the two for loops: in both cases it's the same unoptimized for loops. So it's really setRGB that is the issue here.

    I don't know it works on Android, but you probably should get rid of setRGB if you want something that performs well.

提交回复
热议问题