Skip to content

Commit

Permalink
Merge pull request #884 from WolframResearch/bugfix/rasterize-memory-…
Browse files Browse the repository at this point in the history
…error

Bugfix: Handle `Rasterize::bigraster` errors for large cells
  • Loading branch information
rhennigan authored Nov 4, 2024
2 parents efd6bce + 598f438 commit e202386
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Source/Chatbook/Serialization.wl
Original file line number Diff line number Diff line change
Expand Up @@ -1065,14 +1065,15 @@ $graphicsBoxStringReplacements = {
toMarkdownImageBox // beginDefinition;

toMarkdownImageBox[ graphics_ ] := Enclose[
Module[ { img, uri },
img = ConfirmBy[ rasterizeGraphics @ graphics, ImageQ, "RasterizeGraphics" ];
uri = ConfirmBy[ MakeExpressionURI[ "image", img ], StringQ, "RasterID" ];
Catch @ Module[ { img, uri },
img = ConfirmMatch[ rasterizeGraphics @ graphics, _Image | Missing[ "OutOfMemory" ], "RasterizeGraphics" ];
If[ MissingQ @ img, Block[ { $multiModalImages = False }, Throw @ fasterCellToString0 @ graphics ] ];
uri = ConfirmBy[ MakeExpressionURI[ "image", img ], StringQ, "RasterID" ];
needsBasePrompt[ "MarkdownImageBox" ];
If[ toolSelectedQ[ "WolframLanguageEvaluator" ], needsBasePrompt[ "MarkdownImageBoxImporting" ] ];
"\\!\\(\\*MarkdownImageBox[\"" <> uri <> "\"]\\)"
],
throwInternalFailure[ toMarkdownImageBox @ graphics, ## ] &
throwInternalFailure
];

toMarkdownImageBox // endDefinition;
Expand All @@ -1081,14 +1082,26 @@ toMarkdownImageBox // endDefinition;
(* ::Subsubsubsubsection::Closed:: *)
(*rasterizeGraphics*)
rasterizeGraphics // beginDefinition;
rasterizeGraphics[ gfx: $$graphicsBox ] := rasterizeGraphics[ gfx ] = rasterize @ RawBoxes @ gfx;
rasterizeGraphics[ gfx: $$graphicsBox ] := rasterizeGraphics[ gfx ] = checkedRasterize @ RawBoxes @ gfx;
rasterizeGraphics[ cell_Cell ] := rasterizeGraphics[ cell, 6.25*$cellPageWidth ];

rasterizeGraphics[ cell_Cell, width_Real ] := rasterizeGraphics[ cell, width ] =
rasterize @ Append[ cell, PageWidth -> width ];
checkedRasterize @ Append[ cell, PageWidth -> width ];

rasterizeGraphics // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsubsection::Closed:: *)
(*checkedRasterize*)
checkedRasterize // beginDefinition;

checkedRasterize[ expr_ ] := Quiet[
Check[ rasterize @ RawBoxes @ expr, Missing[ "OutOfMemory" ], Rasterize::bigraster ],
Rasterize::bigraster
];

checkedRasterize // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*Video*)
Expand Down

0 comments on commit e202386

Please sign in to comment.