Image rd file compress

Pages: 1234
Hi

I am trying to read TGA file and then need to compress to almost half the size of the image file. How can this be achieved in cpp?
Any suggestions using graphics apis like opengl/directx.
Below is what I have tried so far in opengl.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "tgard.h"

namespace TGA{
    
    img* loadtextur(std::string &file_name){
	std::ifstream infile( file_name.c_str(), std::ifstream::binary );
        
        char* header = new char[18];
        //infile.seekg(0, infile.beg);
        
        if(infile){
            infile.read(header, 18);
        }
        
        
        img* img1 = new img;
        img1->width = ((unsigned char)header[13]<<8) + (unsigned char)header[12];
        img1->height = ((unsigned char)header[15]<<8) + (unsigned char)header[14];
        img1->pixeldepth = (unsigned char)header[16];
        int size = img->pixeldepth/8*img1->width*img1->height;
        
        char* back = new char[size];
        
        infile.read(back,size);
        
        img1->texture = (GLubyte*)back;
        
        
        return img1;
    }
    
}

#ifndef TGARD
#define TGARD
#include <stdlib.h>
#include <stdio.h>
#include <fstream>

#ifdef 
  #include <OpenGL/gltypes.h>
  #else
  #include <GL/gl.h>
#endif
namespace TGA{
    struct img{
        int height;
        int width;
        int pixeldepth;
        GLubyte* textur;
    };

    img* loadtextur(std::string &filename);
}

Last edited on
If you can handle Qt then the QImage class in Qt handles tga files see https://doc.qt.io/qt-5/qimagewriter.html#setCompression. If it's scaling you want QImage does that too - https://doc.qt.io/qt-5/qimage.html#scaled
as far as I know tga only supports RLE which is not going to ensure 50% compression.
you would have to preprocess the file to get that... probably cut it in half (# of bytes, not dimensions!) first... eg a 100x200 (20k bytes in greyscale) can be 70x140 (9800 bytes greyscale). A little math will get you the exact dimensions, but its not going to be 50x100. RLE can make things bigger, if each entry is unique, eg random pixels. That mean you have to decide if you need exactly 50% or 'thereabouts'. if you need it exact, you do need to round down the dimension resize. If close is ok, round up and hope the RLE makes it smaller instead of larger, and consider random images (bad entropy) to be an anomaly (most real images will not be like that).
Last edited on
which is not going to ensure 50% compression

Depends on the image, of course. Simple icons might compress well.
Of course, we really haven't much idea what the guy really wants.
Does he want to store the compressed images in a different format?
By "compressed" does he perhaps mean "downsampled"?
Who knows.
By "compressed" here means scale down to half the size, by creating copy of the actual image . This is to be done for the TGA. any compression techniques would help here or do I need to convert this TGA file to some other format?
Last edited on
Hi Jonnin

Do you know if any sort of algorithm can help to achieve copy of actual image to scale down to half the size of actual TGA( must be uncompressed), and must support RGB, RGBA?
Last edited on
to cut the actual physical dimensions in half, that is on the web, there are several resamples eg bicubic and so on that do it with minimal damage.

to cut the byte size in half, the best is a wavelet, j2k, jpg, and some unpopular R&D wavelets cut the size by dramatic amounts of course but its not supported to compress TGA this way.
Can you please share any references?
What is the current dimensions and bit depth?
Are they currently RLE compressed?
Are they mostly solid colors? Like icons? Or are they like photographs?
Can you upload a few of them somewhere?

Depending on the situation, you may be able to do it without a library, although there are a number of image processing libraries available, e.g.:
CImg: https://cimg.eu/
Magick++: https://www.imagemagick.org/script/magick++.php
boost GIL: https://www.boost.org/doc/libs/1_76_0/libs/gil/doc/html/index.html
I am trying to read image from tga file.But got the below compiler error. Any idea what this error means?

[code]
void TGAInit(const char *NameFile, unsigned char* &Colour, int &Width, int &Height, int &Channel)
{
// Read whole file into array.
std::ifstream InFile(NameFile, ios::in | ios::binary | CREATENO );
if(!InFile.is_open())
{
fprintf(stderr, "TGA: Failed to open file `%s'.\n", NameFile );
Width = Height = 0;
return;
}

---------------------------------------


[Error] invalid conversion from 'int' to 'std::ios_base::openmode {aka std::_Ios_Openmode}' [-fpermissive]
C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\fstream [Note] initializing argument 2 of 'std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]'
Last edited on
What's CREATENO?
Are your pictures photographic or iconic (where iconic means mostly solid colors, like a cartoon)?
Try running the following hex dump of the first 128 bytes on one of your files and post the results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstdlib>
using namespace std;

int main(int argc, char **argv)
{
    if (argc < 2) {
        fprintf(stderr, "Usage: %s FILENAME [NUMBYTES]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    ifstream fin(argv[1], fin.binary);

    unsigned n = 128;
    if (argc > 2) n = atoi(argv[2]);

    cout << hex << setfill('0');
    char s[17] = {0};
    cout << setw(4) << 0 << ": ";

    for (unsigned i = 0; i < n; ++i)
    {
        unsigned byte = fin.get();
        cout << setw(2) << byte << ' ';
        s[i % 16] = (isprint(byte) ? char(byte) : '.');
        if (i % 16 == 15)
        {
            cout << " |" << s << "|\n";
            cout << setw(4) << i + 1 << ": ";
        }
    }

    if (n % 16 != 0)
    {
        cout << setfill(' ') << setw(3 * (16 - n % 16)) << "";
        cout << " |" << s << "|\n";
    }
    cout << '\n';
}

basic overview of common resample/resize
https://en.wikipedia.org/wiki/Image_scaling


wavelets
https://en.wikipedia.org/wiki/Wavelet_transform

a lossless one:
http://bearcave.com/misl/misl_tech/wavelets/packet/download.html

there are tons of papers and such, and plenty of source code etc out there, just search the web a bit.

Last edited on
Hi dutch

How can I attach my .tga file here. I use dev c++. is there any way to extract hexa dump? I am able to compile your code .

Last edited on
hi seeplus

#define CREATENO 0x20
you can't post a file here, you can post it to another site and link, or maybe uuencode it (please don't lol) if absolutely necessary.

you can open it in your hex editor, visual studio has one built in, or grab a free one off the web.
you can also write something ... file.read(mycharptr, sizeoffile) ... for(.. size of file..) write it in hex <<hex<< for cout or %x for printf.
Can i send to your mail id?
Use a registration-free file-sharing site, e.g., https://www.file.io/
Last edited on
I have uploaded yuv file here.
https://file.io/4uopqhxjCXDM

I have uploaded .tga file here. please check it.
https://file.io/FTcddZ8ShOPH

I am able to read TGA file now. But i am not sure how to make a copy of actual file and then scale to half the size.

Below is the output after reading TGA file.

Width: 64
Height: 64
PixelDepth: 32

Last edited on
Are you able to access the file?
Pages: 1234