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 / palcat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2008-02-03  |  2.8 KB  |  114 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 palcat.c
  22.  * @brief Tool to concatenate image palette
  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_img.h"
  42.  
  43. #include "palcat_help.h"
  44.  
  45. static char* outname = NULL;
  46. static char* inname = NULL;
  47.  
  48.  
  49. static scc_param_t scc_parse_params[] = {
  50.   { "o", SCC_PARAM_STR, 0, 0, &outname },
  51.   { "i", SCC_PARAM_STR, 0, 0, &inname },
  52.   { "help", SCC_PARAM_HELP, 0, 0, &palcat_help },
  53.   { NULL, 0, 0, 0, NULL }
  54. };
  55.  
  56. int main(int argc,char** argv) {
  57.   scc_cl_arg_t* files,*f;
  58.   scc_img_t *out = NULL,*in;
  59.   unsigned pre_off = 0;
  60.   
  61.   files = scc_param_parse_argv(scc_parse_params,argc-1,&argv[1]);
  62.  
  63.   if(!files || !outname) scc_print_help(&palcat_help,1);
  64.  
  65.   if (inname) {
  66.     out = scc_img_open(inname);
  67.     if (!out)
  68.       inname = NULL;
  69.   }
  70.  
  71.   for(f = files ; f ; f = f->next) {
  72.     if (!inname || strcmp(f->val, inname)) {
  73.       in = scc_img_open(f->val);
  74.     } else {
  75.       // Found input filename, now switch to append mode
  76.       inname = NULL;
  77.       continue;
  78.     }
  79.  
  80.     if(!in) {
  81.       scc_log(LOG_ERR,"Failed to open image '%s'.\n",f->val);
  82.       return 1;
  83.     }
  84.  
  85.     if(!out) {
  86.       // Assign output to first file specified
  87.       out = in;
  88.       continue;
  89.     }
  90.  
  91.     // Realloc the palette
  92.     out->pal = realloc(out->pal,(out->ncol+in->ncol)*3);
  93.  
  94.     // Pre-append the palette
  95.     if (inname) {
  96.       int i;
  97.       memmove(out->pal+pre_off+3*in->ncol,out->pal+pre_off,out->ncol*3-pre_off);
  98.       memcpy(out->pal+pre_off,in->pal,in->ncol*3);
  99.       pre_off += in->ncol*3;
  100.       // Offset bitmap data in image by input colors (assuming data is 8bit)
  101.       for (i = 0 ; i < out->w*out->h ; i++)
  102.           out->data[i] += in->ncol;
  103.     } else  // Append the palette
  104.       memcpy(out->pal+3*out->ncol,in->pal,in->ncol*3);
  105.     
  106.     out->ncol += in->ncol;
  107.     
  108.     scc_img_free(in);
  109.   }
  110.   if(!scc_img_save_bmp(out,outname)) return 1;
  111.   
  112.   return 0;
  113. }
  114.