sneakpeak101
September 17th, 2005, 12:48 AM
Hi everyone -
I'm having trouble using the jpeglib and compressing a .bmp into .jpeg, i'm still very new to C++ and what i'm looking for is some sample code or pointers that can help me out, i'm compiling my program using the latest version of dev-c++.
Basically, my program gets a picture from my webcam using capFileSaveDIB(ghCapWnd, "c:\\test.bmp");
Now i'm not sure if it matters if it is saved as a .bmp or .dib, or .jpeg.. of even if it matters, I saw a few examples on net using the same line of code I have so i'm assuming it's correct. The image is saved, and I can view it perfectly using windows picture and fax viewer.
Here is the next piece of code that i'm having trouble with.. it does create the jpeg at 1.78kb (previous size was 225kb) , but the picture is completely black.. I think it's because JSAMPLE .. but i'm not sure.
extern JSAMPLE * image_buffer;
int image_height; /* Number of rows in image */
int image_width;
void write_JPEG_file (char *filename, int quality)
{
struct jpeg_compress_struct cinfo;
cinfo.image_height = 320;
cinfo.image_width = 240;
int imagesize = 3*320*240;
JSAMPLE image_buffer[imagesize];
int i;
for(i=0; i<imagesize; i++) {
image_buffer[i] = (JSAMPLE)0;
}
struct jpeg_error_mgr jerr;
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen("test.jpeg", "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = 320;
cinfo.image_height = 240;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 3;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
(void) jpeg_write_scanlines(&cinfo, row_pointer, cinfo.image_height);
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
-
write_JPEG_file ("test.jpeg", 100);
I didn't write the code above, I simply found it on a search engine and tried playing with it, seeing how it was the only thing that would compile for me. The image I use to compress it the one created using capFileSaveDIB(); above, I just edit the code to create a .jpeg instead of a .bmp like it is above.
If anyone can help me it would be much appreciated.. i'm willing to learn, if someone can just point me in the right area.. i've tried and tried to figure out what is wrong but I can't seem to fix it and i'm not used to using the jpeglib, any help is appreciated thanks all.
I'm having trouble using the jpeglib and compressing a .bmp into .jpeg, i'm still very new to C++ and what i'm looking for is some sample code or pointers that can help me out, i'm compiling my program using the latest version of dev-c++.
Basically, my program gets a picture from my webcam using capFileSaveDIB(ghCapWnd, "c:\\test.bmp");
Now i'm not sure if it matters if it is saved as a .bmp or .dib, or .jpeg.. of even if it matters, I saw a few examples on net using the same line of code I have so i'm assuming it's correct. The image is saved, and I can view it perfectly using windows picture and fax viewer.
Here is the next piece of code that i'm having trouble with.. it does create the jpeg at 1.78kb (previous size was 225kb) , but the picture is completely black.. I think it's because JSAMPLE .. but i'm not sure.
extern JSAMPLE * image_buffer;
int image_height; /* Number of rows in image */
int image_width;
void write_JPEG_file (char *filename, int quality)
{
struct jpeg_compress_struct cinfo;
cinfo.image_height = 320;
cinfo.image_width = 240;
int imagesize = 3*320*240;
JSAMPLE image_buffer[imagesize];
int i;
for(i=0; i<imagesize; i++) {
image_buffer[i] = (JSAMPLE)0;
}
struct jpeg_error_mgr jerr;
FILE * outfile; /* target file */
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
int row_stride; /* physical row width in image buffer */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
if ((outfile = fopen("test.jpeg", "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = 320;
cinfo.image_height = 240;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 3;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
(void) jpeg_write_scanlines(&cinfo, row_pointer, cinfo.image_height);
jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);
}
-
write_JPEG_file ("test.jpeg", 100);
I didn't write the code above, I simply found it on a search engine and tried playing with it, seeing how it was the only thing that would compile for me. The image I use to compress it the one created using capFileSaveDIB(); above, I just edit the code to create a .jpeg instead of a .bmp like it is above.
If anyone can help me it would be much appreciated.. i'm willing to learn, if someone can just point me in the right area.. i've tried and tried to figure out what is wrong but I can't seem to fix it and i'm not used to using the jpeglib, any help is appreciated thanks all.