mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 21:52:45 +00:00 
			
		
		
		
	 f3ff9c26bc
			
		
	
	
		f3ff9c26bc
		
	
	
	
	
		
			
			This enum used to store very precise state about the decoding process, let's simplify that by only including two steps: HeaderDecoder and BitmapDecoded.
		
			
				
	
	
		
			51 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
	
		
			1.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
 | |
|  * Copyright (c) 2022, the SerenityOS developers.
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include "PPMLoader.h"
 | |
| #include "PortableImageLoaderCommon.h"
 | |
| 
 | |
| namespace Gfx {
 | |
| 
 | |
| ErrorOr<void> read_image_data(PPMLoadingContext& context)
 | |
| {
 | |
|     auto const context_size = context.width * context.height;
 | |
| 
 | |
|     TRY(create_bitmap(context));
 | |
| 
 | |
|     auto& stream = *context.stream;
 | |
| 
 | |
|     if (context.type == PPMLoadingContext::Type::ASCII) {
 | |
|         for (u64 i = 0; i < context_size; ++i) {
 | |
|             auto const red = TRY(read_number(stream));
 | |
|             TRY(read_whitespace(context));
 | |
| 
 | |
|             auto const green = TRY(read_number(stream));
 | |
|             TRY(read_whitespace(context));
 | |
| 
 | |
|             auto const blue = TRY(read_number(stream));
 | |
|             TRY(read_whitespace(context));
 | |
| 
 | |
|             Color color { static_cast<u8>(red), static_cast<u8>(green), static_cast<u8>(blue) };
 | |
|             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);
 | |
|         }
 | |
|     } else if (context.type == PPMLoadingContext::Type::RAWBITS) {
 | |
|         for (u64 i = 0; i < context_size; ++i) {
 | |
|             Array<u8, 3> pixel;
 | |
|             Bytes buffer { pixel };
 | |
| 
 | |
|             TRY(stream.read_until_filled(buffer));
 | |
| 
 | |
|             context.bitmap->set_pixel(i % context.width, i / context.width, { pixel[0], pixel[1], pixel[2] });
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     context.state = PPMLoadingContext::State::BitmapDecoded;
 | |
|     return {};
 | |
| }
 | |
| }
 |