-
February 28th, 2014, 02:33 PM
#1
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;
}
-
March 2nd, 2014, 08:26 AM
#2
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
-
March 2nd, 2014, 08:40 AM
#3
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
-
March 2nd, 2014, 11:50 AM
#4
Re: Reading Jpeg pixels and change it
Originally Posted by fedail
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
-
March 4th, 2014, 07:38 AM
#5
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.
-
March 6th, 2014, 09:54 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|