CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2014
    Posts
    2

    Reading Jpeg pixels and change it

    Hello there,

    I'm trying to read a jpeg pixel and then compare it if it'x bigger than a number to change it.
    either I'm breaking the folder if I start less than 22000 which is very large number shouldn't include the header or I'm getting solid color without any different.

    Code:
    const char* filename ="Hydrangeas.JPG";
    		const char* nfilename ="Hydrangeas1.JPG";
    
    		FILE  *ptr_old, *ptr_new;
    		errno_t err = 0, err1 = 0;
    		int a;
    
    		err = fopen_s(&ptr_old, filename, "rb");
    		err1 = fopen_s(&ptr_new, nfilename, "wb");
    
    		if(err != 0){
    			cout<<"can't find it"<<endl;
    			return  -1;
    			fclose(ptr_old);
    		}
    		if (err1 !=0)
    		{
    			cout<<"can't find it"<<endl;
    			return  -1;
    			fclose(ptr_old);
    		}
    		
    		
    		 int o = 0;
    		 while (1)
    		 {
    			 a = fgetc(ptr_old);
    			 
    			 if (a>=0)
    			 {
    				 if (o>30000)
    				{
    				//	cout<<a<<" : ";
    					if (a>90){
    						a=0;
    					}else{
    						a=255;}
    				//cout<<a<<endl;
    				}
    				// cout<<a<<endl;
    				 fputc(a,ptr_new);
    			 }else{
    				break;
    			 }
    			 o++;
    		 }
    		 
    		
    		fclose(ptr_new);
    		fclose(ptr_old);
    		return  0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reading Jpeg pixels and change it

    Your problem description is very poor and hard to understand. Could you explain it more clear?
    Besides, what do you mean by "pixel" in that context, what are these magic numbers like 5000, 90, 22000 in your code snippet and what does the "breaking the folder" mean?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2014
    Posts
    2

    Re: Reading Jpeg pixels and change it

    Sorry I think I wrote it in hurry which I made a lot of mistakes.

    What I want to do is to read an image and then check the pixel or the byte value of that pixel if it's bigger than 180 I'll make it o other wise I'll make it 255. while I'm reading the image and changing the byte values I touched the header of the image which it leads me to unworkable image. the number you saw above is just I was trying to change after the header bytes.

    thanks in advance

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Reading Jpeg pixels and change it

    Quote Originally Posted by fedail View Post
    What I want to do is to read an image and then check the pixel or the byte value of that pixel if it's bigger than 180 I'll make it o other wise I'll make it 255. while I'm reading the image and changing the byte values I touched the header of the image which it leads me to unworkable image. the number you saw above is just I was trying to change after the header bytes.
    Just one thing -- manipulating a JPEG file is much more complex than the code you're showing.

    Scroll to the Syntax and Structure section here:
    http://en.wikipedia.org/wiki/JPEG

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Reading Jpeg pixels and change it

    1) a jpeg is compressed, so you can't directly "find a pixel" in the jpg file,
    you first need to load and decompress the entire image, then you can locate pixels within the image.
    changing pixels then involves recompressing the image and writing the compressed stream to a file.

    2) jpeg is lossy, so the color you are reading may not be the exact color from the original image.

    1+2 combined)
    continuously recompressing a lossy format will over time introduce artefacts and other forms of image distortion. Because of that, jpeg is not a good format to use for an intermediary storage.

  6. #6
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: Reading Jpeg pixels and change it

    Consider using the CImg Library which is a small, open source, C++ toolkit for image processing.

    http://cimg.sourceforge.net/

    Best regards,

    Doron Moraz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured