Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
Raster* TIFFRasterImpl::load(const char* filename) {
    tif_ = TIFFOpen(filename, "r");
    if (tif_ == nil) {
	return nil;
    }
    if (!TIFFGetField(tif_, TIFFTAG_BITSPERSAMPLE, &bitspersample_)) {
	bitspersample_ = 1;
    }
    switch (bitspersample_) {
    case 1: case 2: case 4:
    case 8: case 16:
	break;
    default:
	TIFFClose(tif_);
	return nil;
    }
    if (!TIFFGetField(tif_, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel_)) {
	samplesperpixel_ = 1;
    }
    switch (samplesperpixel_) {
    case 1: case 3: case 4:
	break;
    default:
	TIFFClose(tif_);
	return nil;
    }
    u_long width;
    TIFFGetField(tif_, TIFFTAG_IMAGEWIDTH, &width);
    u_long height;
    TIFFGetField(tif_, TIFFTAG_IMAGELENGTH, &height);
    if (!TIFFGetField(tif_, TIFFTAG_PHOTOMETRIC, &photometric_)) {
	switch (samplesperpixel_) {
	case 1:
	    photometric_ = PHOTOMETRIC_MINISBLACK;
	    break;
	case 3: case 4:
	    photometric_ = PHOTOMETRIC_RGB;
	    break;
	default:
	    TIFFClose(tif_);
	    return nil;
	}
    }
    Raster* r = nil;
    raster_ = new u_long[width * height];
    BWmap_ = nil;
    PALmap_ = nil;
    if (raster_ != nil && gt(width, height)) {
	/* create raster_ from packed image data */
	r = new Raster(width, height);
	for (long i = height - 1; i >= 0; i--) {
	    u_char* c = (u_char*) (raster_ + i*width);
	    /* should use a lookup table here */
	    for (long j = 0; j < width; j++) {
		r->poke(
		    j, i,
		    ColorIntensity(float(c[3]) / float(0xff)),
		    ColorIntensity(float(c[2]) / float(0xff)),
		    ColorIntensity(float(c[1]) / float(0xff)),
		    1.0
		);
		c += sizeof (u_long);
	    }
	}
    }
    TIFFClose(tif_);
    delete raster_;
    delete BWmap_;
    delete PALmap_;
    return r;
}