Skip to content

Working with files #30

@bararchy

Description

@bararchy

I've noticed that when working on images, I get those errors quite alot, it might be because the way we handle things are not optimal.

GC Warning: Repeated allocation of very large block (appr. size 1052672):
        May lead to memory leak and poor performance
GC Warning: Repeated allocation of very large block (appr. size 409600):
        May lead to memory leak and poor performance
GC Warning: Repeated allocation of very large block (appr. size 503808):
        May lead to memory leak and poor performance
GC Warning: Repeated allocation of very large block (appr. size 692224):
        May lead to memory leak and poor performance

Any clue if the below can be done better?

temp = IO::Memory.new(image_file)
        canvas = StumpyPNG.read(temp)
        temp.close

        (0...canvas.height).each do |y| # make it darker
          (0...canvas.width).each do |x|
            curr = canvas[x, y]
            canvas[x, y] = StumpyPNG::RGBA.from_rgb_n(
              curr.r * 0.549,
              curr.g * 0.549,
              curr.b * 0.549,
              16
            )
          end
        end

        alert = StumpyPNG.read("assets/alert.png")

        alert_y = Math.max(0, canvas.height * 0.365 - alert.height // 2).to_i
        alert_x = (canvas.width - alert.width) // 2.to_i

        alert_offset = (Math.min(canvas.width, alert.width) - alert.width) // 2.to_i

        (0...Math.min(canvas.height, alert.height)).each do |y| # paste in alert
          (alert_offset...alert.width - alert_offset).each do |x|
            canvas[alert_x + x, alert_y + y] = alert[x, y]
          end
        end

        font = PCFParser::Font.from_file("assets/font.pcf")

        canvas.text(
          alert_x + (alert.width - 8 * alert_text.size) // 2,
          alert_y + alert.height // 3 + 4,
          alert_text,
          font
        ) # print alert text

        temp = IO::Memory.new
        StumpyPNG.write(canvas, temp, color_type: :rgb)
        data = temp.to_slice
        temp.close
        data

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions