home *** CD-ROM | disk | FTP | other *** search
/ hobbes.nmsu.edu 2008 / 2008-06-02_hobbes.nmsu.edu.zip / new / scummc-0.2.0-os2.zip / ScummC / src / imgsplit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2008-02-03  |  2.8 KB  |  105 lines

  1. /* ScummC
  2.  * Copyright (C) 2005-2006  Alban Bedel
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  17.  *
  18.  */
  19.  
  20. /**
  21.  * @file imgsplit.c
  22.  * @brief Tool to split an image
  23.  */
  24.  
  25. #include "config.h"
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <inttypes.h>
  31. #include <errno.h>
  32.  
  33.  
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #include "scc_fd.h"
  38. #include "scc_util.h"
  39.  
  40. #include "scc_param.h"
  41. //#include "scc.h"
  42. #include "scc_img.h"
  43.  
  44. #include "imgsplit_help.h"
  45.  
  46. static int dw = 0, dh = 0;
  47. static char* outbase = NULL;
  48. static char* outfmt = NULL;
  49.  
  50. static scc_param_t scc_parse_params[] = {
  51.   { "outbase", SCC_PARAM_STR, 0, 0, &outbase },
  52.   { "outfmt", SCC_PARAM_STR, 0, 0, &outfmt },
  53.   { "dw", SCC_PARAM_INT, 1, 100000, &dw },
  54.   { "dh", SCC_PARAM_INT, 1, 100000, &dh },
  55.   { "help", SCC_PARAM_HELP, 0, 0, &imgsplit_help },
  56.   { NULL, 0, 0, 0, NULL }
  57. };
  58.  
  59. int main(int argc,char** argv) {
  60.   scc_cl_arg_t* files,*f;
  61.   int num = 0,x,y,l;
  62.   char outname[1024];
  63.   scc_img_t *in,*out;
  64.   
  65.   files = scc_param_parse_argv(scc_parse_params,argc-1,&argv[1]);
  66.  
  67.   if(!files || !dw || !dh) scc_print_help(&imgsplit_help,1);
  68.  
  69.   if(!outbase) outbase = "frame";
  70.   if(!outfmt) outfmt = "%s-%02d.bmp";
  71.  
  72.   for(f = files ; f ; f = f->next) {
  73.     in = scc_img_open(f->val);
  74.     if(!in) {
  75.       printf("Failed to open %s.\n",f->val);
  76.       return 1;
  77.     }
  78.     if(in->w < dw || in->h < dh) {
  79.       printf("Input image %s is too small.\n",f->val);
  80.       return 1;
  81.     }
  82.     if(in->w % dw)
  83.       printf("Warning: input image %s width is not a multiple of destination height.\n",
  84.              f->val);
  85.     if(in->h % dh)
  86.       printf("Warning: input image %s height is not a multiple of destination height.\n",
  87.              f->val);
  88.     for(y = 0 ; y < in->h ; y += dh) {
  89.       for(x = 0 ; x < in->w ; x += dw) {
  90.         out = scc_img_new(dw,dh,in->ncol);
  91.         memcpy(out->pal,in->pal,3*in->ncol);
  92.         for(l = 0 ; l < dh ; l++)
  93.           memcpy(&out->data[l*dw],&in->data[(y+l)*in->w+x],dw);
  94.  
  95.         snprintf(outname,1023,outfmt,outbase,num);
  96.         outname[1023] = '\0';
  97.  
  98.         if(!scc_img_save_bmp(out,outname)) return 1;
  99.         num++;
  100.       }
  101.     }
  102.   }
  103.   return 0;
  104. }
  105.