Skip to content

Commit

Permalink
fixed scanline bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sufengniu committed Apr 8, 2013
1 parent 143ecb5 commit 612d89c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 25 deletions.
2 changes: 1 addition & 1 deletion libtiff_load/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ tar:
tar cxvf *.c *.h makefile

clean:
rm -rf *.o scanline scanstrip scantile output.tif
rm -rf *.o scanline scanstrip scantile output.tif *.dat
47 changes: 33 additions & 14 deletions libtiff_load/scanline.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <tiffio.h>
#include <stdlib.h>
#include <string.h>

#define BIT_PER_SAMPLE 16 // defined by camera property
#define SAMPLE_PER_PIXEL 1 // sample per pixel default is 1
Expand All @@ -14,12 +15,14 @@ typedef struct tiff_info{
unsigned short spp; // sample per pixel, 16 bits per pixel for ANL camera
unsigned short bps; // bit per sample, default is 1
int line_size;
int image_size;
} tiff_info;

int main(int argc, char **argv)
{
int r, c; // height index and width index

uint16 s;

struct tiff_info *info;
info = (struct tiff_info *)malloc(sizeof(struct tiff_info));

Expand All @@ -33,7 +36,9 @@ int main(int argc, char **argv)
tsize_t strip_size, buffer_size;
tstrip_t strip_max, strip_count;
unsigned long image_offset, result;
unsigned long count;
unsigned char *input_buffer;
FILE *output_file;

input_file = TIFFOpen(argv[1], "r");
if(input_file == NULL){
Expand All @@ -53,16 +58,11 @@ int main(int argc, char **argv)
printf("bit per sample = %d\n", info->bps);
printf("sample per pixel = %d\n", info->spp);
printf("photo metirc is %d\n", info->photo_metric);

if(info->spp == 0){
printf("Warning: the sample per pixel is not set, which should be 1!\n");
printf("automatically set sample per pixel to be 1\n");
info->spp = 1;
}


info->line_size = TIFFScanlineSize(input_file);

if((input_image = (unsigned char *)_TIFFmalloc(info->line_size * info->length)) == NULL){
info->image_size = info->line_size * info->length;

if((input_image = (unsigned char *)_TIFFmalloc(info->image_size)) == NULL){
fprintf(stderr, "Could not allocate enough memory for the uncompressed image!\n");
exit(42);
}
Expand All @@ -75,15 +75,34 @@ int main(int argc, char **argv)
printf("the line size is %d\n", info->line_size);
printf("reading tif files ... \n");
for(r = 0; r < info->length; r++){
TIFFReadScanline(input_file, scanline, r, 0);
for(c = 0; c < info->width; c++)
for (s = 0; s < info->spp; s++){
TIFFReadScanline(input_file, scanline, r, s);
}
for(c = 0; c < 2*info->width; c++)
{
input_image[info->width * r + c] = *(scanline + c);

input_image[2*info->width * r + c] = *(scanline + c);
}

}

if(info->photo_metric != PHOTOMETRIC_MINISWHITE){
// Flip bits
printf("Fixing the photometric interpretation\n");

for(count = 0; count < info->image_size; count++)
input_image[count] = ~input_image[count];
}

output_file = fopen("loaded_image_line.dat", "w");

for(count = 0; count < info->image_size; count++){
fprintf(output_file, "%02x", (unsigned char) input_image[count]);
if((count + 1) % (info->width / 8) == 0) fprintf(output_file, "\n");
else fprintf(output_file, " ");
}

fclose(output_file);

_TIFFfree(input_image);
_TIFFfree(scanline);

Expand Down
34 changes: 25 additions & 9 deletions libtiff_load/scanstrip.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <tiffio.h>
#include <stdlib.h>
#include <string.h>

#define BIT_PER_SAMPLE 16 // defined by camera property
#define SAMPLE_PER_PIXEL 1 // sample per pixel default is 1
Expand All @@ -14,6 +15,7 @@ typedef struct tiff_info{
unsigned short spp; // sample per pixel, 16 bits per pixel for ANL camera
unsigned short bps; // bit per sample, default is 1
int line_size;
int image_size;
} tiff_info;

int main(int argc, char **argv)
Expand All @@ -29,12 +31,15 @@ int main(int argc, char **argv)
TIFF *input_file;
int io_status; // status of io operation

/* according to tiffio library tsize_t is int32, tstrip_t is uint32 */
/* according to tiffio library tsize_t is int32, tstrip_t is uint32 */
tsize_t strip_size, buffer_size;
tstrip_t strip_max, strip_count;
tstrip_t strip_max, strip_count, strip_num;
unsigned long image_offset, result;
unsigned long count;

/* debugging purpose */
FILE *output_file;

input_file = TIFFOpen(argv[1], "r");
if(input_file == NULL){
fprintf(stderr, "ERROR: Could not open input image!\n");
Expand Down Expand Up @@ -63,11 +68,11 @@ int main(int argc, char **argv)

printf("the strip_size is %u\n", strip_size);
printf("the strip_max is %u\n", strip_max);
printf("the buffer size if %u\n", buffer_size);
printf("the image size if %u\n", buffer_size);
printf("reading tif files...\n");

if((stripbuffer = (unsigned char *)_TIFFmalloc(buffer_size)) == NULL){
fprintf(stderr, "Could not allocate enought memory for the uncompressed image!\n");
fprintf(stderr, "Could not allocate enough memory for uncompressed image!\n");
exit(42);
}

Expand All @@ -82,17 +87,28 @@ int main(int argc, char **argv)
result = TIFFReadEncodedStrip( input_file, strip_count,
stripbuffer + image_offset,
strip_size);

image_offset += result;
}

if(info->photo_metric != PHOTOMETRIC_MINISWHITE){
// Flip bits
printf("Fixing the photometric interpretation\n");
// Flip bits
printf("Fixing the photometric interpretation\n");

for(count = 0; count < buffer_size; count++)
stripbuffer[count] = ~stripbuffer[count];
for(count = 0; count < buffer_size; count++)
stripbuffer[count] = ~stripbuffer[count];
}

output_file = fopen("loaded_image_strip.dat", "w");

for(count = 0; count < buffer_size; count++){
fprintf(output_file, "%02x", (unsigned char) stripbuffer[count]);
if((count + 1) % (info->width / 8) == 0) fprintf(output_file, "\n");
else fprintf(output_file, " ");
}

fclose(output_file);

// _TIFFfree(input_image);
_TIFFfree(stripbuffer);

Expand Down
5 changes: 4 additions & 1 deletion temp_test/makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
CC=gcc
CFLAGS=-O3

all: read
all: read read_line

read: read.o
$(CC) $(CFLAGS) read.o -o read -ltiff -lm

read.o: read.c
$(CC) $(CFLAGS) -c read.c -o read.o

read_line: read_line.c
$(CC) $(CFLAGS) -c read_line.c -o read_line -ltiff

tar:
tar cxvf *.c *.h makefile

Expand Down
Binary file added temp_test/read
Binary file not shown.
Binary file added temp_test/read.o
Binary file not shown.
28 changes: 28 additions & 0 deletions temp_test/read_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <tiffio.h>

void main()
{
TIFF* tif = TIFFOpen("myfile.tif", "r");
if (tif) {
uint32 imagelength;
tdata_t buf;
uint32 row;

TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imagelength);
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
buf = _TIFFmalloc(TIFFScanlineSize(tif));
if (config == PLANARCONFIG_CONTIG) {
for (row = 0; row < imagelength; row++)
tiffreadscanline(tif, buf, row);
} else if (config == planarconfig_separate) {
uint16 s, nsamples;

tiffgetfield(tif, tifftag_samplesperpixel, &nsamples);
for (s = 0; s < nsamples; s++)
for (row = 0; row < imagelength; row++)
tiffreadscanline(tif, buf, row, s);
}
_tifffree(buf);
tiffclose(tif);
}
}

0 comments on commit 612d89c

Please sign in to comment.