Skip to content
imlib.c 30.6 KiB
Newer Older
	len = strlen(infilename) + sizeof(".tmp") + 1;
	outfilename = emalloc(len);
	snprintf(outfilename, len, "%s.tmp", infilename);

	if ((output_file = fopen(outfilename, "wb")) == NULL) {
		weprintf("couldn't open file for writing: %s\n", outfilename);
		free(outfilename);
		fclose(input_file);
		D_RETURN_(4);
	}

	/* Initialize the JPEG decompression object with default error handling. */
	srcinfo.err = jpeg_std_error(&jsrcerr);
	jpeg_create_decompress(&srcinfo);

	/* Initialize the JPEG compression object with default error handling. */
	dstinfo.err = jpeg_std_error(&jdsterr);
	jpeg_create_compress(&dstinfo);
	jsrcerr.trace_level = jdsterr.trace_level;

	/* Specify data source for decompression */
	jpeg_stdio_src(&srcinfo, input_file);

	/* Enable saving of extra markers that we want to copy */
	jcopy_markers_setup(&srcinfo, copyoption);

	/* Read file header */
	(void) jpeg_read_header(&srcinfo, TRUE);

	/* Any space needed by a transform option must be requested before
	 * jpeg_read_coefficients so that memory allocation will be done right.
	 */
	jtransform_request_workspace(&srcinfo, &transformoption);

	/* Read source file as DCT coefficients */
	src_coef_arrays = jpeg_read_coefficients(&srcinfo);

	/* Initialize destination compression parameters from source values */
	jpeg_copy_critical_parameters(&srcinfo, &dstinfo);

	/* Adjust destination parameters if required by transform options;
	 * also find out which set of coefficient arrays will hold the output.
	 */
	dst_coef_arrays = jtransform_adjust_parameters(&srcinfo, &dstinfo, src_coef_arrays, &transformoption);

	/* Specify data destination for compression */
	jpeg_stdio_dest(&dstinfo, output_file);

	/* Start compressor (note no image data is actually written here) */
	jpeg_write_coefficients(&dstinfo, dst_coef_arrays);

	/* Copy to the output file any extra markers that we want to preserve */
	jcopy_markers_execute(&srcinfo, &dstinfo, copyoption);

	/* Execute image transformation */
	jtransform_execute_transformation(&srcinfo, &dstinfo, src_coef_arrays, &transformoption);

	/* Finish compression and release memory */
	jpeg_finish_compress(&dstinfo);
	jpeg_destroy_compress(&dstinfo);

	(void) jpeg_finish_decompress(&srcinfo);
	jpeg_destroy_decompress(&srcinfo);

	fclose(input_file);
	fclose(output_file);

	/* TODO fix EXIF tags (orientation, width, height) */

	/* rename outfilename to infilename.. if it worked */
	if (jsrcerr.num_warnings > 0) {
		weprintf("got errors from libjpeg (%d), not replacing file\n", jsrcerr.num_warnings);
	} else {
		if (rename(outfilename, infilename)) {
			weprintf("failed to replace file %s with %s\n", infilename, outfilename);
		}
	}
	free(outfilename);
void feh_draw_actions(winwidget w)
	static Imlib_Font fn = NULL;
	int tw = 0, th = 0;
	int th_offset = 0;
	int max_tw = 0;
	int line_th = 0;
	Imlib_Image im = NULL;
	static DATA8 atab[256];
	int i = 0;
	int num_actions = 0;
	int cur_action = 0;
	char index[1];
	char *line;

	D_ENTER(4);

	/* Count number of defined actions. This method sucks a bit since it needs
	 * to be changed if the number of actions changes, but at least it doesn't
	 * miss actions 2 to 9 if action1 isn't defined
	 */
	for (i = 0; i < 10; i++) {
		if (opt.actions[i])
			num_actions++;
	}

	if (num_actions == 0)
		return;

	if ((!w->file) || (!FEH_FILE(w->file->data))
			|| (!FEH_FILE(w->file->data)->filename))
		D_RETURN_(4);

	if (!fn) {
		memset(atab, 0, sizeof(atab));
		if (w->full_screen)
			fn = gib_imlib_load_font(DEFAULT_FONT_BIG);
		else
			fn = gib_imlib_load_font(DEFAULT_FONT);
	}

	if (!fn) {
		weprintf("Couldn't load font for actions printing");
		D_RETURN_(4);
	}

	gib_imlib_get_text_size(fn, "defined actions:", NULL, &tw, &th, IMLIB_TEXT_TO_RIGHT);
// Check for the widest line
	max_tw = tw;

	for (i = 0; i < 10; i++) {
		if (opt.actions[i]) {
			gib_imlib_get_text_size(fn, opt.actions[i], NULL, &tw, &th, IMLIB_TEXT_TO_RIGHT);
			if (tw > max_tw)
				max_tw = tw;
		}
	}

	tw = max_tw;
	tw += 3;
	th += 3;
	line_th = th;
	th = (th * num_actions) + line_th;

	/* This depends on feh_draw_filename internals...
	 * should be fixed some time
	 */
	if (opt.draw_filename && w->full_screen)
		th_offset = line_th * 2;
	else if (opt.draw_filename)
		th_offset = line_th;

	im = imlib_create_image(tw, th);
	if (!im)
		eprintf("Couldn't create image. Out of memory?");

	gib_imlib_image_set_has_alpha(im, 1);
	gib_imlib_apply_color_modifier_to_rectangle(im, 0, 0, tw, th, NULL, NULL, NULL, atab);
	gib_imlib_image_fill_rectangle(im, 0, 0, tw, th, 0, 0, 0, 0);

	gib_imlib_text_draw(im, fn, NULL, 2, 2, "defined actions:", IMLIB_TEXT_TO_RIGHT, 0, 0, 0, 255);
	gib_imlib_text_draw(im, fn, NULL, 1, 1, "defined actions:", IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);

	for (i = 0; i < 10; i++) {
		if (opt.actions[i]) {
			cur_action++;
			line = emalloc(strlen(opt.actions[i]) + 5);
			sprintf(index, "%d", i);
			strcpy(line, index);
			strcat(line, ": ");
			strcat(line, opt.actions[i]);

			gib_imlib_text_draw(im, fn, NULL, 2,
					(cur_action * line_th) + 2, line,
					IMLIB_TEXT_TO_RIGHT, 0, 0, 0, 255);
			gib_imlib_text_draw(im, fn, NULL, 1,
					(cur_action * line_th) + 1, line,
					IMLIB_TEXT_TO_RIGHT, 255, 255, 255, 255);
			free(line);
		}
	}

	gib_imlib_render_image_on_drawable(w->bg_pmap, im, 0, 0 + th_offset, 1, 1, 0);

	gib_imlib_free_image_and_decache(im);
	D_RETURN_(4);