Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with files #30

Open
bararchy opened this issue Jun 14, 2020 · 1 comment
Open

Working with files #30

bararchy opened this issue Jun 14, 2020 · 1 comment

Comments

@bararchy
Copy link

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
@l3kn
Copy link
Collaborator

l3kn commented Jun 14, 2020

This might be caused by https://github.com/stumpycr/stumpy_core/blob/master/src/stumpy_core/canvas.cr#L41 which allocates one large slice per image, I'm not sure where the "repeated allocation" part comes in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants