File: readWriteLXP.c

package info (click to toggle)
xpaint 2.9.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,552 kB
  • sloc: ansic: 73,017; sh: 492; yacc: 247; lex: 126; sed: 43; makefile: 10
file content (192 lines) | stat: -rw-r--r-- 5,368 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, Jean-Pierre Demailly			       | */
/* | <demailly@fourier.ujf-grenoble.fr>				       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: readWriteLXP.c,v 1.14 2005/03/20 20:15:34 demailly Exp $ */

#define MIN(a,b)       (((a) < (b)) ? (a) : (b))

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include "xpaint.h"
#include "image.h"
#include "rwTable.h"
#include "libpnmrw.h"

#if defined(sco) || defined(__CYGWIN__)
#include <time.h>
#else
#include <time.h>
#include <sys/time.h>
#endif

#include <string.h>
#include <X11/Intrinsic.h>

#ifndef TRUE
#define TRUE	1
#define FALSE	0
#endif

extern void * xmalloc(size_t n);
extern FILE * openTemp(char **np);
extern void removeTemp(void);
extern Image * ReadScriptC(char *file);
extern int WritePNGn(char *file, Image *image);
extern int writeMagic(char *file, Image *image);
extern void AddFileToGlobalList(char * file);

static char tmpdir[256];
static int width0=0, height0=0;

static char *Basename(char *file)
{
    char *ptr;
    if (!file) return NULL;
    ptr = strrchr(file, '/');
    if (!ptr) return file;
    return ptr+1;
}

char * ArchiveFile(char * file)
{
    static char path[256];
    struct stat buf;
    Image *image;
    Pixmap pix;
    Colormap cmap;
    Visual *visual;
    Screen *screen;
    GC gc;

    sprintf(path, "%s/%s", tmpdir, file);
    if (stat(path, &buf)!=0) {
        if (width0==0 || height0==0) {
	    width0 = Global.default_width;
	    height0 = Global.default_height;
	}
        screen = XtScreen(Global.toplevel);
	visual = DefaultVisualOfScreen(screen);
	cmap = XCreateColormap(XtDisplay(Global.toplevel), 
		    RootWindowOfScreen(screen), visual, AllocNone);
        pix = XCreatePixmap(Global.display, XtWindow(Global.toplevel), 
                            width0, height0, 
                            DefaultDepthOfScreen(screen));
        gc = XCreateGC(Global.display, pix, 0, 0);
        XSetForeground(Global.display, gc, WhitePixelOfScreen(screen));
        XFillRectangle(Global.display, pix, gc, 0, 0, width0, height0);
        image = PixmapToImage(Global.toplevel, pix, cmap);
        writeMagic(path, image);
        ImageDelete(image);
        XFreePixmap(Global.display, pix);
        XFreeColormap(Global.display, cmap);
        XFreeGC(Global.display, gc);
    }
    AddFileToGlobalList(path);
    return path;
}

void * LoadLayers(char ** files)
{
    static Image **image = NULL;
    int i;
    width0 = 0;
    height0 = 0;
    i = 0;
    while (files[i]) {
        image = (Image **)realloc(image, (i+1)*sizeof(Image *));
        image[i] = ImageFromFile(ArchiveFile(files[i]));
        if (width0==0 && image[i]) {
	    width0 = image[i]->width;
	    height0 = image[i]->height;
	}
        ++i;
    }
    return (void *)image;
}

/* Test LXP format */
/* LXP is just a plain X.tar.gz archive containing a single directory ./
 * and a C file image.c which may combine any number of other files
 * (images, other .c files of .h headers)
 */
int
TestLXP(char *file)
{
    char header[12];
    FILE *fp = fopen(file, "rb");

    if (!fp)
        return 0;

    fread(header, 1, 8, fp);
    fclose(fp);
    if (!strncmp(header, "\037\213", 2))
        return 1;
    else
        return 0;
}

/* read LXP format */
Image *
ReadLXP(char *file)
{
    Image *image;
    char buf[2048];
    char *home;

    if (!TestLXP(file)) return NULL;

    home = getenv("HOME");
    if (!home) return NULL;
    
    sprintf(tmpdir, "%s/.xpaint/tmp/%s_files", home, Basename(file));
    sprintf(buf, "mkdir -p %s ; ln -s -f %s %s ; cd %s ; tar xvfz %s",
            tmpdir, file, tmpdir, tmpdir, file);
    system(buf);
    sprintf(buf, "%s/image.c", tmpdir);
    AddFileToGlobalList(buf);

    image = ReadScriptC(buf);
    return image;
}

int WriteLXP(char *file, Image * image)
{
    char buf[8192];
    char *home;

    home = getenv("HOME");
    if (!home) return 0;
    
    sprintf(tmpdir, "%s/.xpaint/tmp/%s_dir", home, Basename(file));
    sprintf(buf, "mkdir -p \"%s\"", tmpdir);
    system(buf); 
    sprintf(buf, "%s/file.png", tmpdir);
    WritePNGn(buf, image);
    sprintf(buf, "cp -p -f %s/XPaintIcon.xpm \"%s/XPaintIcon.xpm\"", 
            SHAREDIR, tmpdir);
    system(buf);
    sprintf(buf, "cp -p -f %s/c_scripts/templates/image.c \"%s/image.c\" ; ( cd \"%s\" ; tar cvfz ../\"%s\" . ) ; mv -f \"%s/../%s\" \"%s\" ; rm -rf \"%s\"", 
            SHAREDIR, tmpdir, tmpdir, Basename(file), tmpdir, 
            Basename(file), file, tmpdir);
    system(buf);
    return 0;
}