2013-03-16 2 views
2

다음과 같은 문제가 있습니다. 나는 비트 맵 변환 루틴으로 시작했는데 어떤 종류의 변환이라도 완벽하게 처리 할 수있는 을 완벽하게 처리했습니다.메모리 내 비트 맵 변환

Bitmap transform(Bitmap src) { 
    // ... any kind of transformation , for example GAMMA 
    double gama = 0.8; 
    int[] tR = new int[256]; 
    int[] gG = new int[256]; 
    int[] tB = new int[256]; 
    for(int i = 0; i < 256; ++i) { 
     tR[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5)); 
     tG[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5)); 
     tB[i] = (int)Math.min(255, (int)((255.0 * Math.pow(i/255.0, 1.0/gama)) + 0.5)); 
    } 
    // apply transformation to the old bitmap -> bmOut 
    int wid = src.getWidth(), hei = src.getHeight(); 
    Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig()); 
    int A, R, G, B; 
    for(int x = 0; x < wid; x++) { 
     for(int y = 0; y < hei; y++) { 
     int pixel = src.getPixel(x, y); 
     A = Color.alpha(pixel); 
     R = tR[Color.red(pixel)]; 
     G = tG[Color.green(pixel)]; 
     B = tB[Color.blue(pixel)]; 
     bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
     } 
    } 
    return bmOut; 
    } 

그러나 그것은 고통스럽게 느린 경우 - getPixel와() /와 setPixel() 형제, 자매에 의해 발생. 아무런 문제가 없다고 말하면서, 저는 이전의 StretchBlt() 일과 같이 메모리 버퍼를 사용할 것입니다. 그래서, 변환 없음이없는 경우에도 올바른 결과를 얻을, 소프트웨어 공학의 다음 보석 :

Bitmap transform(Bitmap src) { 
    // ... transformation array are built here 

    // apply transformation 
    int wid = src.getWidth(), hei = src.getHeight(); 
    Bitmap bmOut = Bitmap.createBitmap(wid, hei, src.getConfig()); 

    int[] pixs = new int[wid*hei];     // changed 
    src.getPixels(pixs, 0, wid, 0, 0, wid, hei); // changed 

    int A, R, G, B; 
    for(int x = 0; x < wid; x++) { 
     for(int y = 0; y < hei; y++) { 
     int off = (x * y) + y;     // changed 
     int pixel = pixs[off];      // changed 
     A = Color.alpha(pixel); 
     R = tR[Color.red(pixel)]; 
     G = tG[Color.green(pixel)]; 
     B = tB[Color.blue(pixel)]; 
     pixs[off] = Color.argb(A, R, G, B);   // changed  
     } 
    } 
    bmOut.setPixels(pixs, 0, wid, 0, 0, wid, hei); // changed 
    return bmOut; 
    } 

실행합니다 빨리 만드는 주요 재 작성했다. 그러나 픽셀을 마사지하려고하면 (픽셀을 변환하려고 할 때) 이 떨어져 버립니다. 그래서 getPixel()에서 ARGB 픽셀을 비교하고 getPixels (...)에서 픽셀 값의 배열을 비교했습니다. 그리고 그것들은 서로 다릅니다 (처음 2 개는 똑같습니다.

array  getPixel 
a r g b a r g b 
------------------ 
ff65340b ff65340b 
ff64330a ff64330a 
ff66320b ff63320a 
ff65310a ff613008 
ff66300c ff62300d 
ff67310d ff62300d 
ff68300d ff622d0d 
ff69310e ff5f2a0a 
.... 

아무도 내가 이번에 잘못하고있는 것을 알고 있습니까? 나는 아직 mem-array 솔루션 인 의 속도를 포기할 용의가 없다. 감사합니다, 숀

답변

1

그것은 그런데

int off = (y * wid) + x; 

를해야한다, 나는 두 개의 루프가 필요하다고 생각, 당신은 간단하게 수행 할 수 있습니다

for (int off = pixs.length - 1; off >= 0; off--) 
+0

감사합니다 잔뜩. 20 시간의 코딩과 10 가지의 커피를 깨우쳐서 얼마나 깨달을 수 있는지 얼마나 놀랍습니까? 그리고 나는 결코 세상을 보여 주려고 망설이지 않습니다. 어쨌든 코드를 하나의 루프로 줄이면 속도가 빨라졌습니다. 그것은 실제로 잘 작동합니다. sean – seanpj

+0

당신은 천만에요. 션. 코딩 즐기기 :) –