void orientation_map_free_resources (void) { ppm_kill (&update_om_preview_nbuffer); ppm_kill (&update_vector_preview_backup); ppm_kill (&update_vector_preview_buffer); }
void free_rotate (ppm_t *p, double amount) { int x, y; double nx, ny; ppm_t tmp = {0, 0, NULL}; double f = amount * G_PI * 2 / 360.0; int rowstride = p->width * 3; ppm_new (&tmp, p->width, p->height); for (y = 0; y < p->height; y++) { for (x = 0; x < p->width; x++) { double r, d; nx = fabs (x - p->width / 2.0); ny = fabs (y - p->height / 2.0); r = sqrt (nx * nx + ny * ny); d = atan2 ((y - p->height / 2.0), (x - p->width / 2.0)); nx = (p->width / 2.0 + cos (d - f) * r); ny = (p->height / 2.0 + sin (d - f) * r); get_rgb (p, nx, ny, tmp.col + y * rowstride + x * 3); } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
void dummybrushdmenuselect (GtkWidget *w, gpointer data) { ppm_kill (&brushppm); ppm_new (&brushppm, 10,10); brush_from_file = 0; update_brush_preview (NULL); }
void ppm_copy (ppm_t *s, ppm_t *p) { ppm_kill (p); p->width = s->width; p->height = s->height; p->col = g_memdup (s->col, p->width * 3 * p->height); }
void ppm_pad (ppm_t *p, int left,int right, int top, int bottom, guchar *bg) { int x, y; ppm_t tmp = {0, 0, NULL}; ppm_new (&tmp, p->width + left + right, p->height + top + bottom); for (y = 0; y < tmp.height; y++) { guchar *row, *srcrow; row = tmp.col + y * tmp.width * 3; if ((y < top) || (y >= tmp.height-bottom)) { for (x = 0; x < tmp.width; x++) { int k = x * 3; row[k+0] = bg[0]; row[k+1] = bg[1]; row[k+2] = bg[2]; } continue; } srcrow = p->col + (y-top) * p->width * 3; for (x = 0; x < left; x++) { int k = x * 3; row[k+0] = bg[0]; row[k+1] = bg[1]; row[k+2] = bg[2]; } for (; x < tmp.width-right; x++) { int k = y * tmp.width * 3 + x * 3; tmp.col[k+0] = srcrow[(x - left) * 3 + 0]; tmp.col[k+1] = srcrow[(x - left) * 3 + 1]; tmp.col[k+2] = srcrow[(x - left) * 3 + 2]; } for (; x < tmp.width; x++) { int k = x * 3; row[k+0] = bg[0]; row[k+1] = bg[1]; row[k+2] = bg[2]; } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
void edgepad (ppm_t *p, int left,int right, int top, int bottom) { int x, y; ppm_t tmp = {0, 0, NULL}; guchar testcol[3] = {0, 255, 0}; int srowstride, drowstride; ppm_new (&tmp, p->width+left+right, p->height+top+bottom); fill (&tmp, testcol); srowstride = p->width * 3; drowstride = tmp.width * 3; for (y = 0; y < top; y++) { memcpy (&tmp.col[y * drowstride + left * 3], p->col, srowstride); } for (; y-top < p->height; y++) { memcpy (&tmp.col[y * drowstride + left * 3], p->col + (y - top) * srowstride, srowstride); } for (; y < tmp.height ; y++) { memcpy (&tmp.col[y * drowstride + left * 3], p->col + (p->height - 1) * srowstride, srowstride); } for (y = 0; y < tmp.height; y++) { guchar *col, *tmprow; tmprow = tmp.col + y*drowstride; col = tmp.col + y*drowstride + left*3; for (x = 0; x < left; x++) { memcpy (&tmprow[x * 3], col, 3); } col = tmp.col + y * drowstride + (tmp.width-right - 1) * 3; for (x = 0; x < right; x++) { memcpy (&tmprow[(x + tmp.width - right - 1) * 3], col, 3); } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
/* * This function caches the last result. Call it with fn as NULL, to * free the arguments. * */ void brush_reload (const gchar *fn, ppm_t *p) { static char lastfn[256] = ""; static ppm_t cache = {0, 0, NULL}; if (fn == NULL) { ppm_kill (&cache); lastfn[0] = '\0'; return; } if (strcmp (fn, lastfn)) { g_strlcpy (lastfn, fn, sizeof (lastfn)); ppm_kill (&cache); ppm_load (fn, &cache); } ppm_copy (&cache, p); set_colorbrushes (fn); }
void blur (ppm_t *p, int xrad, int yrad) { int x, y, k; int tx, ty; ppm_t tmp = {0,0,NULL}; int r, g, b, n; int rowstride = p->width * 3; ppm_new (&tmp, p->width, p->height); for (y = 0; y < p->height; y++) { for (x = 0; x < p->width; x++) { r = g = b = n = 0; for (ty = y-yrad; ty <= y+yrad; ty++) { for (tx = x-xrad; tx <= x+xrad; tx++) { if (ty<0) continue; if (ty>=p->height) continue; if (tx<0) continue; if (tx>=p->width) continue; k = ty * rowstride + tx * 3; r += p->col[k+0]; g += p->col[k+1]; b += p->col[k+2]; n++; } } k = y * rowstride + x * 3; tmp.col[k+0] = r / n; tmp.col[k+1] = g / n; tmp.col[k+2] = b / n; } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
void crop (ppm_t *p, int lx, int ly, int hx, int hy) { ppm_t tmp = {0,0,NULL}; int x, y; int srowstride = p->width * 3; int drowstride; ppm_new (&tmp, hx - lx, hy - ly); drowstride = tmp.width * 3; for (y = ly; y < hy; y++) for (x = lx; x < hx; x++) memcpy (&tmp.col[(y - ly) * drowstride + (x - lx) * 3], &p->col[y * srowstride + x * 3], 3); ppm_kill (p); p->col = tmp.col; p->width = tmp.width; p->height = tmp.height; }
static void load_gimp_brush (const gchar *fn, ppm_t *p) { FILE *f; struct _BrushHeader hdr; gchar *ptr; gint x, y; f = fopen_from_search_path (fn, "rb"); ppm_kill (p); if (!f) { g_printerr ("load_gimp_brush: Unable to open file \"%s\"!\n", gimp_filename_to_utf8 (fn)); ppm_new (p, 10,10); return; } fread (&hdr, 1, sizeof (struct _BrushHeader), f); for (x = 0; x < 7; x++) msb2lsb (&((unsigned int *)&hdr)[x]); ppm_new (p, hdr.width, hdr.height); ptr = g_malloc (hdr.width); fseek (f, hdr.header_size, SEEK_SET); for (y = 0; y < p->height; y++) { fread (ptr, p->width, 1, f); for (x = 0; x < p->width; x++) { int k = y * p->width * 3 + x * 3; p->col[k+0] = p->col[k+1] = p->col[k+2] = ptr[x]; } } fclose (f); g_free (ptr); }
static void paper_update_preview (void) { gint i, j; guchar *buf, *paper_preview_buffer; gdouble sc; ppm_t p = {0, 0, NULL}; ppm_load (pcvals.selected_paper, &p); sc = p.width > p.height ? p.width : p.height; sc = 100.0 / sc; resize (&p, p.width*sc,p.height*sc); paper_preview_buffer = g_new0 (guchar, 100*100); for (i = 0, buf = paper_preview_buffer; i < 100; i++, buf += 100) { gint k = i * p.width * 3; if (i < p.height) { for (j = 0; j < p.width; j++) buf[j] = p.col[k + j * 3]; if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (paper_invert))) for (j = 0; j < p.width; j++) buf[j] = 255 - buf[j]; } } gimp_preview_area_draw (GIMP_PREVIEW_AREA (paper_preview), 0, 0, 100, 100, GIMP_GRAY_IMAGE, paper_preview_buffer, 100); ppm_kill (&p); g_free (paper_preview_buffer); gtk_widget_queue_draw (paper_preview); }
void resize (ppm_t *p, int nx, int ny) { int x, y; float xs = p->width / (float)nx; float ys = p->height / (float)ny; ppm_t tmp = {0, 0, NULL}; ppm_new (&tmp, nx, ny); for (y = 0; y < ny; y++) { guchar *row = tmp.col + y * tmp.width * 3; for (x = 0; x < nx; x++) { get_rgb (p, x * xs, y * ys, &row[x * 3]); } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
void resize_fast (ppm_t *p, int nx, int ny) { int x, y; float xs = p->width / (float)nx; float ys = p->height / (float)ny; ppm_t tmp = {0, 0, NULL}; ppm_new (&tmp, nx, ny); for (y = 0; y < ny; y++) { for (x = 0; x < nx; x++) { gint rx = x * xs, ry = y * ys; memcpy (&tmp.col[y * tmp.width * 3 + x * 3], &p->col[ry * p->width * 3 + rx * 3], 3); } } ppm_kill (p); p->width = tmp.width; p->height = tmp.height; p->col = tmp.col; }
static void brushdmenuselect (GtkWidget *widget, gpointer data) { GimpPixelRgn src_rgn; guchar *src_row; guchar *src; gint id; gint bpp; gint x, y; ppm_t *p; gint x1, y1, x2, y2; gint row; GimpDrawable *drawable; gint rowstride; gimp_int_combo_box_get_active (GIMP_INT_COMBO_BOX (widget), &id); if (id == -1) return; if (brush_from_file == 2) return; /* Not finished GUI-building yet */ if (brush_from_file) { #if 0 unselectall (brush_list); #endif preset_save_button_set_sensitive (FALSE); } gtk_adjustment_set_value (brush_gamma_adjust, 1.0); gtk_adjustment_set_value (brush_aspect_adjust, 0.0); drawable = gimp_drawable_get (id); gimp_drawable_mask_bounds (drawable->drawable_id, &x1, &y1, &x2, &y2); bpp = gimp_drawable_bpp (drawable->drawable_id); ppm_kill (&brushppm); ppm_new (&brushppm, x2 - x1, y2 - y1); p = &brushppm; rowstride = p->width * 3; src_row = g_new (guchar, (x2 - x1) * bpp); gimp_pixel_rgn_init (&src_rgn, drawable, 0, 0, x2 - x1, y2 - y1, FALSE, FALSE); if (bpp == 3) { /* RGB */ int bpr = (x2 - x1) * 3; for (row = 0, y = y1; y < y2; row++, y++) { gimp_pixel_rgn_get_row (&src_rgn, src_row, x1, y, (x2 - x1)); memcpy (p->col + row*rowstride, src_row, bpr); } } else { /* RGBA (bpp > 3) GrayA (bpp == 2) or Gray */ gboolean is_gray = ((bpp > 3) ? TRUE : FALSE); for (row = 0, y = y1; y < y2; row++, y++) { guchar *tmprow = p->col + row * rowstride; guchar *tmprow_ptr; gimp_pixel_rgn_get_row (&src_rgn, src_row, x1, y, (x2 - x1)); src = src_row; tmprow_ptr = tmprow; /* Possible micro-optimization here: * src_end = src + src_rgn.bpp * (x2-x1); * for ( ; src < src_end ; src += src_rgn.bpp) */ for (x = x1; x < x2; x++) { *(tmprow_ptr++) = src[0]; *(tmprow_ptr++) = src[is_gray ? 1 : 0]; *(tmprow_ptr++) = src[is_gray ? 2 : 0]; src += src_rgn.bpp; } } } g_free (src_row); if (bpp >= 3) pcvals.color_brushes = 1; else pcvals.color_brushes = 0; brush_from_file = 0; update_brush_preview (NULL); }
static void update_brush_preview (const gchar *fn) { gint i, j; guchar *preview_image; if (fn) brush_from_file = 1; preview_image = g_new0 (guchar, 100*100); if (!fn && brush_from_file) { /* preview_image is already initialized to our liking. */ } else { double sc; ppm_t p = {0, 0, NULL}; guchar gammatable[256]; int newheight; if (brush_from_file) brush_reload (fn, &p); else if (PPM_IS_INITED (&brushppm)) ppm_copy (&brushppm, &p); set_colorbrushes (fn); sc = gtk_adjustment_get_value (brush_gamma_adjust); if (sc != 1.0) for (i = 0; i < 256; i++) gammatable[i] = pow (i / 255.0, sc) * 255; else for (i = 0; i < 256; i++) gammatable[i] = i; newheight = p.height * pow (10, gtk_adjustment_get_value (brush_aspect_adjust)); sc = p.width > newheight ? p.width : newheight; sc = 100.0 / sc; resize_fast (&p, p.width*sc,newheight*sc); padbrush (&p, 100, 100); for (i = 0; i < 100; i++) { int k = i * p.width * 3; if (i < p.height) for (j = 0; j < p.width; j++) preview_image[i*100+j] = gammatable[p.col[k + j * 3]]; } ppm_kill (&p); } gimp_preview_area_draw (GIMP_PREVIEW_AREA (brush_preview), 0, 0, 100, 100, GIMP_GRAY_IMAGE, preview_image, 100); g_free (preview_image); }
void size_map_free_resources (void) { ppm_kill (&update_vector_preview_backup); ppm_kill (&update_vector_preview_sbuffer); }
void autocrop (ppm_t *p, int room) { int lx = 0, hx = p->width, ly = 0, hy = p->height; int x, y, n = 0; guchar tc[3]; ppm_t tmp = {0,0,NULL}; int rowstride = p->width * 3; int drowstride; /* upper */ memcpy (&tc, p->col, 3); for (y = 0; y < p->height; y++) { n = 0; for (x = 0; x < p->width; x++) { if (memcmp (&tc, &p->col[y*rowstride+x*3], 3)) { n++; break; } } if (n) break; } if (n) ly = y; #if 0 printf("ly = %d\n", ly); #endif /* lower */ memcpy (&tc, &p->col[(p->height - 1) * rowstride], 3); for (y = p->height-1; y >= 0; y--) { n = 0; for (x = 0; x < p->width; x++) { if (memcmp (&tc, &p->col[y*rowstride+x*3], 3)) { n++; break; } } if (n) break; } if (n) hy = y+1; if (hy >= p->height) hy = p->height - 1; #if 0 printf("hy = %d\n", hy); #endif /* left */ memcpy (&tc, &p->col[ly * rowstride], 3); for (x = 0; x < p->width; x++) { n = 0; for (y = ly; y <= hy && y < p->height; y++) { if (memcmp (&tc, &p->col[y * rowstride + x * 3], 3)) { n++; break; } } if (n) break; } if (n) lx = x; #if 0 printf("lx = %d\n", lx); #endif /* right */ memcpy (&tc, &p->col[ly * rowstride + (p->width - 1) * 3], 3); for (x = p->width-1; x >= 0; x--) { n = 0; for (y = ly; y <= hy; y++) { if (memcmp (&tc, &p->col[y * rowstride + x * 3], 3)) { n++; break; } } if (n) break; } if (n) hx = x + 1; #if 0 printf("hx = %d\n", hx); #endif lx -= room; if (lx < 0) lx = 0; ly -= room; if (ly < 0) ly = 0; hx += room; if (hx >= p->width) hx = p->width - 1; hy += room; if (hy >= p->height) hy = p->height - 1; ppm_new (&tmp, hx - lx, hy - ly); drowstride = tmp.width * 3; for (y = ly; y < hy; y++) for (x = lx; x < hx; x++) memcpy (&tmp.col[(y - ly) * drowstride + (x - lx) * 3], &p->col[y * rowstride + x * 3], 3); ppm_kill (p); p->col = tmp.col; p->width = tmp.width; p->height = tmp.height; }
void ppm_load (const char *fn, ppm_t *p) { char line[200]; int y, pgm = 0; FILE *f; if (!strcmp (&fn[strlen (fn)-4], ".gbr")) { load_gimp_brush(fn, p); return; } f = fopen_from_search_path (fn, "rb"); ppm_kill (p); if (!f) { g_printerr ("ppm_load: Unable to open file \"%s\"!\n", gimp_filename_to_utf8 (fn)); ppm_new (p, 10,10); return; } readline (f, line, 200); if (strcmp (line, "P6")) { if (strcmp (line, "P5")) { fclose (f); g_printerr ("ppm_load: File \"%s\" not PPM/PGM? (line=\"%s\")%c\n", gimp_filename_to_utf8 (fn), line, 7); ppm_new (p, 10,10); return; } pgm = 1; } readline (f, line, 200); p->width = atoi (line); p->height = atoi (strchr (line, ' ')+1); readline (f, line, 200); if (strcmp (line, "255")) { g_printerr ("ppm_load: File \"%s\" not valid PPM/PGM? (line=\"%s\")%c\n", gimp_filename_to_utf8 (fn), line, 7); ppm_new (p, 10,10); return; } p->col = g_malloc (p->height * p->width * 3); if (!pgm) { fread (p->col, p->height * 3 * p->width, 1, f); } else { guchar *tmpcol = g_malloc (p->width * p->height); fread (tmpcol, p->height * p->width, 1, f); for (y = 0; y < p->width * p->height * 3; y++) { p->col[y] = tmpcol[y / 3]; } } fclose (f); }