1 {
2 Vampyre Imaging Library
3 by Marek Mauder
4 http://imaginglib.sourceforge.net
6 The contents of this file are used with permission, subject to the Mozilla
7 Public License Version 1.1 (the "License"); you may not use this file except
8 in compliance with the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/MPL-1.1.html
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 the specific language governing rights and limitations under the License.
15 Alternatively, the contents of this file may be used under the terms of the
16 GNU Lesser General Public License (the "LGPL License"), in which case the
17 provisions of the LGPL License are applicable instead of those above.
18 If you wish to allow use of your version of this file only under the terms
19 of the LGPL License and not to allow others to use your version of this file
20 under the MPL, indicate your decision by deleting the provisions above and
21 replace them with the notice and other provisions required by the LGPL
22 License. If you do not delete the provisions above, a recipient may use
23 your version of this file under either the MPL or the LGPL License.
25 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
26 }
28 { This unit contains image format loader/saver for Targa images.}
31 {$I ImagingOptions.inc}
33 interface
35 uses
38 type
39 { Class for loading and saving Truevision Targa images.
40 It can load/save 8bit indexed or grayscale, 16 bit RGB or grayscale,
41 24 bit RGB and 32 bit ARGB images with or without RLE compression.}
43 protected
52 public
54 published
55 { Controls that RLE compression is used during saving. Accessible trough
56 ImagingTargaRLE option.}
60 implementation
62 const
69 const
72 type
73 { Targa file header.}
89 { Footer at the end of TGA file.}
99 { TTargaFileFormat class implementation }
102 begin
116 var
126 var
131 begin
133 begin
134 // Alocates buffer large enough to hold the worst case
135 // RLE compressed data and reads then from input
147 begin
151 begin
152 // Process uncompressed pixel
156 begin
157 // Copy pixel from src to dest
167 end
168 else
169 begin
170 // Process compressed pixels
173 // Copy one pixel from src to dest (many times there)
175 begin
187 // set position in source to real end of compressed data
189 smFromCurrent);
194 begin
197 begin
198 // Read targa header
206 // Skip image ID info
208 // Determine image format
220 // Format was not assigned by previous testing (it should be in
221 // well formed targas), so formats which reflects bit dept are selected
234 begin
235 // Read palette
238 try
240 // Process palette
247 begin
253 // I've never seen tga with these palettes so they are untested
256 begin
264 begin
271 finally
278 // Load uncompressed mode images
281 // Load RLE compressed mode images
282 LoadRLE;
285 // Check if there is alpha channel present in A1R5GB5 images, if it is not
286 // change format to X1R5G5B5
288 begin
293 // We must find true end of file and set input' position to it
294 // paint programs appends extra info at the end of Targas
295 // some of them multiple times (PSP Pro 8)
296 repeat
301 begin
302 // 495 = size of Extension Area
304 begin
307 end
308 else
313 begin
316 else
321 // Some editors save targas flipped
331 var
340 var
345 var
349 begin
354 begin
356 Exit;
365 begin
374 Break;
380 Result := N
381 else
386 var
390 begin
402 begin
411 Break;
420 const
422 var
426 begin
429 begin
437 begin
447 begin
466 begin
468 begin
469 // Allocate enough space to hold the worst case compression
470 // result and then compress source's scanlines
476 try
478 begin
484 finally
490 begin
494 try
496 // Fill targa header
506 // This indicates that targa is stored in top-left format
507 // as our images -> no flipping is needed.
509 // Set alpha channel size in descriptor (mostly ignored by other software though)
515 // Choose image type
518 else
521 else
526 // Write palette
528 begin
530 try
533 begin
539 finally
545 // Save rle compressed mode images
546 SaveRLE
547 else
548 // Save uncompressed mode images
552 finally
560 var
562 begin
564 // Convert all grayscale images to Gray8 (preserve alpha of AxGrayx formats)
567 // Convert all indexed images to Index8
568 ConvFormat := ifIndex8
570 // Convert images with alpha channel to A8R8G8B8
571 ConvFormat := ifA8R8G8B8
573 // Convert 16bit images (without alpha channel) to A1R5G5B5
574 ConvFormat := ifA1R5G5B5
575 else
576 // Convert all other formats to R8G8B8
583 var
586 begin
589 begin
599 initialization
602 {
603 File Notes:
605 -- TODOS ----------------------------------------------------
606 - nothing now
608 -- 0.21 Changes/Bug Fixes -----------------------------------
609 - MakeCompatible method moved to base class, put ConvertToSupported here.
610 GetSupportedFormats removed, it is now set in constructor.
611 - Made public properties for options registered to SetOption/GetOption
612 functions.
613 - Changed extensions to filename masks.
614 - Changed SaveData, LoadData, and MakeCompatible methods according
615 to changes in base class in Imaging unit.
617 -- 0.17 Changes/Bug Fixes -----------------------------------
618 - 16 bit images are usually without alpha but some has alpha
619 channel and there is no indication of it - so I have added
620 a check: if all pixels of image are with alpha = 0 image is treated
621 as X1R5G5B5 otherwise as A1R5G5B5
622 - fixed problems with some nonstandard 15 bit images
623 }