C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 04:14 PM   #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
TranT is offline   Reply With Quote
Old 06-29-2009, 04:23 PM   #2
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,922
Post the code that's giving you trouble.
Sebastiani is offline   Reply With Quote
Old 06-29-2009, 04:41 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-29-2009, 04:46 PM   #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
TranT is offline   Reply With Quote
Old 06-29-2009, 04:55 PM   #5
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,922
>> 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...
Sebastiani is offline   Reply With Quote
Old 06-29-2009, 04:58 PM   #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
TranT is offline   Reply With Quote
Old 06-29-2009, 05:06 PM   #7
Guest
 
Sebastiani's Avatar
 
Join Date: Aug 2001
Posts: 4,922
>> 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?
Sebastiani is offline   Reply With Quote
Old 06-29-2009, 05:07 PM   #8
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-30-2009, 06:18 AM   #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?
TranT is offline   Reply With Quote
Old 06-30-2009, 06:26 AM   #10
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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.

Quote:
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...
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-30-2009, 06:47 AM   #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
TranT is offline   Reply With Quote
Old 06-30-2009, 07:02 AM   #12
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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 (&).
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-30-2009, 07:12 AM   #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.
TranT is offline   Reply With Quote
Old 06-30-2009, 07:17 AM   #14
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS

Last edited by MK27; 06-30-2009 at 07:21 AM.
MK27 is online now   Reply With Quote
Old 06-30-2009, 07:27 AM   #15
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,943
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
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
C++ ini file reader problems guitarist809 C++ Programming 7 09-04-2008 06:02 AM
Game Pointer Trouble? Drahcir C Programming 8 02-04-2006 02:53 AM
Need help understanding info in a header file hicpics C Programming 8 12-02-2005 12:36 PM
File Input / Output Help? Unregistered C Programming 3 05-18-2002 10:20 AM


All times are GMT -6. The time now is 05:06 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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