Showing posts with label Photoshop operations. Show all posts
Showing posts with label Photoshop operations. Show all posts

Sunday, July 7, 2013

Changing brightness, hue and saturation of an image

Here is the code to change brightness, hue and saturation of an image. These are the respective functions. You just need to refer code of seekbar in order to implement this.

1.Brightness
2.Hue
3.Saturation

1. Brightness

public static Bitmap doBrightness(Bitmap src, int value) {
       
// get image size 
        int width = src.getWidth();
        int height = src.getHeight();

        //creating output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        int A, R, G, B;
        int pixel;
        //iterations through pixels
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {

               
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // changing R, G, B values to change brightness as per value on seekbar
                R += value;
                if(R > 255) { R = 255; }
                else if(R < 0) { R = 0; }

                G += value;
                if(G > 255) { G = 255; }
                else if(G < 0) { G = 0; }

                B += value;
                if(B > 255) { B = 255; }
                else if(B < 0) { B = 0; }
                //output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        return bmOut;
    }


2. Hue

      public static Bitmap applyHueFilter(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[0] *= level;
                HSV[0] = (float) Math.max(0.0, Math.min(HSV[0], 360.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;       
    }


3. Saturation

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;
    }

Wednesday, July 3, 2013

Photoshop operations on image

Various photoshop apps are available in android. Various operations like rotating, flipping, mirror image, water image, converting to grayscale are available. Also advanced features like snow effect are also possible.

  1. Rotate
  2. Flip (Mirror image)
  3. Gray Scale
  4. Snow Effect
1. Rotate Right
        
          //Get image set to imageview
          Imageview.buildDrawingCache();
            bitmap = Imageview.getDrawingCache();
            Matrix matrixr = new Matrix();

            //Rotate matrix
            matrixr.postRotate(270);
            bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrixr, true);


 Rotate Left

          //Get image set to imageview
          Imageview.buildDrawingCache();
            bitmap = Imageview.getDrawingCache();
            Matrix matrixr = new Matrix();

            //Rotate matrix
            matrixr.postRotate(90);
            bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrixr, true);


2. Flip Horizontal

          //Get image set to imageview
          Imageview.buildDrawingCache();
            bitmap = Imageview.getDrawingCache();
            Matrix matrixr = new Matrix();

            //Scale matr
            matrixr.preScale(-1.0f, 1.0f );
            bmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrixr, true);
  

3. Gray Scale

        public static Bitmap convertToGray(Bitmap src)
        {
       //setting red, green, blue factors to convert image to grayscale
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;
       //create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        int A,R,G,B;
        int pixel;
      
        int width = src.getWidth();
        int height  =src.getHeight();
       //pixels iteration
        for(int x=0;x<width;++x)
        {
            for(int y=0;y<height;++y)
            {
                pixel = src.getPixel(x, y);
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
               // Generating pixels for gray scale image
                R = G = B = (int) (G*GS_GREEN + R*GS_RED + B*GS_BLUE);
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        return bmOut;
    }
 


4.Snow Effect

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(COLOR_MAX, COLOR_MAX, COLOR_MAX);
                }                          
            }
        }
        // output bitmap              
        Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
        return bmOut;
    }