Targa Decompression

This is a discussion on Targa Decompression within the Tech Board forums, part of the Community Boards category; I decided to write a utility to do a number of things with a targa image, one of which is ...

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

    Targa Decompression

    I decided to write a utility to do a number of things with a targa image, one of which is to decompress it.

    Using a targa image I have, I am looking at the encoding it has.

    Code:
    00      00      0A      00      00      00      00      00      00      00
    00      00      1C      00      18      01      18      00      01      BD
    AD      6F      B5      A8      6E      81      88      81      5A      02
    This snippet contains the tga header and a small amount of pixel data.
    Noting the 0x0A (10), it has run-length encoding.

    Question #1
    From what I have read about RLE, I have found tutorials that are completely opposite of each other. I am not sure which result is correct:

    Example:
    Data - AAAAAAAAAAAAABBCCCCCCCCCCCC

    Would this be encoded as:
    13A2B12C
    or as
    A13B2C12

    Question #2
    Looking at the data above, you can see the pixel data starts with this snippet:
    Code:
     01      BD      AD      6F      B5      A8      6E      81      88      81      5A      02
    How would i separate this out?
    1 pixel of BD, AD, 6F?
    181 pixels! of A8, 6E, 81?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    You'd have to read the Targa spec to find out whether it uses <count><data> or <data><count> as the storage of RLE. Both are valid implementations of the same idea.

    Just put "targa file format" into a search engine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Ya, I think I have discovered that it is <data><count>, but I still have some questions about RLE in general.

    This is the start of pixel data in the file I am using.
    001 189 173 111 181 168 110 129 136 129
    090 002 086 083 064 104 099 074 091 087
    068 129 091 088 069 001 132 127 091 098
    I started testing to see what the pixels were and found out that these were the first few:
    189 173 111
    181 168 110
    136 129 090
    136 129 090

    What my questions are is how to know whether or not the pixel is followed by a count or not and why the hell is that random 129 there?

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    What my questions are is how to know whether or not the pixel is followed by a count or not and why the hell is that random 129 there?
    The random 129 is there because you haven't spent 5 minutes reading specs yet.
    You have a 24-bit RLE encoded Targa. ok. so. First byte of image data is "001"
    "001" means "Not RLE encoded (MSB is 0) and there are two (yes, two*) pixels"
    Two pixels follow, 6 bytes worth of data.
    Now, notice that your next two pixels in your sample are the same... hmm. Let's take that 129 byte:
    129 dec = 10000001 bin
    MSB is 1, this is an RLE section. The rest = 1, so there are two pixels. (*We add 1 to the pixel count, as we're always encoding at least one pixel. This allows RLE/nonRLE sections to have a max of 128 instead of 127 pixels...) A single "pixel" follows, for the next three bytes, and we repeat it twice.

    I don't know Targa. Personally, PNG, FTW. But, I Was Feeling Lucky, so, I skimmed through: The result of Googling TGA format, and feeling lucky.
    Now, go read that spec. More or less, you really want to start where it says "Published Specification" in big, green letters, and read the remainder of the page.
    Last edited by Cactus_Hugger; 07-17-2008 at 12:50 AM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading TARGA with OpenGL
    By Da-Nuka in forum Game Programming
    Replies: 9
    Last Post: 04-18-2005, 12:58 PM
  2. Reverse Decompression.
    By Coder87C in forum C++ Programming
    Replies: 2
    Last Post: 04-11-2005, 04:50 PM
  3. Targa File Help
    By jimboob in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2005, 03:34 AM
  4. Video Decompression
    By *Michelle* in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 06-27-2002, 05:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21