<commit_before>//  PlantUML Editor
//  Copyright 2013 <NAME> - <EMAIL>
//  Copyright 2010 <NAME> - http://omaralzabir.com/ (original author)
// 
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PlantUmlEditor.Core.Imaging
{
	/// <summary>
	/// Renders diagram images to bitmaps.
	/// </summary>
	public class BitmapRenderer : IDiagramRenderer
	{
		/// <see cref="IDiagramRenderer.Render(Diagram)"/>
		public ImageSource Render(Diagram diagram)
		{
			Uri imageUri;
			if (!String.IsNullOrEmpty(diagram.ImageFilePath) && Uri.TryCreate(diagram.ImageFilePath, UriKind.RelativeOrAbsolute, out imageUri))
			{
				var bitmap = new BitmapImage();
				bitmap.BeginInit();
				bitmap.UriSource = imageUri;

				// OMAR: Trick #6
				// Unless we use this option, the image file is locked and cannot be modified.
				// Looks like WPF holds read lock on the images. Very bad.
				bitmap.CacheOption = BitmapCacheOption.OnLoad;

				// Unless we use this option, an image cannot be refreshed. It loads from 
				// cache. Looks like WPF caches every image it loads in memory. Very bad.
				bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

				try
				{
					bitmap.EndInit();
					return bitmap;
				}
				catch (Exception e)
				{
					Debug.WriteLine(e.Message);
				}
			}

			return null;
		}

		/// <see cref="IDiagramRenderer.Render(Stream)"/>
		public ImageSource Render(Stream imageData)
		{
			var bitmap = new BitmapImage();
			bitmap.BeginInit();
			bitmap.StreamSource = imageData;
			bitmap.EndInit();
			bitmap.Freeze();
			return bitmap;
		}
	}
}<commit_msg>Removed try/catch hack for non-existent image files and perform an explicit check instead.<commit_after>//  PlantUML Editor
//  Copyright 2013 <NAME> - <EMAIL>
//  Copyright 2010 <NAME> - http://omaralzabir.com/ (original author)
// 
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

using System;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace PlantUmlEditor.Core.Imaging
{
	/// <summary>
	/// Renders bitmap diagram images.
	/// </summary>
	public class BitmapRenderer : IDiagramRenderer
	{
		/// <see cref="IDiagramRenderer.Render(Diagram)"/>
		public ImageSource Render(Diagram diagram)
		{
			Uri imageUri;
			if (String.IsNullOrEmpty(diagram.ImageFilePath) || !File.Exists(diagram.ImageFilePath) ||
			    !Uri.TryCreate(diagram.ImageFilePath, UriKind.RelativeOrAbsolute, out imageUri))
			{
				return null;
			}

			var bitmap = new BitmapImage();
			bitmap.BeginInit();
			bitmap.UriSource = imageUri;

			// OMAR: Trick #6
			// Unless we use this option, the image file is locked and cannot be modified.
			// Looks like WPF holds read lock on the images. Very bad.
			bitmap.CacheOption = BitmapCacheOption.OnLoad;

			// Unless we use this option, an image cannot be refreshed. It loads from 
			// cache. Looks like WPF caches every image it loads in memory. Very bad.
			bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache;

			bitmap.EndInit();
			return bitmap;
		}

		/// <see cref="IDiagramRenderer.Render(Stream)"/>
		public ImageSource Render(Stream imageData)
		{
			var bitmap = new BitmapImage();
			bitmap.BeginInit();
			bitmap.StreamSource = imageData;
			bitmap.EndInit();
			bitmap.Freeze();
			return bitmap;
		}
	}
}
{'fraction_non_alphanumeric': 0.10465901417960838, 'fraction_numerical': 0.005851901868107134, 'mean_word_length': 3.2243346007604563, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4901070', 'n_tokens_mistral': 1467, 'n_tokens_neox': 1306, 'n_words': 449}