Facial Recognition
UsePCAtoperformfacialrecognition
 All Classes Files Functions Variables Typedefs Macros
Functions
tiff_util.c File Reference

File containing common libtiff wrapper functions. More...

#include "tiff_util.h"
#include <math.h>
Include dependency graph for tiff_util.c:

Functions

vectortiff_to_vec (char *filename)
 Converts tiff to vector this function will take a TIFF filename and return a vector* with every element corresponding to pixel. More...
 
TIFF * vec_to_tiff (char *filename, vector *vec, size_t width, size_t height)
 Converts vector to tiff this function will take a vector and return a TIFF* with every element corresponding to pixel. More...
 
FILE * get_all_tiff (char *path, int *num_files)
 Recursively earches the path for all files of .tiff type the search relies on being able to run the commands find and ls. More...
 
vectortiff_stream_to_vec (FILE *stream)
 Converts FILE* tiff stream into a vector the images are appended row-wise. More...
 

Detailed Description

File containing common libtiff wrapper functions.

Minhyuk Park

Date
9 Nov 2017

Function Documentation

FILE* get_all_tiff ( char *  path,
int *  num_files 
)

Recursively earches the path for all files of .tiff type the search relies on being able to run the commands find and ls.

Returns
FILE* the stream containing all the files separated by new line characters
Parameters
pathchar* the path containing the files
num_filesint* outputs the number of files read
77  {
78  char* command = malloc(strlen(path) + 100);
79  sprintf(command, "%s%s%s", "find ", path, "*.tiff -type f -exec ls {} \\;");
80 
81 
82  printf("%s\n", command);
83  FILE* retval = popen(command, "r");
84 
85  sprintf(command, "%s%s%s", "find ", path, "*.tiff -type f -exec ls {} \\; | wc -l");
86 
87  printf("%s\n", command);
88  FILE* num_files_fp = popen(command, "r");
89  if(fgets(command, sizeof(command), num_files_fp) != NULL) {
90  *num_files = atoi(command);
91  }
92  pclose(num_files_fp);
93  free(command);
94  return retval;
95 }
vector* tiff_stream_to_vec ( FILE *  stream)

Converts FILE* tiff stream into a vector the images are appended row-wise.

Returns
vector* newly malloced vector* containing all the images from the stream
Parameters
streamFILE* returned from get_all_tiff()

References tiff_to_vec(), and vec_append().

98  {
99  char* image_filename;
100  char buffer[4096];
101 
102  vector* cumulative_image = NULL;
103  while(fgets(buffer, sizeof(buffer), stream) != NULL) {
104  image_filename = malloc(100);
105  strcpy(image_filename, buffer);
106  image_filename[strlen(image_filename) - 1] = '\0';
107  if(cumulative_image == NULL) {
108  cumulative_image = tiff_to_vec(image_filename);
109  } else {
110  vec_append(&cumulative_image, tiff_to_vec(image_filename));
111  }
112  free(image_filename);
113  }
114  return cumulative_image;
115 }
vector * tiff_to_vec(char *filename)
Converts tiff to vector this function will take a TIFF filename and return a vector* with every eleme...
Definition: tiff_util.c:11
void vec_append(vector **vec_a, vector *vec_b)
Appends vector b to vector a this function will realloc for the user vector a and free vector b...
Definition: linalg.c:192
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
vector* tiff_to_vec ( char *  filename)

Converts tiff to vector this function will take a TIFF filename and return a vector* with every element corresponding to pixel.

Returns
vector* newly malloced representing the image
Parameters
filenamechar* the input filename

References _vector::padding, VEC, and vector_create().

Referenced by tiff_stream_to_vec().

11  {
12  TIFF* image = TIFFOpen(filename, "r");
13  uint32 width;
14  uint32 height;
15  TIFFGetField(image, TIFFTAG_IMAGEWIDTH, &width);
16  TIFFGetField(image, TIFFTAG_IMAGELENGTH, &height);
17 
18  uint32 num_pixels = width * height;
19  uint32 * raster=(uint32*) _TIFFmalloc(num_pixels * sizeof(uint32));
20  vector* retvec = vector_create(num_pixels);
21 
22  TIFFReadRGBAImage(image, width, height, raster, 0);
23 
24  for(size_t i = 0; i < num_pixels; i ++) {
25  VEC(retvec, i) = *(raster + i);
26  }
27  _TIFFfree(raster);
28 
29  retvec->padding = width;
30  TIFFClose(image);
31  return retvec;
32 }
vector * vector_create(size_t size)
Creates a vector this function will malloc the exact space for the required dimensions.
Definition: linalg.c:11
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t padding
unused value but important as padding
Definition: linalg.h:31
represents a vector padding is to make sure that matrix and vector both have the same byte size and a...
Definition: linalg.h:27
TIFF* vec_to_tiff ( char *  filename,
vector vec,
size_t  width,
size_t  height 
)

Converts vector to tiff this function will take a vector and return a TIFF* with every element corresponding to pixel.

Returns
TIFF* opened tiff file stream with necessary data written to from the vector
Parameters
filenamechar* the output filename
vecvector* vector representing the image
widthsize_t width of the image
heightsize_t height of the image

References _vector::size, and VEC.

35  {
36  TIFF* rettiff = TIFFOpen(filename, "w");
37 
38  char* image_char = malloc(vec->size * 4); // 4 since RGBA
39 
40  for(size_t i = 0; i < vec->size; i ++) {
41  image_char[4 * i] = (char)TIFFGetR((uint32)VEC(vec, i));
42  image_char[4 * i + 1] = (char)TIFFGetG((uint32)VEC(vec, i));
43  image_char[4 * i + 2] = (char)TIFFGetB((uint32)VEC(vec, i));
44  image_char[4 * i + 3] = (char)TIFFGetA((uint32)VEC(vec, i));
45  }
46  TIFFSetField (rettiff, TIFFTAG_IMAGEWIDTH, width);
47  TIFFSetField(rettiff, TIFFTAG_IMAGELENGTH, height);
48  TIFFSetField(rettiff, TIFFTAG_SAMPLESPERPIXEL, 4);
49  TIFFSetField(rettiff, TIFFTAG_BITSPERSAMPLE, 8);
50  TIFFSetField(rettiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
51  TIFFSetField(rettiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
52  TIFFSetField(rettiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
53 
54  tsize_t linebytes = 4 * width;
55  unsigned char* buf = NULL;
56 
57  if (TIFFScanlineSize(rettiff) == linebytes) {
58  buf =(unsigned char*)_TIFFmalloc(linebytes);
59  } else {
60  buf = (unsigned char*)_TIFFmalloc(TIFFScanlineSize(rettiff));
61  }
62 
63  TIFFSetField(rettiff, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(rettiff, width * 4));
64 
65  for (uint32 i = 0; i < height; i++) {
66  memcpy(buf, &image_char[(height - i - 1) * linebytes], linebytes);
67  if (TIFFWriteScanline(rettiff, buf, i, 0) < 0) {
68  break;
69  }
70  }
71  _TIFFfree(buf);
72  free(image_char);
73  return rettiff;
74 }
#define VEC(v, x)
macro for element access in a vector struct
Definition: linalg.h:19
size_t size
the number of elements in the vector
Definition: linalg.h:29