5103f78e5e165fc80731a4c950d8a07ceec57e20
1 {
2 $Id: ImagingTarga.pas 139 2008-09-18 02:01:42Z 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/saver for Targa images.}
32 {$I ImagingOptions.inc}
34 interface
36 uses
39 type
40 { Class for loading and saving Truevision Targa images.
41 It can load/save 8bit indexed or grayscale, 16 bit RGB or grayscale,
42 24 bit RGB and 32 bit ARGB images with or without RLE compression.}
44 protected
52 public
55 published
56 { Controls that RLE compression is used during saving. Accessible trough
57 ImagingTargaRLE option.}
61 implementation
63 const
70 const
73 type
74 { Targa file header.}
90 { Footer at the end of TGA file.}
100 { TTargaFileFormat class implementation }
103 begin
119 var
129 var
134 begin
136 begin
137 // Alocates buffer large enough to hold the worst case
138 // RLE compressed data and reads then from input
150 begin
154 begin
155 // Process uncompressed pixel
159 begin
160 // Copy pixel from src to dest
170 end
171 else
172 begin
173 // Process compressed pixels
176 // Copy one pixel from src to dest (many times there)
178 begin
190 // set position in source to real end of compressed data
192 smFromCurrent);
197 begin
200 begin
201 // Read targa header
203 // Skip image ID info
205 // Determine image format
217 // Format was not assigned by previous testing (it should be in
218 // well formed targas), so formats which reflects bit dept are selected
231 begin
232 // Read palette
235 try
237 // Process palette
244 begin
250 // I've never seen tga with these palettes so they are untested
253 begin
261 begin
268 finally
275 // Load uncompressed mode images
278 // Load RLE compressed mode images
279 LoadRLE;
282 // Check if there is alpha channel present in A1R5GB5 images, if it is not
283 // change format to X1R5G5B5
285 begin
290 // We must find true end of file and set input' position to it
291 // paint programs appends extra info at the end of Targas
292 // some of them multiple times (PSP Pro 8)
293 repeat
298 begin
299 // 495 = size of Extension Area
301 begin
304 end
305 else
310 begin
313 else
318 // Some editors save targas flipped
328 var
337 var
342 var
346 begin
351 begin
353 Exit;
362 begin
371 Break;
377 Result := N
378 else
383 var
387 begin
399 begin
408 Break;
417 const
419 var
423 begin
426 begin
434 begin
444 begin
463 begin
465 begin
466 // Allocate enough space to hold the worst case compression
467 // result and then compress source's scanlines
473 try
475 begin
481 finally
487 begin
491 try
493 // Fill targa header
503 // This indicates that targa is stored in top-left format
504 // as our images -> no flipping is needed.
506 // Set alpha channel size in descriptor (mostly ignored by other software though)
512 // Choose image type
515 else
518 else
523 // Write palette
525 begin
527 try
530 begin
536 finally
542 // Save rle compressed mode images
543 SaveRLE
544 else
545 // Save uncompressed mode images
549 finally
557 var
559 begin
561 // Convert all grayscale images to Gray8 (preserve alpha of AxGrayx formats)
564 // Convert all indexed images to Index8
565 ConvFormat := ifIndex8
567 // Convert images with alpha channel to A8R8G8B8
568 ConvFormat := ifA8R8G8B8
570 // Convert 16bit images (without alpha channel) to A1R5G5B5
571 ConvFormat := ifA1R5G5B5
572 else
573 // Convert all other formats to R8G8B8
580 var
583 begin
586 begin
596 initialization
599 {
600 File Notes:
602 -- TODOS ----------------------------------------------------
603 - nothing now
605 -- 0.21 Changes/Bug Fixes -----------------------------------
606 - MakeCompatible method moved to base class, put ConvertToSupported here.
607 GetSupportedFormats removed, it is now set in constructor.
608 - Made public properties for options registered to SetOption/GetOption
609 functions.
610 - Changed extensions to filename masks.
611 - Changed SaveData, LoadData, and MakeCompatible methods according
612 to changes in base class in Imaging unit.
614 -- 0.17 Changes/Bug Fixes -----------------------------------
615 - 16 bit images are usually without alpha but some has alpha
616 channel and there is no indication of it - so I have added
617 a check: if all pixels of image are with alpha = 0 image is treated
618 as X1R5G5B5 otherwise as A1R5G5B5
619 - fixed problems with some nonstandard 15 bit images
620 }