@@ -278,15 +278,6 @@ local render = function(image)
278
278
279
279
-- compute final geometry and prevent useless rerendering
280
280
local rendered_geometry = { x = absolute_x , y = absolute_y , width = width , height = height }
281
- -- if
282
- -- image.is_rendered
283
- -- and image.rendered_geometry.x == rendered_geometry.x
284
- -- and image.rendered_geometry.y == rendered_geometry.y
285
- -- and image.rendered_geometry.width == rendered_geometry.width
286
- -- and image.rendered_geometry.height == rendered_geometry.height
287
- -- then
288
- -- return true
289
- -- end
290
281
291
282
-- handle crop/resize
292
283
local pixel_width = width * term_size .cell_width
@@ -295,23 +286,23 @@ local render = function(image)
295
286
local cropped_pixel_height = height * term_size .cell_height
296
287
local needs_crop = false
297
288
local needs_resize = false
289
+ local initial_crop_hash = image .crop_hash
290
+ local initial_resize_hash = image .resize_hash
298
291
299
292
-- compute crop top/bottom
300
- if not state .backend .features .crop then
301
- -- crop top
302
- if absolute_y < bounds .top then
303
- local visible_rows = height - (bounds .top - absolute_y )
304
- cropped_pixel_height = visible_rows * term_size .cell_height
305
- crop_offset_top = (bounds .top - absolute_y ) * term_size .cell_height
306
- absolute_y = bounds .top
307
- needs_crop = true
308
- end
293
+ -- crop top
294
+ if absolute_y < bounds .top then
295
+ local visible_rows = height - (bounds .top - absolute_y )
296
+ cropped_pixel_height = visible_rows * term_size .cell_height
297
+ crop_offset_top = (bounds .top - absolute_y ) * term_size .cell_height
298
+ if not state .backend .features .crop then absolute_y = bounds .top end
299
+ needs_crop = true
300
+ end
309
301
310
- -- crop bottom
311
- if absolute_y + height > bounds .bottom then
312
- cropped_pixel_height = (bounds .bottom - absolute_y + 1 ) * term_size .cell_height
313
- needs_crop = true
314
- end
302
+ -- crop bottom
303
+ if absolute_y + height > bounds .bottom then
304
+ cropped_pixel_height = (bounds .bottom - absolute_y + 1 ) * term_size .cell_height
305
+ needs_crop = true
315
306
end
316
307
317
308
-- compute resize
@@ -343,26 +334,49 @@ local render = function(image)
343
334
end
344
335
345
336
-- crop
337
+ local crop_hash = (" %d-%d-%d-%d" ):format (0 , crop_offset_top , pixel_width , cropped_pixel_height )
346
338
if needs_crop then
347
- local crop_hash = (" %d-%d-%d-%d" ):format (0 , crop_offset_top , pixel_width , cropped_pixel_height )
348
339
if (needs_resize and image .resize_hash ~= resize_hash ) or image .crop_hash ~= crop_hash then
349
- local cropped_image = magick .load_image (image .resized_path or image .path )
350
- cropped_image :set_format (" png" )
351
-
352
- -- utils.debug(("cropping image %s to %dx%d"):format(image.path, pixel_width, cropped_pixel_height))
353
- cropped_image :crop (pixel_width , cropped_pixel_height , 0 , crop_offset_top )
354
- local tmp_path = state .tmp_dir .. " /" .. utils .base64 .encode (image .id ) .. " -cropped.png"
355
- cropped_image :write (tmp_path )
356
- cropped_image :destroy ()
357
-
358
- image .cropped_path = tmp_path
340
+ if not state .backend .features .crop then
341
+ local cropped_image = magick .load_image (image .resized_path or image .path )
342
+ cropped_image :set_format (" png" )
343
+
344
+ -- utils.debug(("cropping image %s to %dx%d"):format(image.path, pixel_width, cropped_pixel_height))
345
+ cropped_image :crop (pixel_width , cropped_pixel_height , 0 , crop_offset_top )
346
+ local tmp_path = state .tmp_dir .. " /" .. utils .base64 .encode (image .id ) .. " -cropped.png"
347
+ cropped_image :write (tmp_path )
348
+ cropped_image :destroy ()
349
+ image .cropped_path = tmp_path
350
+ end
359
351
image .crop_hash = crop_hash
360
352
end
361
353
else
362
354
image .cropped_path = image .resized_path
363
355
image .crop_hash = nil
364
356
end
365
357
358
+ if
359
+ image .is_rendered
360
+ and image .rendered_geometry .x == rendered_geometry .x
361
+ and image .rendered_geometry .y == rendered_geometry .y
362
+ and image .rendered_geometry .width == rendered_geometry .width
363
+ and image .rendered_geometry .height == rendered_geometry .height
364
+ and image .crop_hash == initial_crop_hash
365
+ and image .resize_hash == initial_resize_hash
366
+ then
367
+ -- utils.debug("skipping render", image.id)
368
+ return true
369
+ end
370
+
371
+ -- utils.debug("redering to backend", image.id, {
372
+ -- x = absolute_x,
373
+ -- y = absolute_y,
374
+ -- width = width,
375
+ -- height = height,
376
+ -- resize_hash = image.resize_hash,
377
+ -- crop_hash = image.crop_hash,
378
+ -- })
379
+
366
380
image .bounds = bounds
367
381
state .backend .render (image , absolute_x , absolute_y , width , height )
368
382
image .rendered_geometry = rendered_geometry
0 commit comments