filters

xcf-read.cc

00001 /* The GIMP -- an image manipulation program
00002  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "config.h"
00020 
00021 #include <stdio.h>
00022 
00023 #include <glib-object.h>
00024 
00025 #include "libgimpbase/gimpbase.h"
00026 
00027 #include "xcf-read.h"
00028 
00029 #include "gimp-intl.h"
00030 
00031 
00032 Q_UINT32
00033 xcf_read_int32 (FILE    *fp,
00034         Q_INT32 *data,
00035         Q_INT32     count)
00036 {
00037   Q_UINT32 total;
00038 
00039   total = count;
00040   if (count > 0)
00041     {
00042       xcf_read_int8 (fp, (Q_UINT8 *) data, count * 4);
00043 
00044       while (count--)
00045         {
00046           *data = g_ntohl (*data);
00047           data++;
00048         }
00049     }
00050 
00051   return total * 4;
00052 }
00053 
00054 Q_UINT32
00055 xcf_read_float (FILE   *fp,
00056         float *data,
00057         Q_INT32    count)
00058 {
00059   return xcf_read_int32 (fp, (Q_INT32 *) ((void *) data), count);
00060 }
00061 
00062 Q_UINT32
00063 xcf_read_int8 (FILE   *fp,
00064            Q_UINT8 *data,
00065            Q_INT32    count)
00066 {
00067   Q_UINT32 total;
00068   Q_INT32  bytes;
00069 
00070   total = count;
00071   while (count > 0)
00072     {
00073       bytes = fread ((char *) data, sizeof (char), count, fp);
00074       if (bytes <= 0) /* something bad happened */
00075         break;
00076       count -= bytes;
00077       data += bytes;
00078     }
00079 
00080   return total;
00081 }
00082 
00083 Q_UINT32
00084 xcf_read_string (FILE   *fp,
00085          QCString **data,
00086          Q_INT32    count)
00087 {
00088   Q_INT32 tmp;
00089   Q_UINT32   total;
00090   Q_INT32    i;
00091 
00092   total = 0;
00093   for (i = 0; i < count; i++)
00094     {
00095       total += xcf_read_int32 (fp, &tmp, 1);
00096       if (tmp > 0)
00097         {
00098           QCString *str;
00099 
00100           str = g_new (QCString, tmp);
00101           total += xcf_read_int8 (fp, (Q_UINT8*) str, tmp);
00102 
00103           if (str[tmp - 1] != '\0')
00104             str[tmp - 1] = '\0';
00105 
00106           data[i] = gimp_any_to_utf8 (str, -1,
00107                                       _("Invalid UTF-8 string in XCF file"));
00108 
00109           g_free (str);
00110         }
00111       else
00112         {
00113           data[i] = NULL;
00114         }
00115     }
00116 
00117   return total;
00118 }
KDE Home | KDE Accessibility Home | Description of Access Keys