Changeset 120

Show
Ignore:
Timestamp:
02/07/06 00:53:13 (3 years ago)
Author:
conrad
Message:

send from memory: if unable to write to the cache, fall back to storing the
rescaled image in memory and writing that out.
Also allow writing to stdout from the commandline, if no output file is given

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fastphoto/trunk/src/cache.c

    r116 r120  
    4444} 
    4545 
    46 static int 
    47 file_check (char * filename, time_t * mtime
     46int 
     47file_check (char * filename, time_t * mtime, off_t * size
    4848{ 
    4949    struct stat statbuf; 
     
    5959    } 
    6060 
    61     *mtime = statbuf.st_mtime; 
     61    if (mtime) *mtime = statbuf.st_mtime; 
     62    if (size) *size = statbuf.st_size; 
    6263 
    6364    return 1; 
     
    7071    int ret; 
    7172 
    72     if ((ret = file_check (cachefile, &cache_mtime)) != 1) { 
     73    if ((ret = file_check (cachefile, &cache_mtime, NULL)) != 1) { 
    7374        return ret; 
    7475    } 
    7576 
    76     if ((ret = file_check (params->infile, &orig_mtime)) != 1) { 
     77    if ((ret = file_check (params->infile, &orig_mtime, NULL)) != 1) { 
    7778        return ret; 
    7879    } 
     
    8788 
    8889    return 1; 
     90} 
     91 
     92int 
     93memory_init (fastphoto_t * params) 
     94{ 
     95    params->outfile = NULL; 
     96 
     97    if ((file_check (params->infile, NULL, &params->infile_size)) == 1) { 
     98        params->data = malloc (params->infile_size); 
     99        params->data_size = params->infile_size; 
     100    } 
     101 
     102    return 0; 
     103} 
     104 
     105int 
     106memory_send (fastphoto_t * params) 
     107{ 
     108    fwrite (params->data, 1, params->data_size, stdout); 
    89109} 
    90110 
     
    125145 
    126146            if (!mkdirs (cachefile)) { 
     147                fprintf (stderr, "fastphoto: Error creating cachefile %s\n", cachefile); 
    127148                free (cachefile); 
    128                 cachefile = "/tmp/cache.jpg"; 
    129                 params->outfile = cachefile
     149 
     150               memory_init (params)
    130151            } 
    131152        } 
  • fastphoto/trunk/src/cache.h

    r107 r120  
    44int cache_init (fastphoto_t * params, char * path_info); 
    55 
     6int memory_init (fastphoto_t * params); 
     7int memory_send (fastphoto_t * params); 
     8 
    69#endif /* __CACHE_H__ */ 
  • fastphoto/trunk/src/cgi.c

    r116 r120  
    8181  params->infile = path_translated; 
    8282  params->outfile = NULL; 
     83  params->data = NULL; 
     84  params->data_size = 0; 
    8385  params->cached = 0; 
    8486  params->x = 0; 
    8587  params->y = 0; 
    8688  params->scale = 0; 
     89  params->quality = 0; /* default */ 
     90  params->gray = 0; 
    8791 
    8892  parse_query (params, query_string); 
     
    106110    size_t n; 
    107111 
    108     fd = fopen (params->outfile, "rb"); 
    109     while ((n = fread (buf, 1, BUFSIZE, fd)) > 0) { 
    110         fwrite (buf, 1, n, stdout); 
     112    if (params->outfile) { 
     113      fd = fopen (params->outfile, "rb"); 
     114      while ((n = fread (buf, 1, BUFSIZE, fd)) > 0) { 
     115          fwrite (buf, 1, n, stdout); 
     116      } 
     117      fclose (fd); 
     118    } else if (params->data) { 
     119      fprintf (stderr, "fastphoto: Sending from memory ...\n"); 
     120      fwrite (params->data, 1, params->data_size, stdout); 
    111121    } 
    112     fclose (fd); 
    113122 
    114123    return 1; 
  • fastphoto/trunk/src/fastphoto.h

    r117 r120  
    11#ifndef __FASTPHOTO_H__ 
    22#define __FASTPHOTO_H__ 
     3 
     4#include <sys/types.h> 
    35 
    46#define FASTPHOTO_DEFAULT_X 128 
     
    1214    char * infile; 
    1315    char * outfile; 
     16 
     17    off_t infile_size; 
     18    /* If outfile == NULL, write to memory */ 
     19    unsigned char * data; 
     20    int data_size; 
     21 
    1422    int cached; 
     23    int info; 
     24 
    1525    int x; 
    1626    int y; 
    1727    int scale; 
     28    int quality; 
    1829    int gray; 
    19     int quality; 
    20     int info; 
    2130}; 
    2231 
  • fastphoto/trunk/src/main.c

    r119 r120  
    88 
    99#include "fastphoto.h" 
     10#include "cache.h" 
    1011#include "cgi.h" 
    1112#include "resize.h" 
     
    4445    int i; 
    4546 
     47    params->data = NULL; 
     48    params->data_size = 0; 
     49 
    4650    params->cached = 0; 
     51    params->info = 0; 
    4752    params->x = 0; 
    4853    params->y = 0; 
    4954    params->scale = 0; 
     55    params->quality = 0; /* default */ 
    5056    params->gray = 0; 
    51     params->quality = 0; /* default */ 
    52     params->info = 0; 
    5357 
    5458    while (1) { 
     
    126130 
    127131    params->infile = argv[optind++]; 
    128     params->outfile = argv[optind++]; 
     132 
     133    if (optind >= argc) { 
     134        memory_init (params); 
     135    } else { 
     136        params->outfile = argv[optind++]; 
     137    } 
    129138 
    130139    return 1; 
     
    161170    if (cgi) { 
    162171        cgi_send (&params); 
     172    } else if (!params.outfile) { 
     173        memory_send (&params); 
    163174    } 
     175 
     176    if (params.data) free (params.data); 
    164177   
    165178    return 0; 
  • fastphoto/trunk/src/resize.c

    r117 r120  
    2121    goto im_close; 
    2222  } 
    23  
    24   if (params->outfile == NULL) goto im_close; 
    2523 
    2624  x = params->x; 
     
    4846    epeg_quality_set (im, params->quality); 
    4947 
    50   epeg_file_output_set(im, params->outfile); 
     48  if (params->outfile) 
     49    epeg_file_output_set(im, params->outfile); 
     50  else if (params->data) 
     51    epeg_memory_output_set (im, &params->data, &params->data_size); 
     52 
    5153  epeg_encode(im); 
    5254 
     
    5456  epeg_close(im); 
    5557} 
    56