Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How do i make the .bmp image to be black/white ?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How do i make the .bmp image to be black/white ?

    I have this small program

    package fisiere;
     
    import java.io.*;
    import java.util.Scanner;
     
     
     
    public class Fisieremain {
     
     
    	public static void main(String[] args) {
     
     
     
     
    		int B;
    		int G;
    		int R;
     
     
    		try
    		{
    		BufferedInputStream fisier = new BufferedInputStream(new FileInputStream("pisica.bmp"));    //file i read bytes from
    		BufferedOutputStream  fisier2 = new BufferedOutputStream (new FileOutputStream("C:/Users/npiulitza/Desktop/poza/poze2094.bmp"));    // here i write them
     
    		for (int i =0; i<54; i++)
    		{
    			fisier2.write(fisier.read());    // here i copy the header of the file
    		}
     
    		while (true)
    		{
    			B = fisier.read();     // here i read values for each of the three colors     
    			G = fisier.read();
    			R = fisier.read();
     
    			fisier2.write(B);     // and here i write them to second file
    			if (B==-1)
    				break;        // the bubble ends when there are no more bytes to read
    			fisier2.write(G);
    			fisier2.write(R);
     
     
    		}
    		fisier.close();
    		fisier2.close();
     
    		}
     
    		catch (IOException e)
    		{
    			System.out.println(e);
    		}
     
     
    	}
    }


    In this program, i read from a .bmp files the blue, yellow and read color values for each pixel of the image. And, after write them as bytes to another .bmp file. It works, i could even manage to change colors little, if i make blue and yellow to be 0, the image will be red, but i wonder if there is any formula for obtaining a black/white image. Searched little on google but did not find something to help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    Are you looking for an algorithm for converting pixels to either black or white?
    Read a pixel and change its color to either 0x000000 or 0xFFFFFF

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    No, i do not need to use HTML colors. In my code, B is blue value (from 0 to 256) , G is yellow and R is red. And there where i write them to the file

    fisier2.write(B); // and here i write them to second file
    if (B==-1)
    break; // the bubble ends when there are no more bytes to read
    fisier2.write(G);
    fisier2.write(R);
    i need to sum or make an operation with these values. For example if i decrease each of them like this

    fisier2.write(B-100); // and here i write them to second file
    if (B==-1)
    break; // the bubble ends when there are no more bytes to read
    fisier2.write(G-100);
    fisier2.write(R-100);
    My picture will have negative colors. And i remember i heard some time ago it is a formula to make it black/white ( i think 2 of them need to be 0 for each pixel and one should have all the colors added or decreased or something)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    Sorry, i have not ideas about the algorithms you mention.
    If you only want black and white then you need to write either 0, 0, 0 or FF FF FF for the three bytes.

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    No, i do not want like that, i want the picture become as if it was taken with black/white camera, not that some pixels be pure black and others pure white, it should also have gray pixels. Well, i will search little more and will try discover the formula myself. And i am sure i have to mix somehow blue,red and yellow color intensities.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    it should also have gray pixels
    How do i make the .bmp image to be black/white ?
    There is s contradiction here. B/W vs gray.

  7. #7
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    Hmm look mate, i have writen for every color
    fisier2.write(B/2);
    fisier2.write(B/2);
    fisier2.write(B/2);
    And the picture that was

    Tom+y+Jerry.jpg


    And, after it is
    Tom+y+Jerry2.jpg

    And...you do not say the second picture in black/white ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How do i make the .bmp image to be black/white ?

    I don't see any white in the second picture.

Similar Threads

  1. Hello from The Great White North
    By Jimmy_1963 in forum Member Introductions
    Replies: 1
    Last Post: July 5th, 2011, 09:07 AM
  2. Is it possible to make a background to in image transparent
    By Zein in forum Java Theory & Questions
    Replies: 10
    Last Post: June 13th, 2011, 04:18 AM
  3. black jack game
    By 314159 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 6th, 2011, 10:42 AM
  4. .gif image becomes black and white (against my wishes)
    By Jolanda in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 02:27 PM
  5. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM