[gimp-dds] / tags / release-2.0.7 / dds.c Repository:
ViewVC logotype

View of /tags/release-2.0.7/dds.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 126 - (download) (as text) (annotate)
Fri Dec 12 19:25:26 2008 UTC (11 months, 1 week ago) by cocidius
File size: 11657 byte(s)
Release 2.0.7 tag
    1 /*
    2    DDS GIMP plugin
    3 
    4    Copyright (C) 2004-2008 Shawn Kirst <skirst@insightbb.com>,
    5    with parts (C) 2003 Arne Reuter <homepage@arnereuter.de> where specified.
    6 
    7    This program is free software; you can redistribute it and/or
    8    modify it under the terms of the GNU General Public
    9    License as published by the Free Software Foundation; either
   10    version 2 of the License, or (at your option) any later version.
   11 
   12    This program is distributed in the hope that it will be useful,
   13    but WITHOUT ANY WARRANTY; without even the implied warranty of
   14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15    General Public License for more details.
   16 
   17    You should have received a copy of the GNU General Public License
   18    along with this program; see the file COPYING.  If not, write to
   19    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   20    Boston, MA 02111-1307, USA.
   21 */
   22 
   23 #include <stdio.h>
   24 #include <stdlib.h>
   25 #include <string.h>
   26 
   27 #include <gtk/gtk.h>
   28 
   29 #include <libgimp/gimp.h>
   30 #include <libgimp/gimpui.h>
   31 
   32 #include "ddsplugin.h"
   33 #include "dds.h"
   34 #include "misc.h"
   35 
   36 FILE *errFile;
   37 gchar *prog_name = "dds";
   38 gchar *filename;
   39 gint interactive_dds;
   40 
   41 static void query(void);
   42 static void run(const gchar *name, gint nparams, const GimpParam *param,
   43                 gint *nreturn_vals, GimpParam **return_vals);
   44 
   45 GimpPlugInInfo PLUG_IN_INFO =
   46 {
   47    0, 0, query, run
   48 };
   49 
   50 
   51 DDSWriteVals dds_write_vals =
   52 {
   53    DDS_COMPRESS_NONE, 0, DDS_SAVE_SELECTED_LAYER, DDS_FORMAT_DEFAULT, -1,
   54    DDS_COLOR_DEFAULT, 0, DDS_MIPMAP_DEFAULT, 0
   55 };
   56 
   57 DDSReadVals dds_read_vals =
   58 {
   59    1, 1
   60 };
   61 
   62 static GimpParamDef load_args[] =
   63 {
   64    {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
   65    {GIMP_PDB_STRING, "filename", "The name of the file to load"},
   66    {GIMP_PDB_STRING, "raw_filename", "The name entered"},
   67    {GIMP_PDB_INT32, "load_mipmaps", "Load mipmaps if present"}
   68 };
   69 static GimpParamDef load_return_vals[] =
   70 {
   71    {GIMP_PDB_IMAGE, "image", "Output image"}
   72 };
   73 
   74 static GimpParamDef save_args[] =
   75 {
   76    {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
   77    {GIMP_PDB_IMAGE, "image", "Input image"},
   78    {GIMP_PDB_DRAWABLE, "drawable", "Drawable to save"},
   79    {GIMP_PDB_STRING, "filename", "The name of the file to save the image as"},
   80    {GIMP_PDB_STRING, "raw_filename", "The name entered"},
   81    {GIMP_PDB_INT32, "compression_format", "Compression format (0 = None, 1 = BC1/DXT1, 2 = BC2/DXT3, 3 = BC3/DXT5, 4 = BC3n/DXT5n, 5 = BC4/ATI1N, 6 = BC5/ATI2N, 7 = Alpha Exponent (DXT5), 8 = YCoCg (DXT5), 9 = YCoCg scaled (DXT5))"},
   82    {GIMP_PDB_INT32, "generate_mipmaps", "Generate mipmaps"},
   83    {GIMP_PDB_INT32, "savetype", "How to save the image (0 = selected layer, 1 = cube map, 2 = volume map"},
   84    {GIMP_PDB_INT32, "format", "Custom pixel format (0 = default, 1 = R5G6B5, 2 = RGBA4, 3 = RGB5A1, 4 = RGB10A2)"},
   85    {GIMP_PDB_INT32, "transparent_index", "Index of transparent color or -1 to disable (for indexed images only)."},
   86    {GIMP_PDB_INT32, "color_type", "Color selection algorithm used in DXT compression (0 = default, 1 = distance, 2 = luminance, 3 = inset bounding box)"},
   87    {GIMP_PDB_INT32, "dither", "Work on dithered color blocks when doing color selection for DXT compression"},
   88    {GIMP_PDB_INT32, "mipmap_filter", "Filtering to use when generating mipmaps (0 = default, 1 = nearest, 2 = box, 3 = bilinear, 4 = bicubic, 5 = lanczos)"}
   89 };
   90 
   91 static GimpParamDef decode_args[] =
   92 {
   93    {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
   94    {GIMP_PDB_IMAGE, "image", "Input image"},
   95    {GIMP_PDB_DRAWABLE, "drawable", "Drawable to save"}
   96 };
   97 
   98 MAIN()
   99 
  100 static void query(void)
  101 {
  102    gimp_install_procedure(LOAD_PROC,
  103                           "Loads files in DDS image format",
  104                           "Loads files in DDS image format",
  105                           "Shawn Kirst",
  106                           "Shawn Kirst",
  107                           "2008",
  108                           "<Load>/DDS image",
  109                           0,
  110                           GIMP_PLUGIN,
  111                           G_N_ELEMENTS(load_args),
  112                           G_N_ELEMENTS(load_return_vals),
  113                           load_args, load_return_vals);
  114 
  115    gimp_register_file_handler_mime(LOAD_PROC, "image/dds");
  116    gimp_register_magic_load_handler(LOAD_PROC,
  117                                     "dds",
  118                                     "",
  119                                     "0,string,DDS");
  120 
  121    gimp_install_procedure(SAVE_PROC,
  122                           "Saves files in DDS image format",
  123                           "Saves files in DDS image format",
  124                           "Shawn Kirst",
  125                           "Shawn Kirst",
  126                           "2008",
  127                           "<Save>/DDS image",
  128                           "INDEXED, GRAY, RGB",
  129                           GIMP_PLUGIN,
  130                           G_N_ELEMENTS(save_args), 0,
  131                           save_args, 0);
  132 
  133    gimp_register_file_handler_mime(SAVE_PROC, "image/dds");
  134    gimp_register_save_handler(SAVE_PROC,
  135                               "dds",
  136                               "");
  137 
  138    gimp_install_procedure(DECODE_YCOCG_PROC,
  139                           "Converts YCoCg encoded pixels to RGB",
  140                           "Converts YCoCg encoded pixels to RGB",
  141                           "Shawn Kirst",
  142                           "Shawn Kirst",
  143                           "2008",
  144                           "<Image>/Filters/Colors/Decode YCoCg",
  145                           "RGBA",
  146                           GIMP_PLUGIN,
  147                           G_N_ELEMENTS(decode_args), 0,
  148                           decode_args, 0);
  149 
  150    gimp_install_procedure(DECODE_YCOCG_SCALED_PROC,
  151                           "Converts YCoCg (scaled) encoded pixels to RGB",
  152                           "Converts YCoCg (scaled) encoded pixels to RGB",
  153                           "Shawn Kirst",
  154                           "Shawn Kirst",
  155                           "2008",
  156                           "<Image>/Filters/Colors/Decode YCoCg (scaled)",
  157                           "RGBA",
  158                           GIMP_PLUGIN,
  159                           G_N_ELEMENTS(decode_args), 0,
  160                           decode_args, 0);
  161 
  162    gimp_install_procedure(DECODE_ALPHA_EXP_PROC,
  163                           "Converts alpha exponent encoded pixels to RGB",
  164                           "Converts alpha exponent encoded pixels to RGB",
  165                           "Shawn Kirst",
  166                           "Shawn Kirst",
  167                           "2008",
  168                           "<Image>/Filters/Colors/Decode Alpha exponent",
  169                           "RGBA",
  170                           GIMP_PLUGIN,
  171                           G_N_ELEMENTS(decode_args), 0,
  172                           decode_args, 0);
  173 
  174 }
  175 
  176 static void run(const gchar *name, gint nparams, const GimpParam *param,
  177                 gint *nreturn_vals, GimpParam **return_vals)
  178 {
  179    static GimpParam values[2];
  180    GimpRunMode run_mode;
  181    GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  182    gint32 imageID;
  183    gint32 drawableID;
  184    GimpExportReturn export = GIMP_EXPORT_CANCEL;
  185 
  186    run_mode = param[0].data.d_int32;
  187 
  188    *nreturn_vals = 1;
  189    *return_vals = values;
  190 
  191    values[0].type = GIMP_PDB_STATUS;
  192    values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  193 
  194    if(!strcmp(name, LOAD_PROC))
  195    {
  196       switch(run_mode)
  197       {
  198          case GIMP_RUN_INTERACTIVE:
  199             gimp_ui_init("dds", 0);
  200             interactive_dds = 1;
  201             gimp_get_data(LOAD_PROC, &dds_read_vals);
  202             break;
  203          case GIMP_RUN_NONINTERACTIVE:
  204             interactive_dds = 0;
  205             dds_read_vals.show_dialog = 0;
  206             dds_read_vals.mipmaps = param[3].data.d_int32;
  207             if(nparams != G_N_ELEMENTS(load_args))
  208                status = GIMP_PDB_CALLING_ERROR;
  209             break;
  210          default:
  211             break;
  212       }
  213 
  214       if(status == GIMP_PDB_SUCCESS)
  215       {
  216          status = read_dds(param[1].data.d_string, &imageID);
  217          if(status == GIMP_PDB_SUCCESS && imageID != -1)
  218          {
  219             *nreturn_vals = 2;
  220             values[1].type = GIMP_PDB_IMAGE;
  221             values[1].data.d_image = imageID;
  222             if(interactive_dds)
  223                gimp_set_data(LOAD_PROC, &dds_read_vals, sizeof(dds_read_vals));
  224          }
  225          else if(status != GIMP_PDB_CANCEL)
  226             status = GIMP_PDB_EXECUTION_ERROR;
  227       }
  228    }
  229    else if(!strcmp(name, SAVE_PROC))
  230    {
  231       imageID = param[1].data.d_int32;
  232       drawableID = param[2].data.d_int32;
  233 
  234       switch(run_mode)
  235       {
  236          case GIMP_RUN_INTERACTIVE:
  237          case GIMP_RUN_WITH_LAST_VALS:
  238             gimp_ui_init("dds", 0);
  239             export = gimp_export_image(&imageID, &drawableID, "DDS",
  240                                        (GIMP_EXPORT_CAN_HANDLE_RGB |
  241                                         GIMP_EXPORT_CAN_HANDLE_GRAY |
  242                                         GIMP_EXPORT_CAN_HANDLE_INDEXED |
  243                                         GIMP_EXPORT_CAN_HANDLE_ALPHA |
  244                                         GIMP_EXPORT_CAN_HANDLE_LAYERS));
  245             if(export == GIMP_EXPORT_CANCEL)
  246             {
  247                values[0].data.d_status = GIMP_PDB_CANCEL;
  248                return;
  249             }
  250          default:
  251             break;
  252       }
  253 
  254       switch(run_mode)
  255       {
  256          case GIMP_RUN_INTERACTIVE:
  257             gimp_get_data(SAVE_PROC, &dds_write_vals);
  258             interactive_dds = 1;
  259             break;
  260          case GIMP_RUN_NONINTERACTIVE:
  261             interactive_dds = 0;
  262             if(nparams != G_N_ELEMENTS(save_args))
  263                status = GIMP_PDB_CALLING_ERROR;
  264             else
  265             {
  266                dds_write_vals.compression = param[5].data.d_int32;
  267                dds_write_vals.mipmaps = param[6].data.d_int32;
  268                dds_write_vals.savetype = param[7].data.d_int32;
  269                dds_write_vals.format = param[8].data.d_int32;
  270                dds_write_vals.transindex = param[9].data.d_int32;
  271                dds_write_vals.color_type = param[10].data.d_int32;
  272                dds_write_vals.dither = param[11].data.d_int32;
  273                dds_write_vals.mipmap_filter = param[12].data.d_int32;
  274 
  275                if(dds_write_vals.compression < DDS_COMPRESS_NONE ||
  276                   dds_write_vals.compression >= DDS_COMPRESS_MAX)
  277                   status = GIMP_PDB_CALLING_ERROR;
  278                if(dds_write_vals.savetype < DDS_SAVE_SELECTED_LAYER ||
  279                   dds_write_vals.savetype >= DDS_SAVE_MAX)
  280                   status = GIMP_PDB_CALLING_ERROR;
  281                if(dds_write_vals.format < DDS_FORMAT_DEFAULT ||
  282                   dds_write_vals.format >= DDS_FORMAT_MAX)
  283                   status = GIMP_PDB_CALLING_ERROR;
  284                if(dds_write_vals.color_type < DDS_COLOR_DEFAULT ||
  285                   dds_write_vals.color_type >= DDS_COLOR_MAX)
  286                   status = GIMP_PDB_CALLING_ERROR;
  287                if(dds_write_vals.mipmap_filter < DDS_MIPMAP_DEFAULT ||
  288                   dds_write_vals.mipmap_filter >= DDS_MIPMAP_MAX)
  289                   status = GIMP_PDB_CALLING_ERROR;
  290             }
  291             break;
  292          case GIMP_RUN_WITH_LAST_VALS:
  293             gimp_get_data(SAVE_PROC, &dds_write_vals);
  294             interactive_dds = 0;
  295             break;
  296          default:
  297             break;
  298       }
  299 
  300       if(status == GIMP_PDB_SUCCESS)
  301       {
  302          status = write_dds(param[3].data.d_string, imageID, drawableID);
  303          if(status == GIMP_PDB_SUCCESS)
  304             gimp_set_data(SAVE_PROC, &dds_write_vals, sizeof(dds_write_vals));
  305       }
  306 
  307       if(export == GIMP_EXPORT_EXPORT)
  308          gimp_image_delete(imageID);
  309    }
  310    else if(!strcmp(name, DECODE_YCOCG_PROC))
  311    {
  312       imageID = param[1].data.d_int32;
  313       drawableID = param[2].data.d_int32;
  314 
  315       decode_ycocg_image(drawableID);
  316 
  317       status = GIMP_PDB_SUCCESS;
  318 
  319       if(run_mode != GIMP_RUN_NONINTERACTIVE)
  320          gimp_displays_flush();
  321    }
  322    else if(!strcmp(name, DECODE_YCOCG_SCALED_PROC))
  323    {
  324       imageID = param[1].data.d_int32;
  325       drawableID = param[2].data.d_int32;
  326 
  327       decode_ycocg_scaled_image(drawableID);
  328 
  329       status = GIMP_PDB_SUCCESS;
  330 
  331       if(run_mode != GIMP_RUN_NONINTERACTIVE)
  332          gimp_displays_flush();
  333    }
  334    else if(!strcmp(name, DECODE_ALPHA_EXP_PROC))
  335    {
  336       imageID = param[1].data.d_int32;
  337       drawableID = param[2].data.d_int32;
  338 
  339       decode_alpha_exp_image(drawableID);
  340 
  341       status = GIMP_PDB_SUCCESS;
  342 
  343       if(run_mode != GIMP_RUN_NONINTERACTIVE)
  344          gimp_displays_flush();
  345    }
  346    else
  347       status = GIMP_PDB_CALLING_ERROR;
  348 
  349    values[0].data.d_status = status;
  350 }
  351 

ViewVC Help
Powered by ViewVC 1.0.4