示例#1
0
void process_file(const char *fname) {
	FILE *in;
	int codec, num_images, format;
	int i;
	int width, height, size, maxval;
	unsigned char *data;
	pixel **img;

	in = fopen(fname, "rb");
	if (in == NULL) {
		perror(fname);
		exit(1);
	}
	read_header(in, &codec, &num_images, &format);

	for (i = 0; i < num_images; i++) {
		width = read_LEint32(in);
		height = read_LEint32(in);
		data = malloc(width * height * 2);

		if (codec == 0)
			read_data_codec0(in, width, height, data);
		else if (codec == 3) {
			size = read_LEint32(in);
			read_data_codec3(in, size, data);
		} else {
			fprintf(stderr, "%s: unsupported codec %d\n", fname, codec);
			exit(1);
		}

		if (format == 1) {
			img = toimg_fmt1(data, width, height);
			maxval = 255;
		} else if (format == 5) {
			img = toimg_fmt5(data, width, height);
			maxval = 255;
		} else {
			fprintf(stderr, "%s: unsupported format %d\n", fname, format);
			exit(1);
		}

		write_img(img, fname, i, width, height, maxval);
		free(data);
		ppm_freearray(img, height);
	}
	fclose(in);
}
示例#2
0
void write_img(FILE *f, const char *fname, int n, int num_img) {
	FILE *out;
	int32_t width, height;
	int x, y;
	unsigned char p;
	pixel **img;
	char newname[1024];
	const char *basename;

	fseek(f, 116 + 40 * (num_img - 1), SEEK_SET);
	width = read_LEint32(f);
	height = read_LEint32(f);
	img = ppm_allocarray(width, height);
	fseek(f, 100 + 40 * num_img + (width * height + 24) * n, SEEK_SET);
	for (y = 0; y < height; y++)
		for (x = 0; x < width; x++) {
			p = getc(f);
			img[y][x] = cmap[p];
		}

		basename = strrchr(fname, '/');
		if (basename != NULL)
			basename++;
		else
			basename = fname;
		strcpy(newname, basename);
		if (strlen(newname) > 4 &&
			strcasecmp(newname + strlen(newname) - 4, ".mat") == 0)
			newname[strlen(newname) - 4] = '\0';
		sprintf(newname + strlen(newname), "_%d.ppm", n);

		out = fopen(newname, "wb");
		if (out == NULL) {
			perror(newname);
			exit(1);
		}

		ppm_writeppm(out, img, width, height, 255, 0);
		ppm_freearray(img, height);
		fclose(out);
}
示例#3
0
void process_file(const char *fname) {
	FILE *in;
	int32_t num_img;
	int i;

	in = fopen(fname, "rb");
	if (in == NULL) {
		perror(fname);
		exit(1);
	}
	fseek(in, 12, SEEK_SET);
	num_img = read_LEint32(in);
	for (i = 0; i < num_img; i++)
		write_img(in, fname, i, num_img);
	fclose(in);
}
示例#4
0
void read_header(FILE *in, int *codec, int *num_images, int *format) {
	char magic[8];

	fread(magic, 1, 8, in);
	if (memcmp(magic, "BM  F\0\0\0", 8) != 0) {
		fprintf(stderr, "not a bitmap file\n");
		exit(1);
	}
	*codec = read_LEint32(in);

	read_LEint32(in);
	*num_images = read_LEint32(in);
	read_LEint32(in);		/* ignore x offset */
	read_LEint32(in);		/* ignore y offset */
	read_LEint32(in);
	*format = read_LEint32(in);
	fseek(in, 128, SEEK_SET);
}