Char [] to Image File (JPEG)

This is a discussion on Char [] to Image File (JPEG) within the C Programming forums, part of the General Programming Boards category; Hi there! I have a variable of the type char [] that has data from an image, i would like ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    14

    Char [] to Image File (JPEG)

    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

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Post the code that's giving you trouble.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This works for me:

    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;
    }
    However, I notice that if you use fread like this:

    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

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    14
    I have this struct and i want to save unsigned char picture[PTEID_MAX_PICTUREH_LEN]; into a JPEG file. I don't know how to do this . I tried writing into a file with fwrite : main code

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    14
    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

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    >> 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:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TranT View Post
    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
    a="1243" and a="asdf" have the same length.

    You may have to start with a method that you can get to work and slowly adapt it to your needs.
    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

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    14
    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?

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TranT View Post
    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]!
    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.

    You may have to start with a method that you can get to work and slowly adapt it to your needs. What method :x?
    There it is!

    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

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    14
    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

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TranT View Post
    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
    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:
    Code:
    PTEID_PIC pic;
    ret= PTEID_GetPic(&pic);
    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 (&).
    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

  13. #13
    Registered User
    Join Date
    Jun 2009
    Posts
    14
    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.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by TranT View Post
    Any more ideas?
    The only thing I can think of is that these two things:
    Code:
    unsigned char imageinfo[PTEID_MAX_IMAGEINFO_LEN];
    unsigned char picture[PTEID_MAX_PICTUREH_LEN];
    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.

    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

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MK27
    Here's a link I just found, not very promising I'm afraid:
    JPEG Header Format
    altho apparently the first two bytes are always "0xFFD8"
    In fact it must mean the last two bits of the first two bytes:
    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;
    }
    I just tried this on a .jpg:

    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

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  5. File Input / Output Help?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-18-2002, 10:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21