| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 15:11:15 +02:00
										 |  |  | #include <AK/Endian.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:58:06 -04:00
										 |  |  | #include <LibGfx/ImageFormats/PGMLoader.h>
 | 
					
						
							|  |  |  | #include <LibGfx/ImageFormats/PortableImageLoaderCommon.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Streamer.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Gfx { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  | static void set_adjusted_pixels(PGMLoadingContext& context, Vector<Gfx::Color> const& color_data) | 
					
						
							| 
									
										
										
										
											2021-01-05 15:14:29 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     size_t index = 0; | 
					
						
							|  |  |  |     for (size_t y = 0; y < context.height; ++y) { | 
					
						
							|  |  |  |         for (size_t x = 0; x < context.width; ++x) { | 
					
						
							|  |  |  |             Color color = color_data.at(index); | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |             if (context.format_details.max_val < 255) { | 
					
						
							|  |  |  |                 color = adjust_color(context.format_details.max_val, color); | 
					
						
							| 
									
										
										
										
											2021-01-05 15:14:29 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |             context.bitmap->set_pixel(x, y, color); | 
					
						
							|  |  |  |             ++index; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 11:58:58 -06:00
										 |  |  | bool read_image_data(PGMLoadingContext& context, Streamer& streamer) | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | { | 
					
						
							|  |  |  |     Vector<Gfx::Color> color_data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |     if (context.type == PGMLoadingContext::Type::ASCII) { | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |         u16 value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (true) { | 
					
						
							|  |  |  |             if (!read_number(streamer, &value)) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 09:59:22 +02:00
										 |  |  |             if (!read_whitespace(context, streamer)) | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             color_data.append({ (u8)value, (u8)value, (u8)value }); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-03-12 11:16:30 -07:00
										 |  |  |     } else if (context.type == PGMLoadingContext::Type::RAWBITS) { | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  |         u8 pixel; | 
					
						
							|  |  |  |         while (streamer.read(pixel)) { | 
					
						
							|  |  |  |             color_data.append({ pixel, pixel, pixel }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 15:14:29 +01:00
										 |  |  |     size_t context_size = (u32)context.width * (u32)context.height; | 
					
						
							|  |  |  |     if (context_size != color_data.size()) { | 
					
						
							|  |  |  |         dbgln("Not enough color data in image."); | 
					
						
							| 
									
										
										
										
											2020-12-20 16:04:29 +01:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-05 15:14:29 +01:00
										 |  |  |     if (!create_bitmap(context)) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-20 10:19:03 -07:00
										 |  |  |     set_adjusted_pixels(context, color_data); | 
					
						
							| 
									
										
										
										
											2020-06-22 15:19:57 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     context.state = PGMLoadingContext::State::Bitmap; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | } |