static GeglTile * get_tile (GeglTileSource *gegl_tile_source, gint x, gint y, gint z) { GeglTileSource *source = ((GeglTileHandler *) gegl_tile_source)->source; GeglTileHandlerEmpty *empty = (GeglTileHandlerEmpty *) gegl_tile_source; GeglTile *tile = NULL; if (source) tile = gegl_tile_source_get_tile (source, x, y, z); if (tile) return tile; if (G_UNLIKELY(!empty->tile)) { gint tile_size = gegl_tile_backend_get_tile_size (empty->backend); empty->tile = gegl_tile_new (tile_size); memset (gegl_tile_get_data (empty->tile), 0x00, tile_size); empty->tile->is_zero_tile = 1; } return gegl_tile_handler_dup_tile (GEGL_TILE_HANDLER (empty), empty->tile, x, y, z); }
int main(int argc, char **argv) { GeglTile *tile; GeglBuffer *buf_a, *buf_b, *buf_small_lin, *buf_big_lin; gpointer shared_data = NULL; gboolean result = TRUE; gpointer scratch_data; GeglRectangle buffer_rect = *GEGL_RECTANGLE(0, 0, 128, 128); gegl_init (&argc, &argv); buf_a = gegl_buffer_new (&buffer_rect, babl_format("RGBA u8")); buf_b = gegl_buffer_new (&buffer_rect, babl_format("RGBA u8")); buf_small_lin = gegl_buffer_linear_new (&buffer_rect, babl_format("RGBA float")); buf_big_lin = gegl_buffer_linear_new (GEGL_RECTANGLE(0, 0, 1024, 1024), babl_format("RGBA float")); tile = gegl_tile_source_get_tile (GEGL_TILE_SOURCE (buf_a), 0, 0, 0); shared_data = gegl_tile_get_data(tile); gegl_tile_unref (tile); if (!assert_is_empty (buf_a, 0, 0, shared_data)) result = FALSE; if (!assert_is_empty (buf_b, 0, 1, shared_data)) result = FALSE; if (!assert_is_empty (buf_a, 0, 0, shared_data)) result = FALSE; if (!assert_is_empty (buf_b, 0, 1, shared_data)) result = FALSE; if (!assert_is_empty (buf_small_lin, 0, 0, shared_data)) result = FALSE; if (!assert_is_unshared (buf_big_lin, 0, 0, shared_data)) result = FALSE; scratch_data = gegl_malloc(4 * buffer_rect.width * buffer_rect.height); gegl_buffer_get (buf_a, &buffer_rect, 1.0, babl_format("RGBA u8"), scratch_data, 0, GEGL_ABYSS_NONE); gegl_buffer_get (buf_b, &buffer_rect, 1.0, babl_format("RGBA u8"), scratch_data, 0, GEGL_ABYSS_NONE); gegl_buffer_get (buf_small_lin, &buffer_rect, 1.0, babl_format("RGBA u8"), scratch_data, 0, GEGL_ABYSS_NONE); gegl_buffer_get (buf_big_lin, &buffer_rect, 1.0, babl_format("RGBA u8"), scratch_data, 0, GEGL_ABYSS_NONE); gegl_free (scratch_data); g_object_unref(buf_a); g_object_unref(buf_b); g_object_unref(buf_small_lin); g_object_unref(buf_big_lin); gegl_exit(); if (result) return SUCCESS; return FAILURE; }
/* 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; }
static gboolean assert_is_empty (GeglBuffer *buf, gint x, gint y, gpointer shared_data) { gboolean result = TRUE; GeglTile *tile = gegl_tile_source_get_tile (GEGL_TILE_SOURCE (buf), x, y, 0); if (!tile->is_zero_tile) { g_warning ("Not a zero tile"); result = FALSE; } if (gegl_tile_get_data(tile) != shared_data) { g_warning ("Not using shared data"); result = FALSE; } gegl_tile_unref (tile); return result; }
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; }
/* 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; }
static void inline gegl_sampler_get_pixel (GeglSampler *sampler, gint x, gint y, gpointer data, GeglAbyssPolicy repeat_mode) { GeglSamplerNearest *nearest_sampler = (GeglSamplerNearest*)(sampler); GeglBuffer *buffer = sampler->buffer; const GeglRectangle *abyss = &buffer->abyss; guchar *buf = data; if (y < abyss->y || x < abyss->x || y >= abyss->y + abyss->height || x >= abyss->x + abyss->width) { switch (repeat_mode) { case GEGL_ABYSS_CLAMP: x = CLAMP (x, abyss->x, abyss->x+abyss->width-1); y = CLAMP (y, abyss->y, abyss->y+abyss->height-1); break; case GEGL_ABYSS_LOOP: x = abyss->x + GEGL_REMAINDER (x - abyss->x, abyss->width); y = abyss->y + GEGL_REMAINDER (y - abyss->y, abyss->height); break; case GEGL_ABYSS_BLACK: { gfloat color[4] = {0.0, 0.0, 0.0, 1.0}; babl_process (babl_fish (gegl_babl_rgba_linear_float (), sampler->format), color, buf, 1); return; } case GEGL_ABYSS_WHITE: { gfloat color[4] = {1.0, 1.0, 1.0, 1.0}; babl_process (babl_fish (gegl_babl_rgba_linear_float (), sampler->format), color, buf, 1); return; } default: case GEGL_ABYSS_NONE: memset (buf, 0x00, babl_format_get_bytes_per_pixel (sampler->format)); return; } } gegl_buffer_lock (sampler->buffer); { gint tile_width = buffer->tile_width; gint tile_height = buffer->tile_height; gint tiledy = y + buffer->shift_y; gint tiledx = x + buffer->shift_x; gint indice_x = gegl_tile_indice (tiledx, tile_width); gint indice_y = gegl_tile_indice (tiledy, tile_height); GeglTile *tile = nearest_sampler->hot_tile; if (!(tile && tile->x == indice_x && tile->y == indice_y)) { if (gegl_config_threads()>1) g_rec_mutex_lock (&buffer->tile_storage->mutex); if (tile) gegl_tile_unref (tile); tile = gegl_tile_source_get_tile ((GeglTileSource *) (buffer), indice_x, indice_y, 0); nearest_sampler->hot_tile = tile; if (gegl_config_threads()>1) g_rec_mutex_unlock (&buffer->tile_storage->mutex); } if (tile) { gint tile_origin_x = indice_x * tile_width; gint tile_origin_y = indice_y * tile_height; gint offsetx = tiledx - tile_origin_x; gint offsety = tiledy - tile_origin_y; guchar *tp = gegl_tile_get_data (tile) + (offsety * tile_width + offsetx) * nearest_sampler->buffer_bpp; babl_process (sampler->fish, tp, buf, 1); } } gegl_buffer_unlock (sampler->buffer); }