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
200 // Skip image ID info
202 // Determine image format
214 // Format was not assigned by previous testing (it should be in
215 // well formed targas), so formats which reflects bit dept are selected
228 begin
229 // Read palette
232 try
234 // Process palette
241 begin
247 // I've never seen tga with these palettes so they are untested
250 begin
258 begin
265 finally
272 // Load uncompressed mode images
275 // Load RLE compressed mode images
276 LoadRLE;
279 // Check if there is alpha channel present in A1R5GB5 images, if it is not
280 // change format to X1R5G5B5
282 begin
287 // We must find true end of file and set input' position to it
288 // paint programs appends extra info at the end of Targas
289 // some of them multiple times (PSP Pro 8)
290 repeat
295 begin
296 // 495 = size of Extension Area
298 begin
301 end
302 else
307 begin
310 else
315 // Some editors save targas flipped
325 var
334 var
339 var
343 begin
348 begin
350 Exit;
359 begin
368 Break;
374 Result := N
375 else
380 var
384 begin
396 begin
405 Break;
414 const
416 var
420 begin
423 begin
431 begin
441 begin
460 begin
462 begin
463 // Allocate enough space to hold the worst case compression
464 // result and then compress source's scanlines
470 try
472 begin
478 finally
484 begin
488 try
490 // Fill targa header
500 // This indicates that targa is stored in top-left format
501 // as our images -> no flipping is needed.
503 // Set alpha channel size in descriptor (mostly ignored by other software though)
509 // Choose image type
512 else
515 else
520 // Write palette
522 begin
524 try
527 begin
533 finally
539 // Save rle compressed mode images
540 SaveRLE
541 else
542 // Save uncompressed mode images
546 finally
554 var
556 begin
558 // Convert all grayscale images to Gray8 (preserve alpha of AxGrayx formats)
561 // Convert all indexed images to Index8
562 ConvFormat := ifIndex8
564 // Convert images with alpha channel to A8R8G8B8
565 ConvFormat := ifA8R8G8B8
567 // Convert 16bit images (without alpha channel) to A1R5G5B5
568 ConvFormat := ifA1R5G5B5
569 else
570 // Convert all other formats to R8G8B8
577 var
580 begin
583 begin
593 initialization
596 {
597 File Notes:
599 -- TODOS ----------------------------------------------------
600 - nothing now
602 -- 0.21 Changes/Bug Fixes -----------------------------------
603 - MakeCompatible method moved to base class, put ConvertToSupported here.
604 GetSupportedFormats removed, it is now set in constructor.
605 - Made public properties for options registered to SetOption/GetOption
606 functions.
607 - Changed extensions to filename masks.
608 - Changed SaveData, LoadData, and MakeCompatible methods according
609 to changes in base class in Imaging unit.
611 -- 0.17 Changes/Bug Fixes -----------------------------------
612 - 16 bit images are usually without alpha but some has alpha
613 channel and there is no indication of it - so I have added
614 a check: if all pixels of image are with alpha = 0 image is treated
615 as X1R5G5B5 otherwise as A1R5G5B5
616 - fixed problems with some nonstandard 15 bit images
617 }