Esempio n. 1
0
Stack *Read_LSM_Stack(char *file_name)
{
  Stack *stack;

  TIFF  *tif;
  int    depth, width, height, kind;

  tif = Open_Tiff(file_name,"r");
  if (!tif) return 0; //070805, bu Hanchuan Peng
	
  depth = 1;
  while (TIFFReadDirectory(tif))
    depth += 1;
  TIFFClose(tif);
  depth = depth / 2;		/* half the dirs are thumbnails */

  tif = Open_Tiff(file_name,"r");
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

  kind  = determine_kind(tif);
  stack = new_stack(depth*height*width*kind,"Read_Stack");

  stack->width  = width;
  stack->height = height;
  stack->depth  = depth;
  stack->kind   = kind;

  printf("test1 done\n");

  int d;

  d = 0;
  /* read every other directory (real data, the in between are thumbnails */
  while (1)
  {
	  read_directory(tif,Select_Plane(stack,d),"Read_Stack");
	  
	  d += 1;
	  if (!TIFFReadDirectory(tif)) break;
	  /* skip the one we just read, it's a thumbnail  */

	  if (!TIFFReadDirectory(tif)) break;
	  
	  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
	  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
	  if (width != stack->width || height != stack->height)
          error("Images of stack are not of the same dimensions!",NULL);
	  
	  kind = determine_kind(tif);
	  if (kind != stack->kind)
          error("Images of stack are not of the same type (GREY, GREY16, or COLOR)!",NULL);
  }
  
  TIFFClose(tif);

  return (stack);
}
Esempio n. 2
0
Stack *Read_Stack_Planes(char *prefix, int num_width, int first_num)
{ Stack *stack;

  char  sname[1000];
  int   width, height, depth, kind;
  TIFF *tif;

  depth = 0;
  while (1)
    { FILE *fd;

      sprintf(sname,"%s%0*d.tif",prefix,num_width,first_num+depth);
      if ((fd = fopen(sname,"r")) == NULL)
        break;
      fclose(fd);

      depth += 1;
    }

  sprintf(sname,"%s%0*d.tif",prefix,num_width,first_num);
  tif = Open_Tiff(sname,"r");
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

  kind  = determine_kind(tif);
  stack = new_stack(depth*height*width*kind,"Read_Stack_Planes");

  stack->width  = width;
  stack->height = height;
  stack->depth  = depth;
  stack->kind   = kind;

  { int d;

    d = 0;
    while (1)
      { read_directory(tif,Select_Plane(stack,d),"Read_Stack_Planes");
        TIFFClose(tif);

        d += 1;
        if (d >= depth) break;

        sprintf(sname,"%s%0*d.tif",prefix,num_width,first_num+d);
        tif = Open_Tiff(sname,"r");
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
        if (width != stack->width || height != stack->height)
          error("Images of stack are not of the same dimensions!",NULL);

        kind = determine_kind(tif);
        if (kind != stack->kind)
          error("Images of stack are not of the same type (GREY, GREY16, or COLOR)!",NULL);
      }
  }

  return (stack);
}
Esempio n. 3
0
Stack *Read_Stack(char *file_name)
{ Stack *stack;

  TIFF  *tif;
  int    depth, width, height, kind;

  tif = Open_Tiff(file_name,"r");
  depth = 1;
  while (TIFFReadDirectory(tif))
    depth += 1;
  TIFFClose(tif);

  tif = Open_Tiff(file_name,"r");
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

  kind  = determine_kind(tif);
  stack = new_stack(depth*height*width*kind,"Read_Stack");

  stack->width  = width;
  stack->height = height;
  stack->depth  = depth;
  stack->kind   = kind;

  { int d;

    d = 0;
    while (1)
      { read_directory(tif,Select_Plane(stack,d),"Read_Stack");

        d += 1;
        if (!TIFFReadDirectory(tif)) break;

        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
        if (width != stack->width || height != stack->height)
          error("Images of stack are not of the same dimensions!",NULL);

        kind = determine_kind(tif);
        if (kind != stack->kind)
          error("Images of stack are not of the same type (GREY, GREY16, or COLOR)!",NULL);
      }
  }

  TIFFClose(tif);

  return (stack);
}
Esempio n. 4
0
//static void read_directory_location(TIFF *tif, Image *image, int xs,int ys,int xe,int ye,char *routine)   // modify the funtion /// yang jinzhu
//{ 
//	uint32 *raster;
//	uint8  *row;
//	int    width, height;
//	
//	width  = image->width;
//	height = image->height;
//	raster = get_raster(width*height,routine);
//	
//	row = image->array;
//	
//	if (image->kind != GREY16)
//		
//    { 
//		int i, j;
//		uint32 *in;
//		uint8  *out;
//		
//		if (TIFFReadRGBAImage(tif,width,height,raster,0) == 0)
//			error("read of tif failed in read_directory()", NULL);
//		in = raster;
//		
//		if (image->kind == GREY)
//        { 
//			for (j = ye-1; j >= ye; j--)
//			{ 
//				out = row;
//				for (i = xs; i < xe; i++)
//				{
//					uint32 pixel = (*in+xs)++;
//					*out++ = TIFFGetR(pixel);
//				}
//				row += width;
//			}
//        }
//		else
//        { 
//			for (j = ye-1; j >= ye; j--)
//			{ 
//				out = row;
//				for (i = xs; i < xe; i++)
//				{ 
//					uint32 pixel = *in++;
//					*out++ = TIFFGetR(pixel);
//					*out++ = TIFFGetG(pixel);
//					*out++ = TIFFGetB(pixel);
//				}
//				row += width*3;
//			}
//		}
//    }	
//	else
//    { 
//		int tile_width, tile_height;
//		
//		if (TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tile_width))    // File is tiled  
//        { 
//			int x, y;
//			int i, j;
//			int m, n;
//			uint16 *buffer = (uint16 *) raster;
//			uint16 *out, *in, *rous;
//			
//			TIFFGetField(tif, TIFFTAG_TILELENGTH, &tile_height);
//			
//			for (y = 0; y < height; y += tile_height)
//            {
//				if (y + tile_height > height)
//                n = height - y;
//			else
//                n = tile_height;
//				for (x = 0; x < width; x += tile_width)
//                { 
//					TIFFReadTile(tif, buffer, x, y, 0, 0);
//					if (x + tile_width > width)
//						m = width - x;
//					else
//						m = tile_width;
//					for (j = 0; j < n; j++)
//                    { 
//						out = (uint16 *) (row + 2*(j*width + x));
//						in  = buffer + j*tile_width;
//						for (i = 0; i < m; i++)
//							*out++ = *in++; 
//                    }
//                }
//				row += n*width*2;
//            }
//        }
//		
//		else    // File is striped
//        { 
//			int y;
//			for (y = 0; y < height; y++)
//            { 
//				TIFFReadScanline(tif, row, y, 0);
//				row += width*2;
//            }
//        }
//    }
//}
Image *Read_Tiff(TIFF *tif, int *lastone)
{ Image *image;

  int   width, height, kind;

  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

  kind = determine_kind(tif);

  image = new_image(height*width*kind,"Read_Tiff");

  image->width  = width;
  image->height = height;
  image->kind   = kind;

  read_directory(tif,image,"Read_Tiff");

  *lastone = (! TIFFReadDirectory(tif));
  return (image);
}
Esempio n. 5
0
static Pe *
read_unmmapped_file(int fildes, size_t maxsize, Pe_Cmd cmd, Pe *parent)
{
	union {
		struct {
			struct mz_hdr mz;
			struct pe_hdr pe;
		};
		unsigned char raw[1];
	} mem;

	ssize_t nread = pread_retry (fildes, &mem.mz, sizeof(mem.mz), 0);
	if (nread == -1)
		return NULL;

	/* this handles MZ-only binaries wrong, but who cares, really? */
	off_t peaddr = le32_to_cpu(mem.mz.peaddr);
	ssize_t prev_nread = nread;
	nread += pread_retry (fildes, &mem.pe, sizeof(mem.pe), peaddr);
	if (nread == prev_nread)
		return NULL;
	mem.mz.peaddr = cpu_to_le32(offsetof(typeof(mem), pe));

	Pe_Kind kind = determine_kind(&mem, nread);

	switch (kind) {
		case PE_K_PE_OBJ:
			return file_read_pe_obj(fildes, NULL, mem.raw, maxsize,
						cmd, parent);
		case PE_K_PE_EXE:
			return file_read_pe_exe(fildes, NULL, mem.raw, maxsize,
						cmd, parent);
		default:
			break;
	}

	return allocate_pe(fildes, NULL, maxsize, cmd, parent, PE_K_NONE, 0);
}
Esempio n. 6
0
Pe *
__libpe_read_mmapped_file(int fildes, void *map_address, size_t maxsize,
			Pe_Cmd cmd, Pe *parent)
{
	unsigned char *p_ident = (unsigned char *) map_address;

	Pe_Kind kind = determine_kind (p_ident, maxsize);

	switch (kind) {
		case PE_K_PE_OBJ:
		case PE_K_PE64_OBJ:
			return file_read_pe_obj(fildes, map_address, p_ident,
						maxsize, cmd, parent);
		case PE_K_PE64_EXE:
		case PE_K_PE_EXE:
			return file_read_pe_exe(fildes, map_address, p_ident,
						maxsize, cmd, parent);
		default:
			break;
	}

	return allocate_pe(fildes, map_address, maxsize, cmd, parent,
		PE_K_NONE, 0);
}
Esempio n. 7
0
Stack *Read_Stack_location(char *file_name, int zs,int ze)//modify the funtion yangjinzhu
{ 
	Stack *stack;

	TIFF  *tif;
	
	int    depth, width, height, kind;
	
	tif = Open_Tiff(file_name,"r");
	depth = 1;
	while (TIFFReadDirectory(tif))
		depth += 1;
	TIFFClose(tif);
	
	tif = Open_Tiff(file_name,"r");
	TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
	TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
	
	kind  = determine_kind(tif);
	stack = new_stack((ze-zs)*height*width*kind,"Read_Stack");
	//stack = new_stack(depth*height*width*kind,"Read_Stack");
	
	stack->width  = width;
	stack->height = height;
	stack->depth = (ze-zs);
	//stack->depth  = depth;
	stack->kind   = kind;
	//for (int i = 0; i<zs; i++)
//	{
//		TIFFReadDirectory(tif);
//		
//	}
	TIFFSetDirectory(tif,zs);
	for( int d = 0; d < (ze-zs); d++ )
	{
		//TIFFSetDirectory(tif,d);
		
		read_directory(tif,Select_Plane(stack,(d)),"Read_Stack");
		
		//d += 1;
		if (!TIFFReadDirectory(tif)) break;
		
		TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
		TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
		if (width != stack->width || height != stack->height)
			error("Images of stack are not of the same dimensions!",NULL);
		
		kind = determine_kind(tif);
		if (kind != stack->kind)
			error("Images of stack are not of the same type (GREY, GREY16, or COLOR)!",NULL);
	}
	
	//{ int d;
//		
//		d = 0;
//		while (1)
//		{ read_directory(tif,Select_Plane(stack,d),"Read_Stack");
//			
//			d += 1;
//			if (!TIFFReadDirectory(tif)) break;
//			
//			TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
//			TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
//			if (width != stack->width || height != stack->height)
//				error("Images of stack are not of the same dimensions!",NULL);
//			
//			kind = determine_kind(tif);
//			if (kind != stack->kind)
//				error("Images of stack are not of the same type (GREY, GREY16, or COLOR)!",NULL);
//		}
//	}
	
	TIFFClose(tif);
	
	return (stack);
}
Esempio n. 8
0
static inline Pe *
file_read_pe_exe(int fildes, void *map_address, unsigned char *p_ident,
		size_t maxsize, Pe_Cmd cmd, Pe *parent)
{
	Pe_Kind kind = determine_kind(p_ident, maxsize);
	size_t scncnt = get_shnum(map_address, maxsize);
	if (scncnt == (size_t) -1l) {
		/* Could not determine the number of sections. */
		return NULL;
	}

	if (scncnt > SIZE_MAX / sizeof(Pe_Scn) + sizeof (struct section_header))
		return NULL;

	const size_t scnmax = (scncnt ?: (cmd == PE_C_RDWR || cmd == PE_C_RDWR_MMAP) ? 1: 0);
	Pe *pe = allocate_pe(fildes, map_address, maxsize, cmd, parent,
			kind, scnmax * sizeof (Pe_Scn));
	if (pe == NULL)
		return NULL;

	pe->state.pe32_obj.mzhdr = (struct mz_hdr *) ((char *)map_address);

	pe->state.pe32_obj.pehdr = (struct pe_hdr *)((char *)map_address +
		 (off_t)le32_to_cpu(pe->state.pe32_obj.mzhdr->peaddr));

	assert((unsigned int)scncnt == scncnt);
	pe->state.pe32_obj.scns.cnt = scncnt;
	pe->state.pe32_obj.scns.max = scnmax;

	pe->state.pe32_obj.scnincr = 10;

	size_t ddsize = 0;

	switch (kind) {
		case PE_K_PE_OBJ:
			pe->state.pe32_obj.shdr = (struct section_header *)
				((char *)pe->state.pe32_obj.pehdr +
				sizeof (struct pe_hdr));
			break;
		case PE_K_PE_EXE:
			pe->state.pe32_exe.opthdr = (struct pe32_opt_hdr *)
				((char *)pe->state.pe32_exe.pehdr +
				sizeof (struct pe_hdr));
			pe->state.pe32_exe.datadir = (data_directory *)
				((char *)pe->state.pe32_exe.opthdr +
				sizeof (struct pe32_opt_hdr));
			ddsize = le32_to_cpu(
					pe->state.pe32_exe.opthdr->data_dirs);
			pe->state.pe32_exe.shdr = (struct section_header *)
				((char *)pe->state.pe32_exe.datadir +
				(sizeof (data_dirent) * ddsize));
			break;
		case PE_K_PE64_OBJ:
			pe->state.pe32plus_obj.shdr = (struct section_header *)
				((char *)pe->state.pe32plus_obj.pehdr +
				sizeof (struct pe_hdr));
			break;
		case PE_K_PE64_EXE:
			pe->state.pe32plus_exe.opthdr =
				(struct pe32plus_opt_hdr *)
					((char *)pe->state.pe32plus_exe.pehdr +
					sizeof (struct pe_hdr));
			pe->state.pe32plus_exe.datadir = 
				(data_directory *)
					((char *)pe->state.pe32plus_exe.opthdr +
					sizeof (struct pe32plus_opt_hdr));
			ddsize = le32_to_cpu(
				pe->state.pe32plus_exe.opthdr->data_dirs);
			pe->state.pe32plus_exe.shdr = (struct section_header *)
				((char *)pe->state.pe32plus_exe.datadir +
				(sizeof (data_dirent) * ddsize));
			break;
		default:
			break;
	}

	for (size_t cnt = 0; cnt < scncnt; cnt++) {
		pe->state.pe.scns.data[cnt].index = cnt;
		pe->state.pe.scns.data[cnt].pe = pe;
		pe->state.pe.scns.data[cnt].shdr =
			&pe->state.pe.shdr[cnt];

		uint32_t raw_data_size =
			le32_to_cpu(pe->state.pe.shdr[cnt].raw_data_size);
		uint32_t data_addr =
			le32_to_cpu(pe->state.pe.shdr[cnt].data_addr);

		if (data_addr < maxsize &&
				maxsize - data_addr <= raw_data_size)
			pe->state.pe.scns.data[cnt].rawdata_base =
				pe->state.pe.scns.data[cnt].data_base = 
				((char *)map_address + data_addr);
		pe->state.pe.scns.data[cnt].list = &pe->state.pe.scns;
	}

	return pe;
}