2012-11-26 189 views
4

在Photoshop有幾個混合模式,如:PNG混合模式

  • 顏色加深
  • 乘法
  • 變暗
  • 變淺
  • 疊加

(更多的信息在這裏:http://help.adobe.com/en_US/photoshop/cs/using/WSfd1234e1c4b69f30ea53e41001031ab64-77eba.html

例如,對於模式顏色加深「着眼於每個通道中的顏色信息,並基色變暗通過增大兩者之間的對比,以反映混合色。與白色混合後不產生變化」

這是我的代碼:

Bitmap blendBitmap = BitmapFactory.decodeStream(ctx.getAssets().open(filename)); 
Canvas canvas = new Canvas(srcBitmap); 
canvas.drawBitmap(blendBitmap, 0, 0, null); // ? 
p.recycle(); 
p = null; 

是否有可能適用,例如,顏色加深混合模式,而不是簡單地在這個小畫上面的其他圖像的圖像(如?。代碼)

+0

你的意思是你想要寫您的圖像濾鏡效果? – Raptor

+0

它不完全是過濾效果,它是兩層混合在一起的某種方式。 – alwx

回答

3

也不難

我使用NDK的(因爲性能)與像素處理混合模式的這個信息是非常有用的:How does photoshop blend two images together?

我最終的解決方案是:

#define ChannelBlend_ColorBurn(A, B) ((uint8_t) ((B == 0) ? B : max(0, (255 - ((255 - 
#define ChannelBlend_Alpha(A, B, O) ((uint8_t) (O * A + (1 - O) * B)) 
#define ChannelBlend_AlphaF(A, B, F, O) (ChannelBlend_Alpha(F(A, B), A, O)) 

typedef struct { 
    uint8_t red; 
    uint8_t green; 
    uint8_t blue; 
    uint8_t alpha; 
} rgba; 

// Blend 
JNIEXPORT void 
JNICALL Java_com_package_Filter_jniBlend(JNIEnv* env, jobject obj, jobject bitmapA, 
    jobject bitmapB, jobject bitmapOut, jint mode) { 

    // Properties 
    AndroidBitmapInfo infoA; 
    void*    pixelsA; 
    AndroidBitmapInfo infoB; 
    void*    pixelsB; 
    AndroidBitmapInfo infoOut; 
    void*    pixelsOut; 

    int ret; 

    // Get image info 
    if ((ret = AndroidBitmap_getInfo(env, bitmapA, &infoA)) < 0 || 
     (ret = AndroidBitmap_getInfo(env, bitmapB, &infoB)) < 0 || 
     (ret = AndroidBitmap_getInfo(env, bitmapOut, &infoOut)) < 0) { 
     return; 
    } 
    // Check image 
    if (infoA.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || 
     infoB.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || 
     infoOut.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { 
     return; 
    } 
    // Lock all images 
    if ((ret = AndroidBitmap_lockPixels(env, bitmapA, &pixelsA)) < 0 || 
     (ret = AndroidBitmap_lockPixels(env, bitmapB, &pixelsB)) < 0 || 
     (ret = AndroidBitmap_lockPixels(env, bitmapOut, &pixelsOut)) < 0) { 
     LOGE("Error! %d", ret); 
    } 

    int h = infoA.height; 
    int w = infoA.width; 
    int wh = w * h; 

    int n; 
    rgba* inputA = (rgba*) pixelsA; 
    rgba* inputB = (rgba*) pixelsB; 
    rgba* output = (rgba*) pixelsOut; 

    rgba pA, pB; 

    int x, y; 
    for (y = 0; y < h; y++) { 
     for (x = 0; x < w; x++) { 
      n = y * w + x; 

      pA = inputA[n]; 
      pB = inputB[n]; 

      float alpha = (float) pB.alpha/255.0; 

      output[n].red = ChannelBlend_AlphaF(pA.red, pB.red, ChannelBlend_ColorBurn, alpha); 
      output[n].green = ChannelBlend_AlphaF(pA.green, pB.green, ChannelBlend_ColorBurn, alpha); 
      output[n].blue = ChannelBlend_AlphaF(pA.blue, pB.blue, ChannelBlend_ColorBurn, alpha); 
     } 
    } 

    // Unlocks everything 
    AndroidBitmap_unlockPixels(env, bitmapA); 
    AndroidBitmap_unlockPixels(env, bitmapB); 
    AndroidBitmap_unlockPixels(env, bitmapOut); 
} 

小的話,以提高性能:我做了單個像素的公式後,它會是很好的保存在某種緩存值更快地訪問它沒有公式下一次。

+1

它的工作!當然,還有一些類似於ChannelBlend_ColorBurn聲明的拼寫錯誤,但仍然非常感謝你!你幾乎救了我的應用程序!我終於可以溝ImageMagick了,謝謝! –