1 {
2 $Id: ImagingXpm.pas 171 2009-09-02 01:34:19Z galfar $
3 Vampyre Imaging Library
4 by Marek Mauder
5 http://imaginglib.sourceforge.net
7 The contents of this file are used with permission, subject to the Mozilla
8 Public License Version 1.1 (the "License"); you may not use this file except
9 in compliance with the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/MPL-1.1.html
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 the specific language governing rights and limitations under the License.
16 Alternatively, the contents of this file may be used under the terms of the
17 GNU Lesser General Public License (the "LGPL License"), in which case the
18 provisions of the LGPL License are applicable instead of those above.
19 If you wish to allow use of your version of this file only under the terms
20 of the LGPL License and not to allow others to use your version of this file
21 under the MPL, indicate your decision by deleting the provisions above and
22 replace them with the notice and other provisions required by the LGPL
23 License. If you do not delete the provisions above, a recipient may use
24 your version of this file under either the MPL or the LGPL License.
26 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
27 }
29 { This unit contains image format loader for X Window Pixmap images. }
31 {$I ImagingOptions.inc}
33 interface
35 uses
39 type
40 { Class for loading X Window Pixmap images known as XPM.
41 It is ASCII-text-based format, basicaly a fragment of C code
42 declaring static array. Loaded image is in ifA8R8G8B8 data format.
43 Loading as well as saving is supported now. }
45 protected
52 public
57 implementation
59 const
64 const
68 const
71 type
73 public
92 { Simple color-string hash table for faster than linear searches
93 during XPM saving. }
95 private
102 public
111 { TSimpleBucketList }
114 begin
119 var
121 begin
128 var
130 begin
136 begin
137 // Skip empty buckets
139 begin
153 var
156 begin
163 begin
166 Break;
171 var
173 begin
176 begin
179 begin
182 else
188 begin
198 var
200 begin
204 {
205 TXPMFileFormat implementation
206 }
209 begin
222 var
228 begin
234 begin
238 begin
246 begin
251 var
253 begin
255 try
262 except
268 var
270 begin
273 Result := pcClear
275 Result := pcBlack
277 Result := pcBlue
279 Result := pcGreen
281 Result := pcAqua
283 Result := pcRed
285 Result := pcFuchsia
287 Result := pcYellow
289 Result := pcWhite
291 Result := pcLtGray
293 Result := pcNavy
295 Result := pcGreen
297 Result := pcTeal
299 Result := pcMaroon
301 Result := pcPurple
303 Result := pcOlive
305 Result := pcMaroon
307 Result := pcOlive
309 Result := pcNavy
311 Result := pcPurple
313 Result := pcTeal
315 Result := pcSilver
317 Result := pcLime
319 Result := pcFuchsia
321 Result := pcAqua
322 else
327 var
332 begin
334 begin
336 // Parse pixel code and color
342 // Convert color from hex number or named constant
344 begin
347 end
348 else
350 // Store code and color in table for later lookup
358 var
362 begin
365 begin
368 begin
369 // Read code and look up color in the palette
373 else
380 begin
384 begin
385 // Look up table for XPM palette entries
389 // Read whole file and assign it to string list
394 // Remove quotes and other stuff
396 begin
400 else
403 // Parse header and create new image
405 Exit;
407 // Read palette entries and assign colors to pixels
408 ParsePalette;
409 ParsePixels;
421 const
423 ColorChars = ' .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZASDFGHJKLPIUYTREWQ!~^/()_`''][{}|';
424 var
437 var
439 begin
442 begin
450 var
453 begin
456 begin
460 begin
468 begin
476 try
477 // Put all unique colors of image to table
479 // Compute the character per pixel
483 begin
487 // Assign char id to each color
488 MakeStrIdsForColors;
490 // Start writing XPM file
498 // Write 'colors' part of XPM file
500 begin
504 else
510 // Write pixels - for aech pixel of image find its char id
511 // and append it to line
514 begin
517 begin
529 // Finally save strings to stream and write stream's data to output
530 // (we could directly write lines from list to output, but stream method
531 // takes care of D2009+ Unicode strings).
536 finally
547 begin
552 var
555 begin
558 begin
565 initialization
569 {
570 File Notes:
572 -- TODOS ----------------------------------------------------
573 - nothing now
575 -- 0.26.3 Changes/Bug Fixes -----------------------------------
576 - Added XPM saving.
578 -- 0.25.0 Changes/Bug Fixes -----------------------------------
579 - Added XPM loading.
580 - Unit created.
581 }