/* Do the final setup of the iter struct */ static void prepare_iteration (GeglBufferIterator *iter) { int index; GeglBufferIteratorPriv *priv = iter->priv; gint origin_offset_x; gint origin_offset_y; /* Set up the origin tile */ /* FIXME: Pick the most compatable buffer, not just the first */ { GeglBuffer *buf = priv->sub_iter[0].buffer; priv->origin_tile.x = buf->shift_x; priv->origin_tile.y = buf->shift_y; priv->origin_tile.width = buf->tile_width; priv->origin_tile.height = buf->tile_height; origin_offset_x = buf->shift_x + priv->sub_iter[0].full_rect.x; origin_offset_y = buf->shift_y + priv->sub_iter[0].full_rect.y; } for (index = 0; index < priv->num_buffers; index++) { SubIterState *sub = &priv->sub_iter[index]; GeglBuffer *buf = sub->buffer; gint current_offset_x = buf->shift_x + priv->sub_iter[index].full_rect.x; gint current_offset_y = buf->shift_y + priv->sub_iter[index].full_rect.y; /* Format converison needed */ if (gegl_buffer_get_format (sub->buffer) != sub->format) sub->access_mode |= GEGL_ITERATOR_INCOMPATIBLE; /* Incompatable tiles */ else if ((priv->origin_tile.width != buf->tile_width) || (priv->origin_tile.height != buf->tile_height) || (abs(origin_offset_x - current_offset_x) % priv->origin_tile.width != 0) || (abs(origin_offset_y - current_offset_y) % priv->origin_tile.height != 0)) { /* Check if the buffer is a linear buffer */ if ((buf->extent.x == -buf->shift_x) && (buf->extent.y == -buf->shift_y) && (buf->extent.width == buf->tile_width) && (buf->extent.height == buf->tile_height)) { sub->linear_tile = gegl_buffer_get_tile (sub->buffer, 0, 0, 0); if (sub->access_mode & GEGL_ACCESS_WRITE) gegl_tile_lock (sub->linear_tile); } else sub->access_mode |= GEGL_ITERATOR_INCOMPATIBLE; } gegl_buffer_lock (sub->buffer); } }
/* Do the final setup of the iter struct */ static void prepare_iteration (GeglBufferIterator *iter) { int index; GeglBufferIteratorPriv *priv = iter->priv; /* Set up the origin tile */ /* FIXME: Pick the most compatable buffer, not just the first */ { GeglBuffer *buf = priv->sub_iter[0].buffer; priv->origin_tile.x = buf->shift_x; priv->origin_tile.y = buf->shift_y; priv->origin_tile.width = buf->tile_width; priv->origin_tile.height = buf->tile_height; } for (index = 0; index < priv->num_buffers; index++) { SubIterState *sub = &priv->sub_iter[index]; GeglBuffer *buf = sub->buffer; /* Format converison needed */ if (gegl_buffer_get_format (sub->buffer) != sub->format) sub->flags |= GEGL_ITERATOR_INCOMPATIBLE; /* Incompatable tiles */ else if ((priv->origin_tile.x != buf->shift_x) || (priv->origin_tile.y != buf->shift_y) || (priv->origin_tile.width != buf->tile_width) || (priv->origin_tile.height != buf->tile_height)) { /* Check if the buffer is a linear buffer */ if ((buf->extent.x == -buf->shift_x) && (buf->extent.y == -buf->shift_y) && (buf->extent.width == buf->tile_width) && (buf->extent.height == buf->tile_height)) { sub->linear_tile = gegl_tile_source_get_tile ((GeglTileSource *)(sub->buffer), 0, 0, 0); if (sub->flags & GEGL_BUFFER_WRITE) gegl_tile_lock (sub->linear_tile); } else sub->flags |= GEGL_ITERATOR_INCOMPATIBLE; } gegl_buffer_lock (sub->buffer); } }
static void get_tile (GeglBufferIterator *iter, int index) { GeglBufferIteratorPriv *priv = iter->priv; SubIterState *sub = &priv->sub_iter[index]; GeglBuffer *buf = priv->sub_iter[index].buffer; if (sub->linear_tile) { sub->current_tile = sub->linear_tile; sub->real_roi = buf->extent; sub->current_tile_mode = GeglIteratorTileMode_LinearTile; } else { int shift_x = buf->shift_x; int shift_y = buf->shift_y; int tile_width = buf->tile_width; int tile_height = buf->tile_height; int tile_x = gegl_tile_indice (iter->roi[index].x + shift_x, tile_width); int tile_y = gegl_tile_indice (iter->roi[index].y + shift_y, tile_height); sub->current_tile = gegl_tile_source_get_tile ((GeglTileSource *)(buf), tile_x, tile_y, 0); if (sub->flags & GEGL_BUFFER_WRITE) gegl_tile_lock (sub->current_tile); sub->real_roi.x = (tile_x * tile_width) - shift_x; sub->real_roi.y = (tile_y * tile_height) - shift_y; sub->real_roi.width = tile_width; sub->real_roi.height = tile_height; sub->current_tile_mode = GeglIteratorTileMode_DirectTile; } sub->row_stride = buf->tile_width * sub->format_bpp; iter->data[index] = gegl_tile_get_data (sub->current_tile); }
static GeglTile * gegl_buffer_get_tile (GeglTileSource *source, gint x, gint y, gint z) { GeglTileHandler *handler = (GeglTileHandler*) (source); GeglTile *tile = NULL; source = handler->source; if (source) tile = gegl_tile_source_get_tile (source, x, y, z); else g_assert (0); if (tile) { GeglBuffer *buffer = (GeglBuffer*) (handler); /* storing information in tile, to enable the dispose function of the * tile instance to "hook" back to the storage with correct * coordinates. */ { if (!tile->tile_storage) { gegl_tile_lock (tile); tile->tile_storage = buffer->tile_storage; gegl_tile_unlock (tile); tile->rev--; } tile->x = x; tile->y = y; tile->z = z; } } return tile; }
GeglBuffer * gegl_buffer_load (const gchar *path) { GeglBuffer *ret; LoadInfo *info = g_slice_new0 (LoadInfo); info->path = g_strdup (path); info->i = g_open (info->path, O_RDONLY, 0770); GEGL_NOTE (GEGL_DEBUG_BUFFER_LOAD, "starting to load buffer %s", path); if (info->i == -1) { GEGL_NOTE (GEGL_DEBUG_BUFFER_LOAD, "failed top open %s for reading", path); return NULL; } { GeglBufferItem *header = gegl_buffer_read_header (info->i, &info->offset); g_assert (header); /*memcpy (&(info->header), header, sizeof (GeglBufferHeader));*/ info->header = *(&header->header); info->offset = info->header.next; /*g_free (header);*/ /* is there a pointer to a string or something we're missing? */ } info->tile_size = info->header.tile_width * info->header.tile_height * info->header.bytes_per_pixel; info->format = babl_format (info->header.description); ret = g_object_new (GEGL_TYPE_BUFFER, "format", info->format, "tile-width", info->header.tile_width, "tile-height", info->header.tile_height, "height", info->header.height, "width", info->header.width, "path", path, NULL); /* sanity check, should probably report error condition and return safely instead */ g_assert (babl_format_get_bytes_per_pixel (info->format) == info->header.bytes_per_pixel); info->tiles = gegl_buffer_read_index (info->i, &info->offset); /* load each tile */ { GList *iter; gint i = 0; for (iter = info->tiles; iter; iter = iter->next) { GeglBufferTile *entry = iter->data; guchar *data; GeglTile *tile; tile = gegl_tile_source_get_tile (GEGL_TILE_SOURCE (ret), entry->x, entry->y, entry->z); if (info->offset != entry->offset) { seekto (info, entry->offset); } /*g_assert (info->offset == entry->offset);*/ g_assert (tile); gegl_tile_lock (tile); data = gegl_tile_get_data (tile); g_assert (data); { ssize_t sz_read = read (info->i, data, info->tile_size); if(sz_read != -1) info->offset += sz_read; } /*g_assert (info->offset == entry->offset + info->tile_size);*/ gegl_tile_unlock (tile); gegl_tile_unref (tile); i++; } GEGL_NOTE (GEGL_DEBUG_BUFFER_LOAD, "%i tiles loaded",i); } GEGL_NOTE (GEGL_DEBUG_BUFFER_LOAD, "buffer loaded %s", info->path); load_info_destroy (info); return ret; }
static GeglTile * get_tile (GeglTileSource *gegl_tile_source, gint x, gint y, gint z) { GeglTileSource *source = ((GeglTileHandler *) gegl_tile_source)->source; GeglTileHandlerZoom *zoom = (GeglTileHandlerZoom *) gegl_tile_source; GeglTile *tile = NULL; const Babl *format = gegl_tile_backend_get_format (zoom->backend); GeglTileStorage *tile_storage; gint tile_width; gint tile_height; gint tile_size; if (source) tile = gegl_tile_source_get_tile (source, x, y, z); if (tile) return tile; if (z == 0)/* at base level with no tile found->send null, and shared empty tile will be used instead */ { return NULL; } tile_storage = _gegl_tile_handler_get_tile_storage ((GeglTileHandler *) zoom); if (z > tile_storage->seen_zoom) tile_storage->seen_zoom = z; g_object_get (zoom->backend, "tile-width", &tile_width, "tile-height", &tile_height, "tile-size", &tile_size, NULL); { gint i, j; GeglTile *source_tile[2][2] = { { NULL, NULL }, { NULL, NULL } }; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) { /* we get the tile from ourselves, to make successive rescales work * correctly */ source_tile[i][j] = gegl_tile_source_get_tile (gegl_tile_source, x * 2 + i, y * 2 + j, z - 1); } if (source_tile[0][0] == NULL && source_tile[0][1] == NULL && source_tile[1][0] == NULL && source_tile[1][1] == NULL) { return NULL; /* no data from level below, return NULL and let GeglTileHandlerEmpty fill in the shared empty tile */ } g_assert (tile == NULL); tile = gegl_tile_handler_create_tile (GEGL_TILE_HANDLER (zoom), x, y, z); gegl_tile_lock (tile); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) { if (source_tile[i][j]) { set_half (tile, source_tile[i][j], tile_width, tile_height, format, i, j); gegl_tile_unref (source_tile[i][j]); } else { set_blank (tile, tile_width, tile_height, format, i, j); } } gegl_tile_unlock (tile); } return tile; }
static GeglTile * gimp_tile_handler_projection_validate (GeglTileSource *source, GeglTile *tile, gint x, gint y) { GimpTileHandlerProjection *projection; cairo_region_t *tile_region; cairo_rectangle_int_t tile_rect; projection = GIMP_TILE_HANDLER_PROJECTION (source); if (cairo_region_is_empty (projection->dirty_region)) return tile; tile_region = cairo_region_copy (projection->dirty_region); tile_rect.x = x * projection->tile_width; tile_rect.y = y * projection->tile_height; tile_rect.width = projection->tile_width; tile_rect.height = projection->tile_height; cairo_region_intersect_rectangle (tile_region, &tile_rect); if (! cairo_region_is_empty (tile_region)) { gint tile_bpp; gint tile_stride; gint n_rects; gint i; if (! tile) tile = gegl_tile_handler_create_tile (GEGL_TILE_HANDLER (source), x, y, 0); cairo_region_subtract_rectangle (projection->dirty_region, &tile_rect); tile_bpp = babl_format_get_bytes_per_pixel (projection->format); tile_stride = tile_bpp * projection->tile_width; gegl_tile_lock (tile); n_rects = cairo_region_num_rectangles (tile_region); #if 0 g_printerr ("%d ", n_rects); #endif for (i = 0; i < n_rects; i++) { cairo_rectangle_int_t blit_rect; cairo_region_get_rectangle (tile_region, i, &blit_rect); #if 0 g_printerr ("constructing projection at %d %d %d %d\n", blit_rect.x, blit_rect.y, blit_rect.width, blit_rect.height); #endif gegl_node_blit (projection->graph, 1.0, GEGL_RECTANGLE (blit_rect.x, blit_rect.y, blit_rect.width, blit_rect.height), projection->format, gegl_tile_get_data (tile) + (blit_rect.y % projection->tile_height) * tile_stride + (blit_rect.x % projection->tile_width) * tile_bpp, tile_stride, GEGL_ABYSS_NONE); } gegl_tile_unlock (tile); } cairo_region_destroy (tile_region); return tile; }
/* FIXME: make this use direct data access in more cases than the * case of the base buffer. */ gpointer * gegl_buffer_linear_open (GeglBuffer *buffer, const GeglRectangle *extent, /* if NULL, use buf */ gint *rowstride,/* returns rowstride */ const Babl *format) /* if NULL, from buf */ { if (!format) format = buffer->soft_format; if (extent == NULL) extent=&buffer->extent; /*gegl_buffer_lock (buffer);*/ g_rec_mutex_lock (&buffer->tile_storage->mutex); if (extent->x == buffer->extent.x && extent->y == buffer->extent.y && extent->width == buffer->tile_width && extent->height <= buffer->tile_height && buffer->soft_format == format) { GeglTile *tile; g_assert (buffer->tile_width <= buffer->tile_storage->tile_width); g_assert (buffer->tile_height == buffer->tile_storage->tile_height); tile = g_object_get_data (G_OBJECT (buffer), "linear-tile"); g_assert (tile == NULL); /* We need to reference count returned direct * linear buffers to allow multiple open like * the copying case. */ tile = gegl_tile_source_get_tile ((GeglTileSource*) (buffer), 0,0,0); g_assert (tile); gegl_tile_lock (tile); g_object_set_data (G_OBJECT (buffer), "linear-tile", tile); if(rowstride)*rowstride = buffer->tile_storage->tile_width * babl_format_get_bytes_per_pixel (format); return (gpointer)gegl_tile_get_data (tile); } /* first check if there is a linear buffer, share the existing buffer if one * exists. */ { GList *linear_buffers; GList *iter; BufferInfo *info = NULL; linear_buffers = g_object_get_data (G_OBJECT (buffer), "linear-buffers"); for (iter = linear_buffers; iter; iter=iter->next) { info = iter->data; if (info->format == format && info->extent.x == buffer->extent.x && info->extent.y == buffer->extent.y && info->extent.width == buffer->extent.width && info->extent.height == buffer->extent.height ) { info->refs++; g_print ("!!!!!! sharing a linear buffer!!!!!\n"); return info->buf; } } } { BufferInfo *info = g_new0 (BufferInfo, 1); GList *linear_buffers; gint rs; linear_buffers = g_object_get_data (G_OBJECT (buffer), "linear-buffers"); linear_buffers = g_list_append (linear_buffers, info); g_object_set_data (G_OBJECT (buffer), "linear-buffers", linear_buffers); info->extent = *extent; info->format = format; rs = info->extent.width * babl_format_get_bytes_per_pixel (format); if(rowstride)*rowstride = rs; info->buf = gegl_malloc (rs * info->extent.height); gegl_buffer_get_unlocked (buffer, 1.0, &info->extent, format, info->buf, rs, GEGL_ABYSS_NONE); return info->buf; } return NULL; }
static gboolean gegl_buffer_tile_iterator_next (GeglBufferTileIterator *i) { GeglBuffer *buffer = i->buffer; gint tile_width = buffer->tile_storage->tile_width; gint tile_height = buffer->tile_storage->tile_height; gint buffer_shift_x = buffer->shift_x; gint buffer_shift_y = buffer->shift_y; gint buffer_x = i->roi.x + buffer_shift_x; gint buffer_y = i->roi.y + buffer_shift_y; if (i->roi.width == 0 || i->roi.height == 0) return FALSE; gulp: /* unref previously held tile */ if (i->tile) { if (i->write && i->subrect.width == tile_width && i->same_format) { gegl_tile_unlock (i->tile); } gegl_tile_unref (i->tile); i->tile = NULL; } if (i->next_col < i->roi.width) { /* return tile on this row */ gint tiledx = buffer_x + i->next_col; gint tiledy = buffer_y + i->next_row; gint offsetx = gegl_tile_offset (tiledx, tile_width); gint offsety = gegl_tile_offset (tiledy, tile_height); { i->subrect.x = offsetx; i->subrect.y = offsety; if (i->roi.width + offsetx - i->next_col < tile_width) i->subrect.width = (i->roi.width + offsetx - i->next_col) - offsetx; else i->subrect.width = tile_width - offsetx; if (i->roi.height + offsety - i->next_row < tile_height) i->subrect.height = (i->roi.height + offsety - i->next_row) - offsety; else i->subrect.height = tile_height - offsety; i->tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer), gegl_tile_indice (tiledx, tile_width), gegl_tile_indice (tiledy, tile_height), 0); if (i->write && i->subrect.width == tile_width && i->same_format) { gegl_tile_lock (i->tile); } i->data = gegl_tile_get_data (i->tile); { gint bpp = babl_format_get_bytes_per_pixel (i->buffer->soft_format); i->rowstride = bpp * tile_width; i->sub_data = (guchar*)(i->data) + bpp * (i->subrect.y * tile_width + i->subrect.x); } i->col = i->next_col; i->row = i->next_row; i->next_col += tile_width - offsetx; i->roi2.x = i->roi.x + i->col; i->roi2.y = i->roi.y + i->row; i->roi2.width = i->subrect.width; i->roi2.height = i->subrect.height; return TRUE; } } else /* move down to next row */ { gint tiledy; gint offsety; i->row = i->next_row; i->col = i->next_col; tiledy = buffer_y + i->next_row; offsety = gegl_tile_offset (tiledy, tile_height); i->next_row += tile_height - offsety; i->next_col=0; if (i->next_row < i->roi.height) { goto gulp; /* return the first tile in the next row */ } return FALSE; } return FALSE; }