Ejemplo n.º 1
0
  /**
   * Assert that two bitmaps are equal.
   *
   * @param Bitmap b1 the first bitmap which needs to compare.
   * @param Bitmap b2 the second bitmap which needs to compare.
   */
  public static void assertEquals(Bitmap b1, Bitmap b2) {
    if (b1 == b2) {
      return;
    }

    if (b1 == null || b2 == null) {
      Assert.fail("the bitmaps are not equal");
    }

    // b1 and b2 are all not null.
    if (b1.getWidth() != b2.getWidth()
        || b1.getHeight() != b2.getHeight()
        || b1.getConfig() != b2.getConfig()) {
      Assert.fail("the bitmaps are not equal");
    }

    int w = b1.getWidth();
    int h = b1.getHeight();
    int s = w * h;
    int[] pixels1 = new int[s];
    int[] pixels2 = new int[s];

    b1.getPixels(pixels1, 0, w, 0, 0, w, h);
    b2.getPixels(pixels2, 0, w, 0, 0, w, h);

    for (int i = 0; i < s; i++) {
      if (pixels1[i] != pixels2[i]) {
        Assert.fail("the bitmaps are not equal");
      }
    }
  }
Ejemplo n.º 2
0
  private static Bitmap getWhiteSheep(File inputDir) {
    File alreadyThere = new File(inputDir, "mob/sheep_0.png");
    if (alreadyThere.exists()) {
      return BitmapFactory.decodeFile(alreadyThere.getAbsolutePath());
    }
    File topHalf = new File(inputDir, "mob/sheep.png");
    if (!topHalf.exists()) {
      topHalf = new File(inputDir, "entity/sheep/sheep.png");
    }

    // We have to build this one by hand
    File bottomHalf = new File(inputDir, "mob/sheep_fur.png");
    if (!bottomHalf.exists()) {
      bottomHalf = new File(inputDir, "entity/sheep/sheep_fur.png");
    }
    if (!topHalf.exists() || !bottomHalf.exists()) {
      return null;
    }
    Bitmap topHalfBitmap = BitmapFactory.decodeFile(topHalf.getAbsolutePath());
    Bitmap bottomHalfBitmap = BitmapFactory.decodeFile(bottomHalf.getAbsolutePath());
    Bitmap finalBitmap =
        Bitmap.createBitmap(
            topHalfBitmap.getWidth(), topHalfBitmap.getHeight() * 2, Bitmap.Config.ARGB_8888);
    int[] pixels = new int[topHalfBitmap.getWidth() * topHalfBitmap.getHeight()];
    topHalfBitmap.getPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    finalBitmap.setPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    bottomHalfBitmap.getPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        0,
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    finalBitmap.setPixels(
        pixels,
        0,
        topHalfBitmap.getWidth(),
        0,
        topHalfBitmap.getHeight(),
        topHalfBitmap.getWidth(),
        topHalfBitmap.getHeight());
    return finalBitmap;
  }
Ejemplo n.º 3
0
  /*Obtenemos la projección inversa a partir del bitmap*/
  public static synchronized int[] setBckHistogram(Bitmap image) {
    int width = image.getWidth(), height = image.getHeight(); // Obtenemos la dimensiones
    int pos = 0,
        gray = 0,
        imPix = 0; // Constantes de posición y de la escala de grises y almacenador del pixel.

    int[] imPixels = new int[width * height]; // Generamos un buffer de la imagen;
    image.getPixels(
        imPixels, 0, width, 0, 0, width,
        height); // Obtenemos la imagen en el buffer a partir del bitmap

    // Recorremos la imagen de arriba a abajo y de izquierda a derecha.
    for (int y = 0; y < height; y++)
      for (int x = 0; x < width; x++) {
        pos = x + (y * width); // Obtenemos la posición del pixel en el array.
        imPix = imPixels[pos]; // Almacenamos el valor del pixel n.

        gray =
            (((imPix >> 16) & 0xFF) + ((imPix >> 8) & 0xFF) + ((imPix >> 0) & 0xFF))
                / 3; // Obtenemos la escala en grises.

        // Binarizamos.
        if (gray < 50) gray = 255;
        else gray = 0;

        imPixels[pos] =
            0xFF000000 | gray << 16 | gray << 8 | gray; // Almacenamos la proyección inversa.
      }

    return imPixels; // Regresamos un vector que contiene los pixeles binarizados.
  }
Ejemplo n.º 4
0
  public static Bitmap applySaturationFilter(Bitmap source, int level) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    float[] HSV = new float[3];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        // get current index in 2D-matrix
        index = y * width + x;
        // convert to HSV
        Color.colorToHSV(pixels[index], HSV);
        // increase Saturation level
        HSV[1] *= level;
        HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
        // take color back
        pixels[index] |= Color.HSVToColor(HSV);
      }
    }
    // output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
  }
  /** Generate a direct RGB {@link ByteBuffer} from a {@link Bitmap}. */
  private static ByteBuffer convertToRGB(Bitmap argbBitmap) {
    // TODO: Optimize this.
    int width = argbBitmap.getWidth();
    int height = argbBitmap.getHeight();
    ByteBuffer buf = ByteBuffer.allocateDirect(BYTES_PER_RGB_PIX * width * height);

    int[] pixelRow = new int[width];
    byte[] finalRow = new byte[BYTES_PER_RGB_PIX * width];
    for (int i = 0; i < height; i++) {
      argbBitmap.getPixels(
          pixelRow, /*offset*/
          0, /*stride*/
          width, /*x*/
          0, /*y*/
          i,
          /*width*/ width, /*height*/
          1);
      for (int j = 0; j < width; j++) {
        colorToRgb(pixelRow[j], j * BYTES_PER_RGB_PIX, /*out*/ finalRow);
      }
      buf.put(finalRow);
    }

    buf.rewind();
    return buf;
  }
Ejemplo n.º 6
0
  /** Factory function to load a texture from the APK. */
  public static Texture loadTextureFromApk(String fileName, AssetManager assets) {
    InputStream inputStream = null;
    try {
      inputStream = assets.open(fileName, AssetManager.ACCESS_BUFFER);

      BufferedInputStream bufferedStream = new BufferedInputStream(inputStream);
      Bitmap bitMap = BitmapFactory.decodeStream(bufferedStream);

      int[] data = new int[bitMap.getWidth() * bitMap.getHeight()];
      bitMap.getPixels(data, 0, bitMap.getWidth(), 0, 0, bitMap.getWidth(), bitMap.getHeight());

      // Convert:
      byte[] dataBytes = new byte[bitMap.getWidth() * bitMap.getHeight() * 4];
      for (int p = 0; p < bitMap.getWidth() * bitMap.getHeight(); ++p) {
        int colour = data[p];
        dataBytes[p * 4] = (byte) (colour >>> 16); // R
        dataBytes[p * 4 + 1] = (byte) (colour >>> 8); // G
        dataBytes[p * 4 + 2] = (byte) colour; // B
        dataBytes[p * 4 + 3] = (byte) (colour >>> 24); // A
      }

      Texture texture = new Texture();
      texture.mWidth = bitMap.getWidth();
      texture.mHeight = bitMap.getHeight();
      texture.mChannels = 4;
      texture.mData = dataBytes;

      return texture;
    } catch (IOException e) {
      DebugLog.LOGE("Failed to log texture '" + fileName + "' from APK");
      DebugLog.LOGI(e.getMessage());
      return null;
    }
  }
  // ==============================================================================
  public final int[] renderGlyph(
      char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) {
    Path p = new Path();
    paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p);

    RectF boundsF = new RectF();
    p.computeBounds(boundsF, true);
    matrix.mapRect(boundsF);

    boundsF.roundOut(bounds);
    bounds.left--;
    bounds.right++;

    final int w = bounds.width();
    final int h = bounds.height();

    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bm);
    matrix.postTranslate(-bounds.left, -bounds.top);
    c.setMatrix(matrix);
    c.drawPath(p, paint);

    final int sizeNeeded = w * h;
    if (cachedRenderArray.length < sizeNeeded) cachedRenderArray = new int[sizeNeeded];

    bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h);
    bm.recycle();
    return cachedRenderArray;
  }
Ejemplo n.º 8
0
        @Override
        public void run() {
          int w = getWidth();
          int h = getHeight();

          float wipeArea = 0;
          float totalArea = w * h;

          Bitmap bitmap = mBitmap;

          int[] mPixels = new int[w * h];
          // 获取bitmap的所有像素信息
          bitmap.getPixels(mPixels, 0, w, 0, 0, w, h);
          for (int i = 0; i < w; i++)
            for (int j = 0; j < h; j++) {
              int index = i + j * w;
              if (mPixels[index] == 0) {
                wipeArea++;
              }
            }
          // 计算已扫区域所占的比例
          if (wipeArea > 0 && totalArea > 0) {
            int percent = (int) (wipeArea * 100 / totalArea);
            Log.v("czm", "percent=" + percent);

            if (percent > 70) {
              // 清除图层区域
              mCompleted = true;
              postInvalidate();
            }
          }
        };
Ejemplo n.º 9
0
  /** Copy the heightmap data into a vertex buffer object. */
  private float[] loadBitmapData(Bitmap bitmap) {
    final int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    bitmap.recycle();

    final float[] heightmapVertices = new float[width * height * POSITION_COMPONENT_COUNT];
    int offset = 0;
    for (int row = 0; row < height; row++) {
      for (int col = 0; col < width; col++) {
        // The heightmap will lie flat on the XZ plane and centered
        // around (0, 0), with the bitmap width mapped to X and the
        // bitmap height mapped to Z, and Y representing the height. We
        // assume the heightmap is grayscale, and use the value of the
        // red color to determine the height.
        final float xPosition = (((float) col / (float) (width - 1)) - 0.5f) * 20;
        final float yPosition =
            ((float) Color.red(pixels[(row * height) + col]) / (float) 255) * 1f;
        final float zPosition = (((float) row / (float) (height - 1)) - 0.5f) * 20;

        heightmapVertices[offset++] = xPosition;
        heightmapVertices[offset++] = yPosition;
        heightmapVertices[offset++] = zPosition;
      }
    }
    return heightmapVertices;
  }
Ejemplo n.º 10
0
  @Override
  public Bitmap filter(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Logger.v(name + ", multiplier: " + multiplier + ", divider: " + divider + ", bias: " + bias);

    if (bitmap == null || divider == 0 || (multiplier == 1 && divider == 1 && bias == 0)) {
      Logger.e("parameter error (bitmap null, divider is zero, or given filter has no effect)");
      return null;
    }

    int[] bitmapBytes = new int[width * height];
    bitmap.getPixels(bitmapBytes, 0, width, 0, 0, width, height);

    int color;
    for (int i = 0; i < bitmapBytes.length; i++) {
      color = bitmapBytes[i];

      bitmapBytes[i] =
          Color.rgb(
              forcepin(0, r(color) * multiplier / divider + bias, 255),
              forcepin(0, g(color) * multiplier / divider + bias, 255),
              forcepin(0, b(color) * multiplier / divider + bias, 255));
    }

    return Bitmap.createBitmap(bitmapBytes, width, height, bitmap.getConfig());
  }
Ejemplo n.º 11
0
  @Override
  public void run(Canvas canvas, Bitmap bitmap) {
    setChanged();
    notifyStatus(NOTIFY_STATES.COMMAND_STARTED);
    if (mClickedPixel == null) {
      setChanged();
      notifyStatus(NOTIFY_STATES.COMMAND_FAILED);
      return;
    }
    int colorToReplace = bitmap.getPixel(mClickedPixel.x, mClickedPixel.y);
    int pixels[] = new int[bitmap.getWidth() * bitmap.getHeight()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    QueueLinearFloodFiller.floodFill(
        pixels,
        bitmap.getWidth(),
        bitmap.getHeight(),
        mClickedPixel,
        colorToReplace,
        mPaint.getColor(),
        SELECTION_THRESHOLD);

    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    setChanged();
    notifyStatus(NOTIFY_STATES.COMMAND_DONE);
  }
Ejemplo n.º 12
0
  @Override
  public void run(Canvas canvas, Bitmap bitmap) {

    notifyStatus(NOTIFY_STATES.COMMAND_STARTED);
    if (mClickedPixel == null) {
      setChanged();
      notifyStatus(NOTIFY_STATES.COMMAND_FAILED);
      return;
    }

    if (PaintroidApplication.savedPictureUri == null
        && PaintroidApplication.commandManager.getNumberOfCommands()
            == EMPTY_COMMAND_LIST_LENGTH + 1) {
      canvas.drawColor(mPaint.getColor());
      Log.w(PaintroidApplication.TAG, "Fill Command color: " + mPaint.getColor());
    } else {
      int colorToReplace = bitmap.getPixel(mClickedPixel.x, mClickedPixel.y);
      int pixels[] = new int[bitmap.getWidth() * bitmap.getHeight()];
      bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

      QueueLinearFloodFiller.floodFill(
          pixels,
          bitmap.getWidth(),
          bitmap.getHeight(),
          mClickedPixel,
          colorToReplace,
          mPaint.getColor(),
          SELECTION_THRESHOLD);

      bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    }

    notifyStatus(NOTIFY_STATES.COMMAND_DONE);
  }
 public RGBLuminanceSource(Bitmap bitmap) {
   super(bitmap.getWidth(), bitmap.getHeight());
   int width = bitmap.getWidth();
   int height = bitmap.getHeight();
   int[] pixels = new int[width * height];
   bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
   // In order to measure pure decoding speed, we convert the entire image
   // to a greyscale array
   // up front, which is the same as the Y channel of the
   // YUVLuminanceSource in the real app.
   luminances = new byte[width * height];
   for (int y = 0; y < height; y++) {
     int offset = y * width;
     for (int x = 0; x < width; x++) {
       int pixel = pixels[offset + x];
       int r = (pixel >> 16) & 0xff;
       int g = (pixel >> 8) & 0xff;
       int b = pixel & 0xff;
       if (r == g && g == b) {
         // Image is already greyscale, so pick any channel.
         luminances[offset + x] = (byte) r;
       } else {
         // Calculate luminance cheaply, favoring green.
         luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
       }
     }
   }
 }
Ejemplo n.º 14
0
  public static Bitmap applySnowEffect(Bitmap source) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);
    // random object
    Random random = new Random();

    int R, G, B, index = 0, thresHold = 50;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        // get current index in 2D-matrix
        index = y * width + x;
        // get color
        R = Color.red(pixels[index]);
        G = Color.green(pixels[index]);
        B = Color.blue(pixels[index]);
        // generate threshold
        thresHold = random.nextInt(COLOR_MAX);
        if (R > thresHold && G > thresHold && B > thresHold) {
          pixels[index] = Color.rgb(255, 255, 255);
        }
      }
    }
    // output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
  }
Ejemplo n.º 15
0
  // 版画效果函数
  public static Bitmap changeToBlock(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    int dst[] = new int[width * height];
    bitmap.getPixels(dst, 0, width, 0, 0, width, height);

    int iPixel = 0;
    int i, j, color, pos;
    for (j = 0; j < height; j++) {
      for (i = 0; i < width; i++) {
        pos = j * width + i;
        color = dst[pos];
        int avg = (Color.red(color) + Color.green(color) + Color.blue(color)) / 3;
        if (avg >= 100) iPixel = 255;
        else iPixel = 0;

        dst[pos] = Color.rgb(iPixel, iPixel, iPixel);
      }
    }

    Bitmap bmpReturn = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bmpReturn.setPixels(dst, 0, width, 0, 0, width, height);
    return bmpReturn;
  }
Ejemplo n.º 16
0
 public RGBLuminanceSource(Bitmap bitmap) {
   super(bitmap.getWidth(), bitmap.getHeight());
   // 得到图片的宽高
   int width = bitmap.getWidth();
   int height = bitmap.getHeight();
   // 得到图片的像素
   int[] pixels = new int[width * height];
   //
   bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
   // 为了测量纯解码速度,我们将整个图像灰度阵列前面,这是一样的通道
   // YUVLuminanceSource在现实应用。
   // 得到像素大小的字节数
   luminances = new byte[width * height];
   // 得到图片每点像素颜色
   for (int y = 0; y < height; y++) {
     int offset = y * width;
     for (int x = 0; x < width; x++) {
       int pixel = pixels[offset + x];
       int r = (pixel >> 16) & 0xff;
       int g = (pixel >> 8) & 0xff;
       int b = pixel & 0xff;
       // 当某一点三种颜色值相同时,相应字节对应空间赋值为其值
       if (r == g && g == b) {
         luminances[offset + x] = (byte) r;
       }
       // 其它情况字节空间对应赋值为:
       else {
         luminances[offset + x] = (byte) ((r + g + g + b) >> 2);
       }
     }
   }
 }
Ejemplo n.º 17
0
  /**
   * Get the white icon corresponding to a poiType.
   *
   * @param poiType the PoiType or null for notes.
   * @return The white icon.
   */
  public Drawable getIconWhite(PoiType poiType) {
    Bitmap myBitmap =
        BitmapFactory.decodeResource(
            context.getResources(),
            poiType == null ? R.drawable.open_book : getIconDrawableId(poiType));
    myBitmap = myBitmap.copy(myBitmap.getConfig(), true);

    int[] allpixels = new int[myBitmap.getHeight() * myBitmap.getWidth()];

    myBitmap.getPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    for (int i = 0; i < myBitmap.getHeight() * myBitmap.getWidth(); i++) {
      if (allpixels[i] != 0) {
        int A = Color.alpha(allpixels[i]);
        // inverting byte for each R/G/B channel
        int R = 255 - Color.red(allpixels[i]);
        int G = 255 - Color.green(allpixels[i]);
        int B = 255 - Color.blue(allpixels[i]);
        // set newly-inverted pixel to output image
        allpixels[i] = Color.argb(A, R, G, B);
      }
    }

    myBitmap.setPixels(
        allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    return new BitmapDrawable(context.getResources(), myBitmap);
  }
Ejemplo n.º 18
0
 public void loadFromCroppedColorArray(int x, int y, int width, int height) {
   int[] pixels = new int[width * height];
   mOriginalBitmap.getPixels(pixels, 0, width, x, y, width, height);
   CloseableReference<Bitmap> bitmapRef =
       mBitmapFactory.createBitmap(pixels, width, height, mOriginalBitmap.getConfig());
   setImageBitmap(bitmapRef);
   checkValid(bitmapRef, Bitmap.createBitmap(pixels, width, height, mOriginalBitmap.getConfig()));
 }
Ejemplo n.º 19
0
  /**
   * Fast Gaussian blurring algorithm source:
   * https://github.com/patrickfav/BlurTestAndroid/blob/master/BlurBenchmark/src/main/java/at/favre/app/blurbenchmark/blur/algorithms/GaussianFastBlur.java
   */
  @NonNull
  public static Bitmap applyFastGaussianBlurToBitmap(@NonNull Bitmap mutableBitmap, int radius) {
    int w = mutableBitmap.getWidth();
    int h = mutableBitmap.getHeight();
    int[] pixels = new int[w * h];
    mutableBitmap.getPixels(pixels, 0, w, 0, 0, w, h);

    for (int r = radius; r >= 1; r /= 2) {
      for (int i = r; i < h - r; i++) {
        for (int j = r; j < w - r; j++) {
          int tl = pixels[(i - r) * w + j - r];
          int tr = pixels[(i - r) * w + j + r];
          int tc = pixels[(i - r) * w + j];
          int bl = pixels[(i + r) * w + j - r];
          int br = pixels[(i + r) * w + j + r];
          int bc = pixels[(i + r) * w + j];
          int cl = pixels[i * w + j - r];
          int cr = pixels[i * w + j + r];

          pixels[(i * w) + j] =
              0xFF000000
                  | (((tl & 0xFF)
                              + (tr & 0xFF)
                              + (tc & 0xFF)
                              + (bl & 0xFF)
                              + (br & 0xFF)
                              + (bc & 0xFF)
                              + (cl & 0xFF)
                              + (cr & 0xFF))
                          >> 3)
                      & 0xFF
                  | (((tl & 0xFF00)
                              + (tr & 0xFF00)
                              + (tc & 0xFF00)
                              + (bl & 0xFF00)
                              + (br & 0xFF00)
                              + (bc & 0xFF00)
                              + (cl & 0xFF00)
                              + (cr & 0xFF00))
                          >> 3)
                      & 0xFF00
                  | (((tl & 0xFF0000)
                              + (tr & 0xFF0000)
                              + (tc & 0xFF0000)
                              + (bl & 0xFF0000)
                              + (br & 0xFF0000)
                              + (bc & 0xFF0000)
                              + (cl & 0xFF0000)
                              + (cr & 0xFF0000))
                          >> 3)
                      & 0xFF0000;
        }
      }
    }

    mutableBitmap.setPixels(pixels, 0, w, 0, 0, w, h);
    return mutableBitmap;
  }
Ejemplo n.º 20
0
  public static int[] getPixelsARGB_8888(final Bitmap pBitmap) {
    final int w = pBitmap.getWidth();
    final int h = pBitmap.getHeight();

    final int[] pixelsARGB_8888 = new int[w * h];
    pBitmap.getPixels(pixelsARGB_8888, 0, w, 0, 0, w, h);

    return pixelsARGB_8888;
  }
Ejemplo n.º 21
0
  public static Bitmap sharpenImageAmeliorate(Bitmap bmp) {
    if (ImageCache.get("sharpenImageAmeliorate") != null) {
      return ImageCache.get("sharpenImageAmeliorate");
    }
    int[] laplacian = new int[] {-1, -1, -1, -1, 9, -1, -1, -1, -1};

    int width = bmp.getWidth();
    int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int idx = 0;
    float alpha = 0.3F;
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    for (int i = 1, length = height - 1; i < length; i++) {
      for (int k = 1, len = width - 1; k < len; k++) {
        idx = 0;
        for (int m = -1; m <= 1; m++) {
          for (int n = -1; n <= 1; n++) {
            pixColor = pixels[(i + n) * width + k + m];
            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            newR = newR + (int) (pixR * laplacian[idx] * alpha);
            newG = newG + (int) (pixG * laplacian[idx] * alpha);
            newB = newB + (int) (pixB * laplacian[idx] * alpha);
            idx++;
          }
        }

        newR = Math.min(255, Math.max(0, newR));
        newG = Math.min(255, Math.max(0, newG));
        newB = Math.min(255, Math.max(0, newB));

        pixels[i * width + k] = Color.argb(255, newR, newG, newB);
        newR = 0;
        newG = 0;
        newB = 0;
      }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    ImageCache.put("sharpenImageAmeliorate", bitmap);
    return bitmap;
  }
Ejemplo n.º 22
0
 public void getRGB(
     final int[] rgbData,
     final int offset,
     final int scanlength,
     final int x,
     final int y,
     final int width,
     final int height) {
   bitmap.getPixels(rgbData, offset, scanlength, x, y, width, height);
 }
  /** アプリ一覧で、あるアプリがクリックされたときのアクションです。 */
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final AppInfo appInfo = (AppInfo) parent.getItemAtPosition(position);
    BitmapDrawable icon = (BitmapDrawable) appInfo.getIcon();

    final Bitmap bmp = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setDither(true);
    canvas.drawBitmap(
        ((BitmapDrawable) icon).getBitmap(),
        null,
        new Rect(0, 0, bmp.getWidth() - 1, bmp.getHeight() - 1),
        paint);
    final int[] pixels = new int[320 * 320];
    bmp.getPixels(pixels, 0, 320, 0, 0, 320, 320);

    // Random Dithering で二値化する
    final Random rand = new Random();
    for (int i = 0; i < pixels.length; i++) {
      final int p = pixels[i];
      // y=0.587*g+0.299*r+0.114b
      final int y =
          Color.green(p) * 587 / 1000 //
              + Color.red(p) * 299 / 1000 //
              + Color.blue(p) * 114 / 1000;

      final int blackOrWhite = (y < rand.nextInt(256)) ? 0 : 255;
      pixels[i] = Color.argb(0xff, blackOrWhite, blackOrWhite, blackOrWhite);
    }

    // 二値化されたデータを印刷用ラスターデータに変換する
    // TODO 二値化の際に変換も一緒に行ったほうが効率的
    final int bmpWidth = bmp.getWidth();
    final int bmpHeight = bmp.getHeight();
    final int black = Color.argb(0xff, 0, 0, 0);
    byte[][] rasterData = new byte[bmpWidth][];
    for (int w = 0; w < rasterData.length; w++) {
      final byte[] line = new byte[4 /*印刷されない領域分*/ + bmp.getHeight() / 8];
      int d = 0;
      for (int h = 0; h < bmpHeight; h++) {
        final int index = h * bmpWidth + w;
        d <<= 1;
        d += (pixels[index] == black ? 1 : 0);
        if (h % 8 == 7) {
          line[4 + h / 8] = (byte) d;
        }
      }
      rasterData[w] = line;
    }

    final Intent intent = new Intent(this, PrintActivity.class);
    intent.putExtra("data", rasterData);
    startActivity(intent);
  }
Ejemplo n.º 24
0
 public void loadFromColorArray() {
   int[] pixels = new int[mOriginalWidth * mOriginalHeight];
   mOriginalBitmap.getPixels(pixels, 0, mOriginalWidth, 0, 0, mOriginalWidth, mOriginalHeight);
   CloseableReference<Bitmap> bitmapRef =
       mBitmapFactory.createBitmap(
           pixels, mOriginalWidth, mOriginalHeight, mOriginalBitmap.getConfig());
   setImageBitmap(bitmapRef);
   checkValid(
       bitmapRef,
       Bitmap.createBitmap(pixels, mOriginalWidth, mOriginalHeight, mOriginalBitmap.getConfig()));
 }
Ejemplo n.º 25
0
 void initializeSoftCursor() {
   Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.cursor);
   int w = bm.getWidth();
   int h = bm.getHeight();
   int[] tempPixels = new int[w * h];
   bm.getPixels(tempPixels, 0, w, 0, 0, w, h);
   // Set cursor rectangle as well.
   bitmapData.setCursorRect(pointer.mouseX, pointer.mouseY, w, h, 0, 0);
   // Set softCursor to whatever the resource is.
   bitmapData.setSoftCursor(tempPixels);
   bm.recycle();
 }
Ejemplo n.º 26
0
  public static Bitmap sunshine(Bitmap bmp, int centerX, int centerY) {
    final int width = bmp.getWidth();
    final int height = bmp.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;
    int radius = Math.min(centerX, centerY);

    final float strength = 150F;
    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;
    for (int i = 1, length = height - 1; i < length; i++) {
      for (int k = 1, len = width - 1; k < len; k++) {
        pos = i * width + k;
        pixColor = pixels[pos];

        pixR = Color.red(pixColor);
        pixG = Color.green(pixColor);
        pixB = Color.blue(pixColor);

        newR = pixR;
        newG = pixG;
        newB = pixB;

        int distance = (int) (Math.pow((centerY - i), 2) + Math.pow(centerX - k, 2));
        if (distance < radius * radius) {

          int result = (int) (strength * (1.0 - Math.sqrt(distance) / radius));
          newR = pixR + result;
          newG = pixG + result;
          newB = pixB + result;
        }

        newR = Math.min(255, Math.max(0, newR));
        newG = Math.min(255, Math.max(0, newG));
        newB = Math.min(255, Math.max(0, newB));

        pixels[pos] = Color.argb(255, newR, newG, newB);
      }
    }

    bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return bitmap;
  }
Ejemplo n.º 27
0
  public Bitmap getResBitmapMutable(Resources res, int bmpResId) {
    Options opts = new Options();
    opts.inDither = false;
    Bitmap b = BitmapFactory.decodeResource(res, bmpResId, opts);

    int w = b.getWidth();
    int h = b.getHeight();
    Bitmap mutable = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    int[] pixels = new int[w * h];
    b.getPixels(pixels, 0, w, 0, 0, w, h);
    mutable.setPixels(pixels, 0, w, 0, 0, w, h);
    return mutable;
  }
Ejemplo n.º 28
0
  private static void updateForView(final RFBClient client, View view) {
    view.setDrawingCacheEnabled(true);
    // view.destroyDrawingCache();
    //		view.buildLayer();

    //		Bitmap bm = Bitmap.createBitmap( view.getMeasuredWidth(), view.getMeasuredHeight(),
    // Bitmap.Config.ARGB_8888);
    //		Canvas c = new Canvas(bm);
    //		view.invalidate();
    //		view.forceLayout();
    //		view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
    //		view.draw(c);
    //
    // view.getDrawingCache();
    Bitmap cache = view.getDrawingCache();
    //		Canvas renderC = new Canvas(cache);
    // view.destroyDrawingCache();
    //		view.layout(0, 0, cache.getWidth(), cache.getHeight());
    //		view.draw(renderC);
    // view.buildDrawingCache(false);

    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int[] pixels = new int[cache.getWidth() * cache.getHeight()];
    cache.getPixels(
        pixels,
        0,
        cache.getWidth(),
        view.getLeft(),
        view.getTop(),
        cache.getWidth(),
        cache.getHeight());
    Rect rect =
        Rect.encode(
            client.getPreferredEncoding(),
            pixels,
            client.getPixelFormat(),
            cache.getWidth(),
            view.getLeft(),
            view.getTop(),
            cache.getWidth(),
            cache.getHeight());
    rect.transform(location[0], location[1]);
    Rect[] rects = {rect};

    try {
      client.writeFrameBufferUpdate(rects);
    } catch (IOException e) {
      Log.e("RemoteDisplay", "error while sending update rect", e);
    }
  }
Ejemplo n.º 29
0
  @Override
  public void run() {
    for (int x = mCurrentX; x < mMaxX; x++) {
      mCallback.run();
      for (int y = mCurrentY; y < mMaxY; y++) {
        int tileindex =
            mRenderer.findBestFit(
                mRenderer.mColors[x][y], x, y, mRenderer.mTileCountX, mRenderer.mTileCountY);

        String path = mRenderer.mTileList.get(tileindex).getFilePath();
        int[] tilepixels;

        if (mRenderer.mExportedTiles.containsKey(path)) {
          tilepixels = mRenderer.mExportedTiles.get(path);
        } else {
          tilepixels = new int[mRenderer.mTileWidth * mRenderer.mTileHeight];
          Bitmap origtile;
          if (mRenderer.mTileList.get(tileindex).isInternal()) {
            origtile =
                BitmapFactory.decodeResource(
                    mRenderer.mBasecontext.getResources(), Integer.parseInt(path));
          } else {
            origtile = BitmapFactory.decodeFile(path);
          }

          if (origtile == null) {
            System.out.println("Bild nicht vorhanden: " + path);
            continue;
          }
          Bitmap tile =
              Bitmap.createScaledBitmap(
                  origtile, mRenderer.mTileWidth, mRenderer.mTileHeight, false);
          //          origtile.recycle();
          tile.getPixels(
              tilepixels,
              0,
              mRenderer.mTileWidth,
              0,
              0,
              mRenderer.mTileWidth,
              mRenderer.mTileHeight);
          mRenderer.mExportedTiles.put(path, tilepixels);
        }

        //          System.out.println("current: x=" +x +"   y="+y);
        mRenderer.setPixels(tilepixels, x, y);
      }
    }
    mCallback.run();
  }
Ejemplo n.º 30
0
 void addPngTexture(String fn, int kind, boolean transparentWhite) {
   InputStream istr;
   Bitmap bitmap = null;
   try {
     istr = assets.open(fn);
     bitmap = BitmapFactory.decodeStream(istr);
     int w = bitmap.getWidth();
     int h = bitmap.getHeight();
     int[] pixels = new int[h * w];
     bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
     GameLibJNIWrapper.add_intarr_texture(w, h, pixels, kind, transparentWhite);
   } catch (IOException e) {
     // handle exception
   }
 }