Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Traitement d'image simple et rapide avec Rende...

Traitement d'image simple et rapide avec Renderscript

Quentin Menini

April 24, 2018
Tweet

Other Decks in Programming

Transcript

  1. Traitement d’image : pourquoi c’est chiant ? 4032 3024 .

    12 192 768 Pixels . for (i in 0 .. 12192768)
  2. Traitement d’image : pourquoi c’est chiant ? ➔ 46,5 Mo

    46,5 Mo 12 192 768 Pixels ➔ 46,5 Mo
  3. Traitement d’image : pourquoi c’est chiant ? ➔ Out of

    Memory 46,5 Mo 46,5 Mo 12 192 768 Pixels ➔ 46,5 Mo
  4. ➔ 46,5 Mo 46,5 Mo Traitement d’image : pourquoi c’est

    chiant ? Out of Memory 12 192 768 Pixels ➔ 46,5 Mo
  5. RenderScript Fonctionne sur GPU, CPU et/ou DSP « Write once,

    run anywhere » Parallélisation gérée par RenderScript
 

  6. RenderScript Fonctionne sur GPU, CPU et/ou DSP « Write once,

    run anywhere » Parallélisation gérée par RenderScript
 
 for (int i = 0; i < 12192768; i++) { out[i] = doSomething(in[i]) }
  7. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  8. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  9. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  10. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  11. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  12. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  13. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  14. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  15. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  16. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  17. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  18. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  19. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  20. Blur.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setRadius(radius); script.setInput(in); script.forEach(out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  21. Les plus simples - ScriptIntrinsicBlur - ScriptIntrinsicResize - ScriptIntrinsicColorMatrix -

    ScriptIntrinsicBlend Les moins simples - ScriptIntrinsicConvolve3x3 - ScriptIntrinsicConvolve5x5 - ScriptIntrinsicYuvToRGB - ScriptIntrinsicLUT - ScriptIntrinsic3DLUT ScriptIntrinsics
  22. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  23. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  24. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  25. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  26. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  27. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  28. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); } static float computeDistance(uint32_t x, uint32_t y) {...}
  29. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); } static float4 computeColor(...) {...}
  30. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  31. vignette.rs int width; int height; uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t

    x, uint32_t y) { float4 f4 = rsUnpackColor8888(in); float red = f4.r; float green = f4.g; float blue = f4.b; float distance = computeDistance(x, y); float4 newColor = computeColor(red, green, blue, distance); return rsPackColorTo8888(newColor.r, newColor.g, newColor.b, f4.a); }
  32. Mask.java RenderScript rs = RenderScript.create(context); Allocation in = Allocation.createFromBitmap(rs, bitmap);

    Type t = in.getType(); Allocation out = Allocation.createTyped(rs, t); ScriptC_vignette script = ScriptC_vignette(rs); script.setWidth(bitmap.width); script.setHeight(bitmap.height); script.forEach_root(in, out); out.copyTo(bitmap); in.destroy(); out.destroy(); script.destroy(); rs.destroy(); return bitmap;
  33. Générations à partir du fichier .rs script.rs ScriptC_script.java Typedef struct

    Struct ScriptField_Struct.java Struct_t* my_struct; .bind_my_struct(...); RenderScript ➔ Java
  34. Générations à partir du fichier .rs script.rs ScriptC_script.java Typedef struct

    Struct ScriptField_Struct.java Struct_t* my_struct; .bind_my_struct(...); int value; .set_value(int i); .get_value(); RenderScript ➔ Java
  35. Générations à partir du fichier .rs script.rs ScriptC_script.java Typedef struct

    Struct ScriptField_Struct.java Struct_t* my_struct; .bind_my_struct(...); int value; .set_value(int i); .get_value(); void function(); .invoke_function(); RenderScript ➔ Java
  36. Générations à partir du fichier .rs script.rs ScriptC_script.java Typedef struct

    Struct ScriptField_Struct.java Struct_t* my_struct; .bind_my_struct(...); int value; .set_value(int i); .get_value(); void function(); .invoke_function(); uchar4 __attribute__((kernel)) root(uchar4 in, uint32_t x, uint32_t y) .forEach_root(Allocation in, Allocation out); RenderScript ➔ Java