Hi there! I have a variable of the type char [] that has data from an image, i would like to know how can i write that data into a JPEG file! I've tried fwrite but the image isn't shown
Thank you
Hi there! I have a variable of the type char [] that has data from an image, i would like to know how can i write that data into a JPEG file! I've tried fwrite but the image isn't shown
Thank you
Post the code that's giving you trouble.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
This works for me:
However, I notice that if you use fread like this:Code:#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { int size=0; char image[250000] = {0}; FILE *in = fopen(argv[1],"r"), *out; size = fread(image,1,250000,in); fclose(in); out = fopen("test.jpg","w"); fwrite(image,1,size,out); return 0; }
while (fread(image,1,1,in)) size++;
ie, call it multiple times, the jpg does not come out.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
>> I tried writing into a file with fwrite
You're should be checking the return value of fwrite. I've never used that library so I have no idea if you're using it correctly. Double check the documentation and make sure you are calling all of the functions properly, but IMO, this is probably not an fwrite issue...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
I am getting the things from the structure correctly because i checked the "piclength" and it is the same as the written file but i don't see the image at all :s
>> I am getting the things from the structure correctly because i checked the "piclength" and it is the same as the written file but i don't see the image at all :s
How are you loading the image?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
How are you loading the image? The image is stored in a smartcard and I think that the image data is in that unsigned char picture[PTEID_MAX_PICTUREH_LEN]!
You may have to start with a method that you can get to work and slowly adapt it to your needs. What method :x?
By "you" you mean me & the bit of code in post #3? I'm just loading it from a .jpg file. Compile that code and run it using (a valid) jpg file as an argument on the command-line, eg.
./a.out somepicture.jpg
and it will copy this to "test.jpg". The two files will be identical.
There it is!You may have to start with a method that you can get to work and slowly adapt it to your needs. What method :x?
But you have to be certain about the source you are dealing with. Have you been able to view the jpeg on the smart card using normal means? A smart card is just a small filesystem (generally). What kind of file is the image in? A .jpg? There is more than one kind of image file, so if it is not already explicitly a jpeg, of course writing it to "somefile.jpg" will not work...
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
From what I've read i know that the picture is compressed in JPEG/JPEG2000 and in the PTEID API the only function I have is :
long PTEID_GetPic(PTEID_PIC *PicData) - Read the picture
PicData : OUT -> The address of the record PTEID_PIC
Hmmm. And it comes out the right size. It probably should work then...
I was just looking at your pastebin code again. I don't think "wb" is a proper argument to fwrite() -- it is just for the lowest level write(). Anyway, going by my example, it is not necessary, so try with just "w".
Another *possible* problem:
Looking at the struct, I think these are mismatched. Either you should make "pic" a pointer and malloc(sizeof(PTEID_PC)) or you should not use the address of operator (&).Code:PTEID_PIC pic; ret= PTEID_GetPic(&pic);
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
I've tried fwrite() with "w" with no success and changed the PTEID_PIC pic to a pointer and got the same result :x.
I've also tried chaging the fwrite(..,..,sizeof(pic->picture),..) and didn't work too :/
http://pastebin.com/m7017e58a
Any more ideas?
Last edited by TranT; 06-30-2009 at 07:15 AM.
The only thing I can think of is that these two things:
might be intended to be contiguous and that imageinfo is the jpeg header. So you may want to try reading those two into one buffer, or using w+ to append one to the other in the file. Or (best idea) first find out what a jpeg header should look like and try and find out where it is in the data you are reading in.Code:unsigned char imageinfo[PTEID_MAX_IMAGEINFO_LEN]; unsigned char picture[PTEID_MAX_PICTUREH_LEN];
Here's a link I just found, not very promising I'm afraid:
http://www.fastgraph.com/help/jpeg_header_format.html
altho apparently the first two bytes are always "0xFFD8"
Kinda sketchy tho. I guess you are "hacking" an interface of some sort here, as opposed to following an actual API?
Last edited by MK27; 06-30-2009 at 07:21 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
In fact it must mean the last two bits of the first two bytes:Originally Posted by MK27
I just tried this on a .jpg:Code:#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { char buffer[128]; FILE *in = fopen(argv[1],"r"); fread(buffer,128,1,in); printf("%x %x\n",buffer[0],buffer[1]); return 0; }
ffffffff ffffffd8
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge