| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  | // Aseprite Document Library
 | 
					
						
							|  |  |  | // Copyright (c) 2001-2015 David Capello
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // This file is released under the terms of the MIT license.
 | 
					
						
							|  |  |  | // Read LICENSE.txt for more information.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include "config.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "doc/remap.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 23:20:58 +08:00
										 |  |  | #include "doc/palette_picks.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  | namespace doc { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-10 02:57:46 +08:00
										 |  |  | Remap create_remap_to_move_picks(const PalettePicks& picks, int beforeIndex) | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-09 23:20:58 +08:00
										 |  |  |   Remap map(picks.size()); | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   int selectedTotal = 0; | 
					
						
							|  |  |  |   int selectedBeforeIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int i=0; i<map.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2015-05-09 23:20:58 +08:00
										 |  |  |     if (picks[i]) { | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  |       ++selectedTotal; | 
					
						
							|  |  |  |       if (i < beforeIndex) | 
					
						
							|  |  |  |         ++selectedBeforeIndex; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (int i=0, j=0, k=0; i<map.size(); ++i) { | 
					
						
							|  |  |  |     if (k == beforeIndex - selectedBeforeIndex) | 
					
						
							|  |  |  |       k += selectedTotal; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-09 23:20:58 +08:00
										 |  |  |     if (picks[i]) { | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  |       map.map(i, beforeIndex - selectedBeforeIndex + j); | 
					
						
							|  |  |  |       ++j; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |       map.map(i, k++); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return map; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-10 02:57:46 +08:00
										 |  |  | Remap create_remap_to_expand_palette(int size, int count, int beforeIndex) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Remap map(size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int j, k = 0; | 
					
						
							|  |  |  |   for (int i=0; i<size; ++i) { | 
					
						
							|  |  |  |     if (i < beforeIndex) | 
					
						
							|  |  |  |       j = i; | 
					
						
							|  |  |  |     else if (i + count < size) | 
					
						
							|  |  |  |       j = i + count; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       j = beforeIndex + (k++); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     map.map(i, j); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return map; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 23:57:59 +08:00
										 |  |  | void Remap::merge(const Remap& other) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   for (int i=0; i<size(); ++i) { | 
					
						
							|  |  |  |     m_map[i] = other[m_map[i]]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Remap Remap::invert() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   Remap inv(size()); | 
					
						
							|  |  |  |   for (int i=0; i<size(); ++i) | 
					
						
							|  |  |  |     inv.map(operator[](i), i); | 
					
						
							|  |  |  |   return inv; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-07 03:15:54 +08:00
										 |  |  | bool Remap::isFor8bit() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   for (int i=0; i<size(); ++i) { | 
					
						
							|  |  |  |     // Moving entries between [0,255] range to or from [256,+inf)
 | 
					
						
							|  |  |  |     // range are invalid for 8-bit images.
 | 
					
						
							|  |  |  |     if ((i <  256 && m_map[i] >= 256) || | 
					
						
							|  |  |  |         (i >= 256 && m_map[i] <  256)) | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 23:25:32 +08:00
										 |  |  | } // namespace doc
 |