| 
									
										
										
										
											2020-06-21 12:00:22 +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-21 12:00:22 +03:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "PBMLoader.h"
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:19:03 -07:00
										 |  |  | #include "PortableImageLoaderCommon.h"
 | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  | ErrorOr<void> read_image_data(PBMLoadingContext& context) | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +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; | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 == PBMLoadingContext::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 const byte = TRY(stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |             if (byte == '0') | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |                 context.bitmap->set_pixel(i % context.width, i / context.width, Color::White); | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |             else if (byte == '1') | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |                 context.bitmap->set_pixel(i % context.width, i / context.width, Color::Black); | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |             else | 
					
						
							|  |  |  |                 i--; | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |     } else if (context.type == PBMLoadingContext::Type::RAWBITS) { | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |         for (u64 color_index = 0; color_index < context_size;) { | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |             auto byte = TRY(stream.read_value<u8>()); | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  |             for (int i = 0; i < 8; i++) { | 
					
						
							| 
									
										
										
										
											2023-03-12 20:08:29 -04:00
										 |  |  |                 auto const val = byte & 0x80; | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |                 if (val == 0) | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |                     context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::White); | 
					
						
							| 
									
										
										
										
											2023-03-16 20:17:14 -04:00
										 |  |  |                 else | 
					
						
							| 
									
										
										
										
											2023-06-06 11:59:38 -04:00
										 |  |  |                     context.bitmap->set_pixel(color_index % context.width, color_index / context.width, Color::Black); | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 byte = byte << 1; | 
					
						
							|  |  |  |                 color_index++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (color_index % context.width == 0) { | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-10 16:41:42 -04:00
										 |  |  |     context.state = PBMLoadingContext::State::BitmapDecoded; | 
					
						
							| 
									
										
										
										
											2023-03-12 22:55:47 -04:00
										 |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2020-06-21 12:00:22 +03:00
										 |  |  | } | 
					
						
							|  |  |  | } |