LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-07-2005, 05:27 AM   #1
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Rep: Reputation: 15
My tiff to jpeg program not working.....why?


#include <stdio.h>
#include <jpeglib.h>
#include <tiffio.h>

int main(int argc, char *argv[])
{
int quality = 100;
char * filename;
/*Tiff code*/
TIFF *image;

uint32 image_width, image_height, *raster;
tsize_t stripSize;
char *img, *upto;
unsigned long imagesize, c, d, e;

// Open the TIFF image
if((image = TIFFOpen("input.tif", "r")) == NULL){
fprintf(stderr, "Could not open incoming image\n");
exit(42);
}
filename = argv[1];
// Find the width and height of the image
TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &image_width);
TIFFGetField(image, TIFFTAG_IMAGELENGTH, &image_height);
imagesize = image_height * image_width + 1;

if((raster = (uint32 *) malloc(sizeof(uint32) * imagesize)) == NULL){
fprintf(stderr, "Could not allocate enough memory\n");
exit(42);
}

// Read the image into the memory buffer
if(TIFFReadRGBAStrip(image, 0, raster) == 0){
fprintf(stderr, "Could not read image\n");
exit(42);
}
JSAMPLE * image_buffer;

struct jpeg_compress_struct cinfo;

struct jpeg_error_mgr jerr;
/* More stuff */
FILE * outfile; /* target file */
JSAMPROW row_pointer[image_width]; /* 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("output.jpg", "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_dest(&cinfo, outfile);

/* Step 3: set parameters for compression */


cinfo.image_width = image_width; /* image width and height, in pixels */
cinfo.image_height = image_height;
cinfo.input_components = 3; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);

/* Step 4: Start compressor */
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
/* Read every pixel in the correct order into the JSAMPLE buffer/
d = 0;
int counter = 0;
for(e = image_height - 1; e != -1; e--){
for(c = 0; c < image_width; c++){
unsigned char Red = TIFFGetR(raster[e * image_width + c]);
image_buffer[counter];
counter++;
unsigned char Green = TIFFGetG(raster[e * image_width + c]);
image_buffer[counter];
counter++;
unsigned char Blue = TIFFGetB(raster[e * image_width + c]);
image_buffer[counter];
counter++;
}
}
/* This is the problem area it only reads in one scanline and gives an error reading the next line*/
counter = 0;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[counter] = & image_buffer[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer, counter);
counter++;
}

jpeg_finish_compress(&cinfo);
/* After finish_compress, we can close the output file. */
fclose(outfile);
TIFFClose(image)
jpeg_destroy_compress(&cinfo);

}

Last edited by James_dean; 11-07-2005 at 05:35 AM.
 
Old 11-08-2005, 12:09 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Ok, there are two problems with your post.

First, whenever you post code, please, please, please use [code] tags. Just click the "Code" button above the main text box for a post/reply and the code tags will be inserted for you.

Second, you don't tell us anything about why the program is failing at all. All you've given us is a source code dump. What isn't working? How do you know it's not working? Is it a compile error? Is it a segmentation fault? Is it a garbage conversion? What have you tried to fix it? Have you been able to isolate the problem to a specific section? It may be a relatively short program, but you've got to narrow the focus when asking for help.

EDIT:
Looking at the post again, you do restrict it to a section of the code, but that information is buried in the code itself. It would be helpful to others that read the post at first to pull that out and say what the problem is right up front.

Last edited by Dark_Helmet; 11-08-2005 at 12:11 AM.
 
Old 11-08-2005, 12:21 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
By the way, is the code above free of typos?

For instance:
Code:
 /* Step 4: Start compressor */
jpeg_start_compress(&cinfo, TRUE);
row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */
/* Read every pixel in the correct order into the JSAMPLE buffer/
d = 0;
int counter = 0;
for(e = image_height - 1; e != -1; e--){
  for(c = 0; c < image_width; c++){
    unsigned char Red = TIFFGetR(raster[e * image_width + c]);
    image_buffer[counter];
    counter++;
    unsigned char Green = TIFFGetG(raster[e * image_width + c]);
    image_buffer[counter];
    counter++;
    unsigned char Blue = TIFFGetB(raster[e * image_width + c]);
    image_buffer[counter];
    counter++;
  }
}
 /* This is the problem area it only reads in one scanline and gives an error reading the next line*/
Everything in blue is commented out, because the code is missing the closing asterisk on this line:
Code:
/* Read every pixel in the correct order into the JSAMPLE buffer/
 
Old 11-08-2005, 12:39 AM   #4
James_dean
Member
 
Registered: Sep 2005
Posts: 41

Original Poster
Rep: Reputation: 15
Dark helmet i appreciate the advice and i unfortunately did not think too much before posting the code. It was just posted without too much care and explanations and that was of course pointless and i apologise if it annoyed you. Well the problem is writing the image information to the jpeg file. The code above is not free of typos, when i posted it i deleted alot of comments and this caused the typo above. The code compiles fine.......
Specifically this part there is a problem:
Code:
/* This is the problem area it only reads in one scanline and gives an error reading the next line*/
counter = 0;
while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[counter] = & image_buffer[cinfo.next_scanline * row_stride];
jpeg_write_scanlines(&cinfo, row_pointer, counter);
counter++;
}
I have tried just writing the whole image data in one go like:
Code:
counter = 0;
while (cinfo.next_scanline < cinfo.image_height) {
jpeg_write_scanlines(&cinfo, image_buffer, counter);
counter++;
}
With this code my program ran fine but the output image was wrong. What i want to know is how to correctly read in the image information from the tiff image and correctly write it out to the output image(jpeg file)

Last edited by James_dean; 11-08-2005 at 12:43 AM.
 
Old 11-08-2005, 03:17 AM   #5
scuzzman
Senior Member
 
Registered: May 2004
Location: Hilliard, Ohio, USA
Distribution: Slackware, Kubuntu
Posts: 1,851

Rep: Reputation: 47
In this code segment:
Code:
while (cinfo.next_scanline < cinfo.image_height) {
You're using less than instead of less than or equal to. Hence, you're grabbing all but the last part... That might be a problem...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting tiff to jpeg freebies Linux - Software 8 06-26-2013 10:17 PM
PDF to TIFF chr15t0 Linux - Software 5 05-09-2013 09:47 AM
Single Page tiff to Multi Page tiff James_dean Programming 3 11-10-2005 10:50 PM
Whats the problem with this raw to tiff program? James_dean Programming 8 10-28-2005 12:20 AM
TIFF Resolution Downgrade meshcurrent Linux - General 2 01-28-2003 06:14 AM


All times are GMT -5. The time now is 05:53 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration