File: import.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (411 lines) | stat: -rw-r--r-- 11,941 bytes parent folder | download | duplicates (4)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (c) 1991 Stanford University
 *
 * Permission to use, copy, modify, distribute, and sell 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, and that the name of Stanford not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  Stanford makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 *
 * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * ImportCmd implementation.
 */

#define NOREF

#include <Unidraw/catalog.h>
#include <Unidraw/clipboard.h>
#include <Unidraw/editor.h>
#include <Unidraw/statevars.h>
#include <Unidraw/unidraw.h>
#include <Unidraw/viewer.h>

#include <Unidraw/Commands/align.h>
#include <Unidraw/Commands/edit.h>
#include <Unidraw/Commands/import.h>

#include <Unidraw/Components/rastercomp.h>
#include <Unidraw/Components/stencilcomp.h>

#include <Unidraw/Graphic/rasterrect.h>
#include <Unidraw/Graphic/ustencil.h>

#include <IV-look/dialogs.h>
#include <InterViews/bitmap.h>
#include <InterViews/raster.h>
#include <InterViews/session.h>
#include <InterViews/style.h>
#include <InterViews/tiff.h>
#include <InterViews/window.h>

#include <TIFF/format.h>
#include <OS/string.h>

#include <cstdio>
#include <string.h>

/*****************************************************************************/

static int hexmap[] = {
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,  0,  0,  0,  0,  0,
     0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0, 10, 11, 12, 13, 14, 15,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
};

/*****************************************************************************/

static FILE* CheckCompression(
	FILE* file, const char *filename, boolean& compressed
) {
    char cmd[256];
    
    if (!file || !fgets (cmd, 4, file)) {
        compressed = false;

    } else if (*((unsigned short *)cmd) == COMPRESS_MAGIC_NUM) {
        fclose (file);
        sprintf (cmd, "uncompress < %s", filename);
        file = popen (cmd, "r");

        if (!file) {
            return NULL;
        }
        compressed = true;

    } else {
        fclose (file);
        file = fopen(filename, "r");
        compressed = false;
    }

    return file;
}

static const char* ReadCreator (const char* filename) {
    char* buf = nil;
    FILE* file = fopen(filename, "r");
    
    if (file != nil) {
        boolean compressed;
        static char creator[CHARBUFSIZE];
        char line[CHARBUFSIZE];
        
        file = CheckCompression(file, filename, compressed);

        if (!file) {
            return NULL;
        }

        if (fgets(line, CHARBUFSIZE, file) != NULL) {

            /* Two-byte magic numbers */

            switch(*((unsigned short *)line)) {
            case TIFF1_MAGIC_NUM:
            case TIFF2_MAGIC_NUM:
                return ("TIFF");
            case SUN_MAGIC_NUM:
                return ("SUN");
            }

            /* One-byte Magic numbers */

            switch (line[0]) {
            case BM_MAGIC_NUM:
                return ("BM");
            case PBM_MAGIC_NUM:
                return ("PBM");
            case ATK_MAGIC_NUM:
                return ("ATK");
            case MP_MAGIC_NUM:
                return ("MP");
            case X11_MAGIC_NUM:
                return ("X11");
            case PCX_MAGIC_NUM:
                return ("PCX");
            case IFF_MAGIC_NUM:
                return ("IFF");
            case GIF_MAGIC_NUM:
                return ("GIF");
            case RLE_MAGIC_NUM:
                return ("RLE");
            }

        } else {
            return NULL;
        }

        do {
            if (sscanf(line, "%%%%Creator: %s", creator)) {
                buf = creator;
                break;
                
            } else if (strcmp(line, "%%EndComments\n") == 0) {
                break;
            }
        } while (fgets(line, CHARBUFSIZE, file) != NULL);

        if (compressed) {
            pclose(file);

        } else {
            fclose(file);
        }
    }
    return buf;
}

static int gethex (FILE* file) {
    int c;
    while ((c = getc(file)) == ' ' || c == '\n') { }
    return (hexmap[c] << 4) + hexmap[getc(file)];
}

/*****************************************************************************/

ClassId ImportCmd::GetClassId () { return IMPORT_CMD; }

boolean ImportCmd::IsA (ClassId id) {
    return IMPORT_CMD == id || Command::IsA(id);
}

ImportCmd::ImportCmd (ControlInfo* c, FileChooser* f) : Command(c) { Init(f); }
ImportCmd::ImportCmd (Editor* ed, FileChooser* f) : Command(ed) { Init(f); }
ImportCmd::~ImportCmd () { Resource::unref(chooser_); }
void ImportCmd::Init (FileChooser* f) {
    chooser_ = f;
    Resource::ref(chooser_);
}

Command* ImportCmd::Copy () {
    ImportCmd* copy = new ImportCmd(CopyControlInfo());
    InitCopy(copy);
    return copy;
}

void ImportCmd::Execute () {
    GraphicComp* comp = PostDialog();

    if (comp != nil) {
	PasteCmd* paste_cmd = new PasteCmd(GetEditor(), new Clipboard(comp));
	paste_cmd->Execute();
	paste_cmd->Log();
	GetEditor()->GetViewer()->Align(comp, /* Center */ 4);

	if (GetEditor()->GetViewer()->GetGrid() != nil) {
	  GravityVar* grav = (GravityVar*) GetEditor()->GetState("GravityVar");
	  if (grav != nil && grav->IsActive()) {
	    // seems we need one for each dimension, x and y
	    AlignToGridCmd* algcmd = new AlignToGridCmd(GetEditor());
	    algcmd->Execute();
	    algcmd->Log();
	    AlignToGridCmd* alg2cmd = new AlignToGridCmd(GetEditor());
	    alg2cmd->Execute();
	    alg2cmd->Log();
	  }
	}

    }
}

boolean ImportCmd::Reversible () { return false; }

GraphicComp* ImportCmd::PostDialog () {
    GraphicComp* comp = nil;
    Editor* ed = GetEditor();

    Style* style;
    boolean reset_caption = false;
    if (chooser_ == nil) {
	style = new Style(Session::instance()->style());
	style->attribute("subcaption", "Import graphic from file:");
	style->attribute("open", "Import");
	chooser_ = DialogKit::instance()->file_chooser(".", style);
	Resource::ref(chooser_);
    } else {
	style = chooser_->style();
    }
    while (chooser_->post_for(ed->GetWindow())) {
	const String* str = chooser_->selected();
	if (str != nil) {
	    NullTerminatedString ns(*str);
	    comp = Import(ns.string());
	    if (comp != nil) {
		break;
	    }
	    chooser_->bodyclear();
	    style->attribute("caption", "Import failed!");
	    reset_caption = true;
	}
    }
    if (reset_caption) {
        chooser_->bodyclear();
	style->attribute("caption", "");
    }
    return comp;
}

GraphicComp* ImportCmd::Import (const char* filename) {
    GraphicComp* comp = nil;
    const char* creator = ReadCreator(filename);

    if (creator == nil || strcmp(creator, "idraw") == 0) {
        Catalog* catalog = unidraw->GetCatalog();

        if (catalog->Valid(filename, (Component*&) comp)) {
            comp = (GraphicComp*) comp->Copy();

        } else if (catalog->Retrieve(filename, (Component*&) comp)) {
            catalog->Forget(comp);
        }

    } else {
        if (strcmp(creator, "X11") == 0) {
            comp = XBitmap_Image(filename);

        } else if (strcmp(creator, "TIFF") == 0) {
            comp = TIFF_Image(filename);

        } else if (strcmp(creator, "pgmtops") == 0) {
            comp = PGM_Image(filename);

        } else if (strcmp(creator, "ppmtops") == 0) {
            comp = PPM_Image(filename);
        }
    }
    return comp;
}

GraphicComp* ImportCmd::TIFF_Image (const char* filename) {
    GraphicComp* comp = nil;
    Raster* raster = TIFFRaster::load(filename);

    if (raster != nil) {
#ifndef NOREF
        raster->ref();
#endif
        raster->flush();
        comp = new RasterComp(new RasterRect(raster), filename);
    }

    return comp;
}

GraphicComp* ImportCmd::PGM_Image (const char* filename) {
    GraphicComp* comp = nil;
    FILE* file = fopen(filename, "r");

    if (file != nil) {
        char line[1000];
        do {
            fgets(line, 1000, file);
        } while (strcmp(line, "gsave\n") != 0);

        fgets(line, 1000, file);                    // translate
        fgets(line, 1000, file);                    // scale
        fgets(line, 1000, file);                    // sizes
        int w, h, d;
        sscanf(line, "%d %d %d", &w, &h, &d);
        fgets(line, 1000, file);                    // [ ... ]
        fgets(line, 1000, file);                    // { ... }
        fgets(line, 1000, file);                    // image

        Raster* raster = new Raster(w, h);

        for (int row = h - 1; row >= 0; --row) {
            for (int column = 0; column < w; ++column) {
                int byte = gethex(file);
                float g = float(byte) / 0xff;
                raster->poke(column, row, g, g, g, 1.0);
            }
        }
        raster->flush();
        comp = new RasterComp(new RasterRect(raster), filename);
    }
    fclose(file);
    return comp;
}

GraphicComp* ImportCmd::PPM_Image (const char* filename) {
    GraphicComp* comp = nil;
    FILE* file = fopen(filename, "r");
    boolean compressed;
    file = CheckCompression(file, filename, compressed);

    if (file != nil) {
        char line[1000];
        do {
            fgets(line, 1000, file);
        } while (strcmp(line, "gsave\n") != 0);

        fgets(line, 1000, file);                    // translate
        fgets(line, 1000, file);                    // scale
        fgets(line, 1000, file);                    // scale
        fgets(line, 1000, file);                    // sizes
        int w, h, d;
        sscanf(line, "%d %d %d", &w, &h, &d);
        fgets(line, 1000, file);                    // [ ... ]
        fgets(line, 1000, file);                    // { ... }
        fgets(line, 1000, file);                    // false 3
        fgets(line, 1000, file);                    // colorimage

        Raster* raster = new Raster(w, h);

        for (int row = h - 1; row >= 0; --row) {
            for (int column = 0; column < w; ++column) {
                int red = gethex(file);
                int green = gethex(file);
                int blue = gethex(file);
                raster->poke(
                    column, row,
                    float(red)/0xff, float(green)/0xff, float(blue)/0xff, 1.0
                );
            }
        }
        raster->flush();
        comp = new RasterComp(new RasterRect(raster), filename);
    }
    
    if (compressed) {
        pclose(file);

    } else {
        fclose(file);
    }
    return comp;
}

GraphicComp* ImportCmd::XBitmap_Image (const char* filename) {
    GraphicComp* comp = nil;
    FILE* file = fopen(filename, "r");

    if (file != nil) {
        Bitmap* bm = Bitmap::open(filename);

        if (bm != nil) {
            comp = new StencilComp(
	   	new UStencil(bm, bm, stdgraphic), filename
	    );
        }
    }
    fclose(file);
    return comp;
}