home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / bmp / bmp.c next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  8.9 KB  |  306 lines

  1. /* bmp.c                                          */
  2. /* Version 0.51                                      */
  3. /* This is a File input and output filter for the */
  4. /* Gimp. It loads and saves images in windows(TM) */
  5. /* bitmap format.                                 */
  6. /* Some Parts that deal with the interaction with */
  7. /* the Gimp are taken from the GIF plugin by      */
  8. /* Peter Mattis & Spencer Kimball and from the    */
  9. /* PCX plugin by Francisco Bustamante.            */
  10. /*                                                */
  11. /* Alexander.Schulz@stud.uni-karlsruhe.de         */
  12.  
  13. /* Changes:   28.11.1997 Noninteractive operation */
  14. /*            16.03.1998 Endian-independent!!     */
  15. /*          21.03.1998 Little Bug-fix          */
  16. /*            06.04.1998 Bugfix in Padding        */
  17. /*            11.04.1998 Arch. cleanup (-Wall)    */
  18. /*                       Parses gtkrc             */
  19. /*            14.04.1998 Another Bug in Padding   */
  20. /*            28.04.1998 RLE-Encoding rewritten   */
  21. /*            29.10.1998 Changes by Tor Lillqvist */
  22. /*                       <tml@iki.fi> to support  */
  23. /*                       16 and 32 bit images     */
  24. /*            28.11.1998 Bug in RLE-read-padding  */
  25. /*                       fixed.                   */
  26. /*            19.12.1999 Resolution support added */
  27. /*            06.05.2000 Overhaul for 16&24-bit   */
  28. /*                       plus better OS/2 code    */
  29. /*                       by njl195@zepler.org.uk  */
  30.  
  31. /* 
  32.  * The GIMP -- an image manipulation program
  33.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  34.  *
  35.  * This program is free software; you can redistribute it and/or modify
  36.  * it under the terms of the GNU General Public License as published by
  37.  * the Free Software Foundation; either version 2 of the License, or
  38.  * (at your option) any later version.
  39.  *
  40.  * This program is distributed in the hope that it will be useful,
  41.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  43.  * GNU General Public License for more details.
  44.  *
  45.  * You should have received a copy of the GNU General Public License
  46.  * along with this program; if not, write to the Free Software
  47.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  48.  * ----------------------------------------------------------------------------
  49.  */
  50.  
  51. #include "config.h"
  52.  
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. #include <gtk/gtk.h>
  58.  
  59. #include <libgimp/gimp.h>
  60. #include <libgimp/gimpui.h>
  61.  
  62. #include "bmp.h"
  63.  
  64. #include "libgimp/stdplugins-intl.h"
  65.  
  66. FILE  *errorfile;
  67. gchar *prog_name = "bmp";
  68. gchar *filename;
  69. gint   interactive_bmp;
  70.  
  71. struct Bitmap_File_Head_Struct Bitmap_File_Head;
  72. struct Bitmap_Head_Struct Bitmap_Head;
  73.  
  74. /* Declare some local functions.
  75.  */
  76. static void   query      (void);
  77. static void   run        (gchar   *name,
  78.                           gint     nparams,
  79.                           GimpParam  *param,
  80.                           gint    *nreturn_vals,
  81.                           GimpParam **return_vals);
  82.  
  83. GimpPlugInInfo PLUG_IN_INFO =
  84. {
  85.   NULL,  /* init_proc  */
  86.   NULL,  /* quit_proc  */
  87.   query, /* query_proc */
  88.   run,   /* run_proc   */
  89. };
  90.  
  91. MAIN ()
  92.  
  93. static void
  94. query (void)
  95. {
  96.   static GimpParamDef load_args[] =
  97.   {
  98.     { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
  99.     { GIMP_PDB_STRING,   "filename",     "The name of the file to load" },
  100.     { GIMP_PDB_STRING,   "raw_filename", "The name entered" },
  101.   };
  102.   static GimpParamDef load_return_vals[] =
  103.   {
  104.     { GIMP_PDB_IMAGE, "image", "Output image" },
  105.   };
  106.   static gint nload_args = sizeof (load_args) / sizeof (load_args[0]);
  107.   static gint nload_return_vals = (sizeof (load_return_vals) /
  108.                    sizeof (load_return_vals[0]));
  109.  
  110.   static GimpParamDef save_args[] =
  111.   {
  112.     { GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive" },
  113.     { GIMP_PDB_IMAGE,    "image",        "Input image" },
  114.     { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
  115.     { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
  116.     { GIMP_PDB_STRING,   "raw_filename", "The name entered" },
  117.   };
  118.   static gint nsave_args = sizeof (save_args) / sizeof (save_args[0]);
  119.   
  120.   INIT_I18N();
  121.  
  122.   gimp_install_procedure ("file_bmp_load",
  123.                           "Loads files of Windows BMP file format",
  124.                           "Loads files of Windows BMP file format",
  125.                           "Alexander Schulz",
  126.                           "Alexander Schulz",
  127.                           "1997",
  128.                           "<Load>/BMP",
  129.                           NULL,
  130.                           GIMP_PLUGIN,
  131.                           nload_args, nload_return_vals,
  132.                           load_args, load_return_vals);
  133.  
  134.   gimp_install_procedure ("file_bmp_save",
  135.                           "Saves files in Windows BMP file format",
  136.                           "Saves files in Windows BMP file format",
  137.                           "Alexander Schulz",
  138.                           "Alexander Schulz",
  139.                           "1997",
  140.                           "<Save>/BMP",
  141.                           "INDEXED, GRAY, RGB",
  142.                           GIMP_PLUGIN,
  143.                           nsave_args, 0,
  144.                           save_args, NULL);
  145.  
  146.   gimp_register_magic_load_handler ("file_bmp_load",
  147.                     "bmp",
  148.                     "",
  149.                     "0,string,BM");
  150.   gimp_register_save_handler       ("file_bmp_save",
  151.                     "bmp",
  152.                     "");
  153. }
  154.  
  155. static void
  156. run (gchar   *name,
  157.      gint     nparams,
  158.      GimpParam  *param,
  159.      gint    *nreturn_vals,
  160.      GimpParam **return_vals)
  161. {
  162.   static GimpParam values[2];
  163.   GimpRunModeType  run_mode;
  164.   GimpPDBStatusType   status = GIMP_PDB_SUCCESS;
  165.   gint32        image_ID;
  166.   gint32        drawable_ID;
  167.   GimpExportReturnType export = GIMP_EXPORT_CANCEL;
  168.   
  169.   run_mode = param[0].data.d_int32;
  170.  
  171.   *nreturn_vals = 1;
  172.   *return_vals  = values;
  173.   values[0].type          = GIMP_PDB_STATUS;
  174.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  175.  
  176.   if (strcmp (name, "file_bmp_load") == 0)
  177.     {
  178.        INIT_I18N();
  179.  
  180.        switch (run_mode)
  181.         {
  182.         case GIMP_RUN_INTERACTIVE:
  183.       interactive_bmp = TRUE;
  184.           break;
  185.  
  186.         case GIMP_RUN_NONINTERACTIVE:
  187.           /*  Make sure all the arguments are there!  */
  188.           interactive_bmp = FALSE;
  189.       if (nparams != 3)
  190.             status = GIMP_PDB_CALLING_ERROR;
  191.           break;
  192.  
  193.         default:
  194.           break;
  195.         }
  196.  
  197.        if (status == GIMP_PDB_SUCCESS)
  198.      {
  199.        image_ID = ReadBMP (param[1].data.d_string);
  200.  
  201.        if (image_ID != -1)
  202.          {
  203.            *nreturn_vals = 2;
  204.            values[1].type         = GIMP_PDB_IMAGE;
  205.            values[1].data.d_image = image_ID;
  206.          }
  207.        else
  208.          {
  209.            status = GIMP_PDB_EXECUTION_ERROR;
  210.          }
  211.      }
  212.     }
  213.   else if (strcmp (name, "file_bmp_save") == 0)
  214.     {
  215.       INIT_I18N();
  216.  
  217.       image_ID    = param[1].data.d_int32;
  218.       drawable_ID = param[2].data.d_int32;
  219.  
  220.       /*  eventually export the image */ 
  221.       switch (run_mode)
  222.     {
  223.     case GIMP_RUN_INTERACTIVE:
  224.     case GIMP_RUN_WITH_LAST_VALS:
  225.       gimp_ui_init ("bmp", FALSE);
  226.       export = gimp_export_image (&image_ID, &drawable_ID, "BMP", 
  227.                       (GIMP_EXPORT_CAN_HANDLE_RGB |
  228.                        GIMP_EXPORT_CAN_HANDLE_GRAY |
  229.                        GIMP_EXPORT_CAN_HANDLE_INDEXED));
  230.       if (export == GIMP_EXPORT_CANCEL)
  231.         {
  232.           values[0].data.d_status = GIMP_PDB_CANCEL;
  233.           return;
  234.         }
  235.       break;
  236.     default:
  237.       break;
  238.     }
  239.  
  240.       switch (run_mode)
  241.         {
  242.         case GIMP_RUN_INTERACTIVE:
  243.       interactive_bmp = TRUE;
  244.           break;
  245.  
  246.         case GIMP_RUN_NONINTERACTIVE:
  247.           /*  Make sure all the arguments are there!  */
  248.           interactive_bmp = FALSE;
  249.       if (nparams != 5)
  250.             status = GIMP_PDB_CALLING_ERROR;
  251.           break;
  252.  
  253.         case GIMP_RUN_WITH_LAST_VALS:
  254.           interactive_bmp = FALSE;
  255.           break;
  256.  
  257.         default:
  258.           break;
  259.         }
  260.  
  261.       if (status == GIMP_PDB_SUCCESS)
  262.     {
  263.       status = WriteBMP (param[3].data.d_string, image_ID, drawable_ID);
  264.     }
  265.  
  266.       if (export == GIMP_EXPORT_EXPORT)
  267.     gimp_image_delete (image_ID);
  268.     }
  269.   else
  270.     {
  271.       status = GIMP_PDB_CALLING_ERROR;
  272.     }
  273.  
  274.   values[0].data.d_status = status;
  275. }
  276.  
  277. gint32 
  278. ToL (guchar *puffer)
  279. {
  280.   return (puffer[0] | puffer[1]<<8 | puffer[2]<<16 | puffer[3]<<24);
  281. }
  282.  
  283. gint16 
  284. ToS (guchar *puffer)
  285. {
  286.   return (puffer[0] | puffer[1]<<8);
  287. }
  288.  
  289. void 
  290. FromL (gint32  wert, 
  291.        guchar *bopuffer)
  292. {
  293.   bopuffer[0] = (wert & 0x000000ff)>>0x00;
  294.   bopuffer[1] = (wert & 0x0000ff00)>>0x08;
  295.   bopuffer[2] = (wert & 0x00ff0000)>>0x10;
  296.   bopuffer[3] = (wert & 0xff000000)>>0x18;
  297. }
  298.  
  299. void  
  300. FromS (gint16  wert, 
  301.        guchar *bopuffer)
  302. {
  303.   bopuffer[0] = (wert & 0x00ff)>>0x00;
  304.   bopuffer[1] = (wert & 0xff00)>>0x08;
  305. }
  306.