Skip to content

Segmentation fault when composing a larger image of many smaller images #192

@MarcusRiemer

Description

@MarcusRiemer

tl;dr: Clone https://github.com/MarcusRiemer/elixir-tiled, run mix deps.get && mix test test/tiled/map_test.exs --trace and you will probably see a crash.

I have a very much WIP state of a renderer for Tiled .tmj files. These are essentially JSON files that contain instructions on how to compose an image of many small "tiles". The current implementation is very much WIP and not optimized at all, there is no caching and each tile that is to be rendered is effectively a new image created by Image.crop. So this might be related to #191 but so far I could only observe the issue when actively writing the image. Or I am too sleep deprived to recognize ...

Reducing the amount of tiles rendered (or maybe cropped?) prevents the crash, hence the following section:

  def render_layer!(%Vix.Vips.Image{} = map_image, %__MODULE__{} = map, %TileLayer{} = layer) do
    layer.data
    |> Enum.with_index()
    |> Enum.filter(fn {tile, _coord_index} -> tile > 0 end)
    # |> Enum.take(50) # Rendering more than 50 tiles on a single layer crashes the phoenix server on the second rendering of a controller
    |> Enum.reduce(map_image, fn {tile, coord_index}, %Vix.Vips.Image{} = accu_image ->
      {render_x, render_y} = Tiled.Coordinate.point_from_index(map, layer.width, coord_index)
      {:ok, rendered_tile_image} = tile_image(map, tile)

      Image.compose!(accu_image, rendered_tile_image, x: render_x, y: render_y)
    end)
  end

So I copied together the code from the private project, published it and recognized it's well past midnight where I live, so I will leave it at that for the moment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions