| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com> | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 20:08:29 -04:00
										 |  |  | #include "PGMLoader.h"
 | 
					
						
							|  |  |  | #include "PortableImageLoaderCommon.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  | ErrorOr<void> read_image_data(PGMLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |     TRY(create_bitmap(context)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 20:08:29 -04:00
										 |  |  |     auto& stream = *context.stream; | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |     auto const context_size = context.width * context.height; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |     if (context.type == PGMLoadingContext::Type::ASCII) { | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |         for (u64 i = 0; i < context_size; ++i) { | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |             auto value = TRY(read_number(stream)); | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |             TRY(read_whitespace(context)); | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |             Color color { static_cast<u8>(value), static_cast<u8>(value), static_cast<u8>(value) }; | 
					
						
							|  |  |  |             if (context.format_details.max_val < 255) | 
					
						
							|  |  |  |                 color = adjust_color(context.format_details.max_val, color); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             context.bitmap->set_pixel(i % context.width, i / context.width, color); | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |     } else if (context.type == PGMLoadingContext::Type::RAWBITS) { | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |         for (u64 i = 0; i < context_size; ++i) { | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |             auto const pixel = TRY(stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |             context.bitmap->set_pixel(i % context.width, i / context.width, { pixel, pixel, pixel }); | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     context.state = PGMLoadingContext::State::Bitmap; | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | } | 
					
						
							|  |  |  | } |