Example #1
0
/* check for file in the database and if not found there, if it can be detected */
retvalue files_expect(const char *filekey, const struct checksums *checksums, bool warnifadded) {
	retvalue r;
	char *filename;
	struct checksums *improvedchecksums = NULL;

	r = files_canadd(filekey, checksums);
	if (r == RET_NOTHING)
		return RET_OK;
	if (RET_WAS_ERROR(r))
		return r;

	/* ready to add means missing, so have to look for the file itself: */
	filename = files_calcfullfilename(filekey);
	if (FAILEDTOALLOC(filename))
		return RET_ERROR_OOM;

	/* first check if a possible manually put (or left over from previous
	 * downloads attepts) file is there and is correct */
	r = checksums_test(filename, checksums, &improvedchecksums);
	if (r == RET_ERROR_WRONG_MD5) {
		fprintf(stderr,
"Deleting unexpected file '%s'!\n"
"(not in database and wrong in pool)\n ",
				filename);
		if (unlink(filename) == 0)
			r = RET_NOTHING;
		else {
			int e = errno;
			fprintf(stderr,
"Error %d deleting '%s': %s!\n", e, filename, strerror(e));
		}
	}
	free(filename);
	if (!RET_IS_OK(r))
		return r;

	if (warnifadded)
		fprintf(stderr,
"Warning: readded existing file '%s' mysteriously missing from the checksum database.\n",
				filekey);

	// TODO: some callers might want the updated checksum when
	// improves is true, how to get them there?

	/* add found file to database */
	if (improvedchecksums != NULL) {
		r = files_add_checksums(filekey, improvedchecksums);
		checksums_free(improvedchecksums);
	} else
		r = files_add_checksums(filekey, checksums);
	assert (r != RET_NOTHING);
	return r;
}
Example #2
0
int main(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "usage: picture [options] <infile> ...\n");
		fprintf(stderr, "options:  --test    run test suite\n");
		fprintf(stderr,
				"          --force   don't skip existing output files\n");
		return 1;
	}
	char basename[100];
	char outfilename[200];
	char linkfilename[200];
	bool force = false;
	AppData data;
	null_images(&data);
	for (int n = 1; n < argc; n++) {
		if (strcmp(argv[n], "--test") == 0) {
			printf("Running dir8 test suite...\n");
			dir8_test();
			printf("Running dir16 test suite...\n");
			dir16_test();
			printf("Running checksums test suite...\n");
			checksums_test();
			continue;
		} else if (strcmp(argv[n], "--force") == 0) {
			force = true;
			continue;
		}
		char *infilename = argv[n];
		char *slash = strrchr(infilename, '/');
		if (!slash)
			slash = infilename - 1;
		strcpy(basename, slash + 1);
		char *ext = strrchr(basename, '.');
		if (ext)
			ext[0] = 0;
		strcpy(outfilename, "output/");
		strcat(outfilename, basename);
		strcat(outfilename, ".png");
		strcpy(linkfilename, "digits/");
		strcat(linkfilename, basename);
		if (!force) {
			FILE *fp = fopen(outfilename, "r");
			if (fp) {			// Skip existing output file.
				fclose(fp);
				continue;
			}
		}
		printf("%s => %s\n", infilename, outfilename);
		IplImage *input = cvLoadImage(infilename, CV_LOAD_IMAGE_COLOR);
		if (!input) {
			fprintf(stderr, "could not read %s\n", infilename);
			return 1;
		}
		int zoom = max(1, max(input->width, input->height) / 600);
		if (data.red == NULL ||
			data.red->width != input->width / zoom ||
			data.red->height != input->height / zoom) {
			if (data.red)
				release_images(&data);
			if (data.debug)
				cvReleaseImage(&data.debug);
			CvSize zoom_size =
				cvSize(input->width / zoom, input->height / zoom);
			create_images(&data, zoom_size, 4);
			CvSize debug_size = cvSize(COLUMNS * input->width / zoom,
									   ROWS * input->height / zoom);
			printf("debug_size=%dx%d\n", debug_size.width,
				   debug_size.height);
			data.debug = cvCreateImage(debug_size, IPL_DEPTH_8U, 3);
		}
		// printf("Resizing input from %dx%d to %dx%d...\n",
		// input->width, input->height, data.rgb->width, data.rgb->height);
		cvCvtColor(input, input, CV_BGR2RGB);
		cvResize(input, data.rgb, CV_INTER_AREA);
		cvSplit(data.rgb, data.red, NULL, NULL, NULL);
		// debug_image(&data, data.rgb);
		bool success = decode(&data, ANGLE_OF_VIEW,
							  "gnuplot threshold trace simulator1 simulator3",
							  NULL, NULL);
		if (success) {
			unlink(linkfilename);
			symlink(data.digits, linkfilename);
			printf("digits=%s\n", data.digits);
		}
		cvResetImageROI(data.debug);
		cvCvtColor(data.debug, data.debug, CV_RGB2BGR);
		if (cvSaveImage(outfilename, data.debug) == 0) {
			fprintf(stderr, "could not save output file\n");
			return 1;
		}
	}
	if (data.red)
		release_images(&data);
	if (data.debug)
		cvReleaseImage(&data.debug);
	return 0;
}