Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 393821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:17:44+00:00 2026-05-12T16:17:44+00:00

I am looking for a small and free TGA image loading class or library

  • 0

I am looking for a small and free TGA image loading class or library for java.
Ideally the result is a BufferedImage.

Yes, I have already googled, but most results are outdated, or are quite big libraries that contain a lot of other stuff i dont need. I am looking for something small and simple that reads just TGA images.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:17:44+00:00Added an answer on May 12, 2026 at 4:17 pm

    We use this class copied from some open source project to read TGA files. It’s really old. It can only handle Targa files with most basic encoding. Give it a try.

    public class TargaReader
    {
            public static Image getImage(String fileName) throws IOException
            {
                    File f = new File(fileName);
                    byte[] buf = new byte[(int)f.length()];
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
                    bis.read(buf);
                    bis.close();
                    return decode(buf);
            }
    
            private static int offset;
    
            private static int btoi(byte b)
            {
                    int a = b;
                    return (a<0?256+a:a);
            }
    
            private static int read(byte[] buf)
            {
                    return btoi(buf[offset++]);
            }
    
            public static Image decode(byte[] buf) throws IOException
            {
                    offset = 0;
    
                    // Reading header
                    for (int i=0;i<12;i++)
                            read(buf);
                    int width = read(buf)+(read(buf)<<8);
                    int height = read(buf)+(read(buf)<<8);
                    read(buf);
                    read(buf);
    
                    // Reading data
                    int n = width*height;
                    int[] pixels = new int[n];
                    int idx=0;
    
                    while (n>0)
                    {
                            int nb = read(buf);
                            if ((nb&0x80)==0)
                            {
                                    for (int i=0;i<=nb;i++)
                                    {
                                            int b = read(buf);
                                            int g = read(buf);
                                            int r = read(buf);
                                            pixels[idx++] = 0xff000000 | (r<<16) | (g<<8) | b;
                                    }
                            }
                            else
                            {
                                    nb &= 0x7f;
                                    int b = read(buf);
                                    int g = read(buf);
                                    int r = read(buf);
                                    int v = 0xff000000 | (r<<16) | (g<<8) | b;
                                    for (int i=0;i<=nb;i++)
                                            pixels[idx++] = v;
                            }
                            n-=nb+1;
                    }
    
                    BufferedImage bimg = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
                    bimg.setRGB(0,0,width,height,pixels,0,width);
                    return bimg;
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 190k
  • Answers 190k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Strange co-incidence, I just wrote one earlier today! I don't… May 12, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer Something like this might help: $date = date( "j F",… May 12, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer Here's an article covering serializing and de-serializing a TreeView control… May 12, 2026 at 5:58 pm

Related Questions

My company is a custom development shop for a number of projects, some larger
I wonder what options there are for .NET (or C# specifically) code coverage, especially
I am starting a small project with one other developer and need to setup
I am interested in writing a simplistic navigation application as a pet project. After
OK, I know there have already been questions about getting started with TDD ..

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.