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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (download) (as text) (annotate)
Fri Apr 11 18:08:24 2008 UTC (19 months, 1 week ago) by cocidius
File size: 8431 byte(s)
Release 2.0.3 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 
   35 FILE *errFile;
   36 gchar *prog_name = "dds";
   37 gchar *filename;
   38 gint interactive_dds;
   39 
   40 static void query(void);
   41 static void run(const gchar *name, gint nparams, const GimpParam *param,
   42                 gint *nreturn_vals, GimpParam **return_vals);
   43 
   44 GimpPlugInInfo PLUG_IN_INFO =
   45 {
   46    0, 0, query, run
   47 };
   48 
   49 
   50 DDSWriteVals dds_write_vals =
   51 {
   52    DDS_COMPRESS_NONE, 0, DDS_SAVE_SELECTED_LAYER, DDS_FORMAT_DEFAULT, -1,
   53    DDS_COLOR_DEFAULT, 0, 0
   54 };
   55 
   56 DDSReadVals dds_read_vals =
   57 {
   58    1, 1
   59 };
   60 
   61 static GimpParamDef load_args[] =
   62 {
   63    {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
   64    {GIMP_PDB_STRING, "filename", "The name of the file to load"},
   65    {GIMP_PDB_STRING, "raw_filename", "The name entered"},
   66    {GIMP_PDB_INT32, "load_mipmaps", "Load mipmaps if present"}
   67 };
   68 static GimpParamDef load_return_vals[] =
   69 {
   70    {GIMP_PDB_IMAGE, "image", "Output image"}
   71 };
   72 
   73 static GimpParamDef save_args[] =
   74 {
   75    {GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive"},
   76    {GIMP_PDB_IMAGE, "image", "Input image"},
   77    {GIMP_PDB_DRAWABLE, "drawable", "Drawable to save"},
   78    {GIMP_PDB_STRING, "filename", "The name of the file to save the image as"},
   79    {GIMP_PDB_STRING, "raw_filename", "The name entered"},
   80    {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))"},
   81    {GIMP_PDB_INT32, "generate_mipmaps", "Generate mipmaps"},
   82    {GIMP_PDB_INT32, "savetype", "How to save the image (0 = selected layer, 1 = cube map, 2 = volume map"},
   83    {GIMP_PDB_INT32, "format", "Custom pixel format (0 = default, 1 = R5G6B5, 2 = RGBA4, 3 = RGB5A1, 4 = RGB10A2)"},
   84    {GIMP_PDB_INT32, "transparent_index", "Index of transparent color or -1 to disable (for indexed images only)."},
   85    {GIMP_PDB_INT32, "color_type", "Color selection algorithm used in DXT compression (0 = default, 1 = distance, 2 = luminance, 3 = inset bounding box)"},
   86    {GIMP_PDB_INT32, "dither", "Work on dithered color blocks when doing color selection for DXT compression"}
   87 };
   88 
   89 MAIN()
   90 
   91 static void query(void)
   92 {
   93    gimp_install_procedure(LOAD_PROC,
   94                           "Loads files in DDS image format",
   95                           "Loads files in DDS image format",
   96                           "Shawn Kirst",
   97                           "Shawn Kirst",
   98                           "2004",
   99                           "<Load>/DDS image",
  100                           0,
  101                           GIMP_PLUGIN,
  102                           G_N_ELEMENTS(load_args),
  103                           G_N_ELEMENTS(load_return_vals),
  104                           load_args, load_return_vals);
  105 
  106    gimp_register_file_handler_mime(LOAD_PROC, "image/dds");
  107    gimp_register_magic_load_handler(LOAD_PROC,
  108                                     "dds",
  109                                     "",
  110                                     "0,string,DDS");
  111 
  112    gimp_install_procedure(SAVE_PROC,
  113                           "Saves files in DDS image format",
  114                           "Saves files in DDS image format",
  115                           "Shawn Kirst",
  116                           "Shawn Kirst",
  117                           "2004",
  118                           "<Save>/DDS image",
  119                           "INDEXED, GRAY, RGB",
  120                           GIMP_PLUGIN,
  121                           G_N_ELEMENTS(save_args), 0,
  122                           save_args, 0);
  123 
  124    gimp_register_file_handler_mime(SAVE_PROC, "image/dds");
  125    gimp_register_save_handler(SAVE_PROC,
  126                               "dds",
  127                               "");
  128 }
  129 
  130 static void run(const gchar *name, gint nparams, const GimpParam *param,
  131                 gint *nreturn_vals, GimpParam **return_vals)
  132 {
  133    static GimpParam values[2];
  134    GimpRunMode run_mode;
  135    GimpPDBStatusType status = GIMP_PDB_SUCCESS;
  136    gint32 imageID;
  137    gint32 drawableID;
  138    GimpExportReturn export = GIMP_EXPORT_CANCEL;
  139 
  140    run_mode = param[0].data.d_int32;
  141 
  142    *nreturn_vals = 1;
  143    *return_vals = values;
  144 
  145    values[0].type = GIMP_PDB_STATUS;
  146    values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  147 
  148    if(!strcmp(name, LOAD_PROC))
  149    {
  150       switch(run_mode)
  151       {
  152          case GIMP_RUN_INTERACTIVE:
  153             gimp_ui_init("dds", 0);
  154             interactive_dds = 1;
  155             gimp_get_data(LOAD_PROC, &dds_read_vals);
  156             break;
  157          case GIMP_RUN_NONINTERACTIVE:
  158             interactive_dds = 0;
  159             dds_read_vals.show_dialog = 0;
  160             dds_read_vals.mipmaps = param[3].data.d_int32;
  161             if(nparams != G_N_ELEMENTS(load_args))
  162                status = GIMP_PDB_CALLING_ERROR;
  163             break;
  164          default:
  165             break;
  166       }
  167 
  168       if(status == GIMP_PDB_SUCCESS)
  169       {
  170          status = read_dds(param[1].data.d_string, &imageID);
  171          if(status == GIMP_PDB_SUCCESS && imageID != -1)
  172          {
  173             *nreturn_vals = 2;
  174             values[1].type = GIMP_PDB_IMAGE;
  175             values[1].data.d_image = imageID;
  176             if(interactive_dds)
  177                gimp_set_data(LOAD_PROC, &dds_read_vals, sizeof(dds_read_vals));
  178          }
  179          else if(status != GIMP_PDB_CANCEL)
  180             status = GIMP_PDB_EXECUTION_ERROR;
  181       }
  182    }
  183    else if(!strcmp(name, SAVE_PROC))
  184    {
  185       imageID = param[1].data.d_int32;
  186       drawableID = param[2].data.d_int32;
  187 
  188       switch(run_mode)
  189       {
  190          case GIMP_RUN_INTERACTIVE:
  191          case GIMP_RUN_WITH_LAST_VALS:
  192             gimp_ui_init("dds", 0);
  193             export = gimp_export_image(&imageID, &drawableID, "DDS",
  194                                        (GIMP_EXPORT_CAN_HANDLE_RGB |
  195                                         GIMP_EXPORT_CAN_HANDLE_GRAY |
  196                                         GIMP_EXPORT_CAN_HANDLE_INDEXED |
  197                                         GIMP_EXPORT_CAN_HANDLE_ALPHA |
  198                                         GIMP_EXPORT_CAN_HANDLE_LAYERS));
  199             if(export == GIMP_EXPORT_CANCEL)
  200             {
  201                values[0].data.d_status = GIMP_PDB_CANCEL;
  202                return;
  203             }
  204          default:
  205             break;
  206       }
  207 
  208       switch(run_mode)
  209       {
  210          case GIMP_RUN_INTERACTIVE:
  211             gimp_get_data(SAVE_PROC, &dds_write_vals);
  212             interactive_dds = 1;
  213             break;
  214          case GIMP_RUN_NONINTERACTIVE:
  215             interactive_dds = 0;
  216             if(nparams != G_N_ELEMENTS(save_args))
  217                status = GIMP_PDB_CALLING_ERROR;
  218             else
  219             {
  220                dds_write_vals.compression = param[5].data.d_int32;
  221                dds_write_vals.mipmaps = param[6].data.d_int32;
  222                dds_write_vals.savetype = param[7].data.d_int32;
  223                dds_write_vals.format = param[8].data.d_int32;
  224                dds_write_vals.transindex = param[9].data.d_int32;
  225                dds_write_vals.color_type = param[10].data.d_int32;
  226                dds_write_vals.dither = param[11].data.d_int32;
  227 
  228                if(dds_write_vals.compression < DDS_COMPRESS_NONE ||
  229                   dds_write_vals.compression >= DDS_COMPRESS_MAX)
  230                   status = GIMP_PDB_CALLING_ERROR;
  231                if(dds_write_vals.savetype < DDS_SAVE_SELECTED_LAYER ||
  232                   dds_write_vals.savetype >= DDS_SAVE_MAX)
  233                   status = GIMP_PDB_CALLING_ERROR;
  234                if(dds_write_vals.format < DDS_FORMAT_DEFAULT ||
  235                   dds_write_vals.format >= DDS_FORMAT_MAX)
  236                   status = GIMP_PDB_CALLING_ERROR;
  237                if(dds_write_vals.color_type < DDS_COLOR_DEFAULT ||
  238                   dds_write_vals.color_type >= DDS_COLOR_MAX)
  239                   status = GIMP_PDB_CALLING_ERROR;
  240             }
  241             break;
  242          case GIMP_RUN_WITH_LAST_VALS:
  243             gimp_get_data(SAVE_PROC, &dds_write_vals);
  244             interactive_dds = 0;
  245             break;
  246          default:
  247             break;
  248       }
  249 
  250       if(status == GIMP_PDB_SUCCESS)
  251       {
  252          status = write_dds(param[3].data.d_string, imageID, drawableID);
  253          if(status == GIMP_PDB_SUCCESS)
  254             gimp_set_data(SAVE_PROC, &dds_write_vals, sizeof(dds_write_vals));
  255       }
  256 
  257       if(export == GIMP_EXPORT_EXPORT)
  258          gimp_image_delete(imageID);
  259    }
  260    else
  261       status = GIMP_PDB_CALLING_ERROR;
  262 
  263    values[0].data.d_status = status;
  264 }
  265 

ViewVC Help
Powered by ViewVC 1.0.4