Image fading help needed C++

Pages: 123
I am doing it right there. This is a 32 bit format (ABGR byte-wise). You can even see it because I pasted the byte adjustment 3 times, once for each of red, green, and blue... (really should clean that up ...)

I always do it via a one dimensional pointer to RGB or RGBA data (or something akin to it, eg the above is due to byte-order/endian ). If you cast to unsigned char *, its by field (eg, the red byte) and if you do it by integers its the entire color at a time (for RGBA). For 3 byte color, you have to make your own pixel type if you want that.

I took the sprintf out anyway. but it would not compile with the standard function without a pragma, which is just ... not right, however you want to phrase it. I can ignore a warning, but to refuse to compile is nuts.

Its bad code. I needed something done, and I needed to do it with a PNG, and this was the path of least resistance.
Last edited on
It hasn't been mentioned yet, but stb image is a simple header-only library to integrate into your project.
https://github.com/nothings/stb/blob/master/stb_image.h

It's what SFML uses.
https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/ImageLoader.cpp
ImageLoader::loadImageFromFile

Are there any image filter algorithms that can be applied here?
A simple blur kernel is just a square filter of 1s.
1 1 1
1 1 1
1 1 1

You convolve this with each channel of your image (with your choice of padding). This is probably 'good enough' for your purposes. There are some downsides of doing things like averages with RGB; you can get some distortion due to the color space. If you do the math in the YUV color space, this is said to reduce distortion.
https://towardsdatascience.com/image-processing-with-python-blurring-and-sharpening-for-beginners-3bcebec0583a
(Yes, this article is for Python, but the explanation on what it's doing still applies to any language)

I suggest taking Duthomhas' advice; you'd be wasting your time writing your own code for anything more complicated than BMP/TGA image loading/writing.

Before you even do image filtering, I suggest just getting passthrough read-write logic down (write the same image that you read).
Last edited on
@Ganado,

the OP mentioned a few times that he can't use libraries.
Not sure why. Maybe it's a commercial project? It doesn't look like homework.
I know that. It's a nonsensical request, so I ignored it.

Actually, I didn't ignore it, I repeated what Duthomhas said. Writing your own PNG or JPEG compression/decompression is a huge, huge effort. I actually don't often like to discourage people from tinkering, but when it comes to complex things like image formats and compression, it really isn't worth the effort. If OP said "I understand it's ridiculously complex to handroll my own support for multiple image types, but I want to do it anyway", then fine. Or if you specifically wanted to improve an existing library due to quantifiable issues you have with it, or develop a new file format that you think has advantages, then that's also a different story. But there are libraries already out there that can be used commercially, or else 99.9% of graphical programs would be non-free. I agree, it's not homework (or it's badly interpreted homework). Apologies for the rambling edit.
Last edited on
One of the regular users(seeplus ?) here can't use any libraries at work. That made me wandering if this is some commercial project. Maybe the OP will shed some light why can't/want use any library.
OP is so far over his head I really can't believe he is being employed for this. His requirement to not use libraries is insane. Even hospital systems use libraries to display fancy graphics. Graphics format writers are heavily incentivized to write libraries that can be used in secure contexts.

I've actually written decoders for everything mentioned here except JPEG/JFIF (for which I've only written code to read/write EXIF data in a JFIF). That includes the major TGA, BMP (RGB and RGBA), GIF, PNG, APNG, TIFF formats. (TGA is a legacy format not generally used anymore.) For everything but the TIFF format (which is vast) I've written stuff that handles the shockingly flexible specs way better than a lot of libraries. Heck, I was using TrueColor GIFs before people generally realized it was a thing. (A frustrating endeavor, actually, since until very recently no browser could display one properly, because browser library code doesn't adhere to the GIF spec.)

That said, I'm telling you (OP) that your No Libraries policy is stupid (IMHO, of course). You are trying to build an entire car to design a cup-holder.

If you MUST go that route (because your company absolutely requires it), choose one or two formats that you are willing to go bonkers with and either require your users to stick to them or have them converted by a library middleware program on your server before it hits your secure context. I recommend BMP and PNG. They are both very easy. If your company's secure context doesn't have software using zlib, then just go with BMP. No one uses TGA; BMP covers its niche very well.

@jonnin, thmm All the secure warning stuff is just Microsoft's way of wanting you to use different functions. Those functions are *not* deprecated by anyone but Microsoft, wrt their non- core systems engineers. There's nothing wrong with turning the warnings off.

/rant off
its a shame about TGA. Its uncompressed format is ultra simple to crank out.
PNG is my go-to now, because transparency. Which everyone makes far more aggravating than it needs to be in the editors.
Last edited on
Yes!
I love TGA. It is far more capable than BMP. And, unlike most BMP editors, TGA editors don't typically clobber alpha data while doing simple things like simply transferring the file across the network. (Also, its compressed format is uber-easy to crank out as well.)

Alpha handling has its quirks, and PNG supports all of them (pre-multiplied, anyone?), but fortunately most applications these days do the Right Thing and store it unmolested in file and leave the weirdness for specialized contexts.

Too bad APNG is treated like the bastard child lurking in the corner. MPNG may be more flexible, but it is a nightmare to deal with. APNG just does it the right, easy way. (Though it did inherit one annoying quirk from GIFs, alas.)
Hi Ganado

I have no idea about Python code. All I am trying is with C++,so if could tell me which appropriate filter to be used it would help. My code provided here is not able to get a readable image after doing the fading functionality. Any idea how to do it from scratch?
There isnt one. C++ does not know anything about images.

there are 2 or 3 approaches you could try.
1) ignore reading and saving the image. Use a library (for now, you can take it out later) and get your image processing algorithm(s) in place.

2) focus on reading and writing a couple of the easier image types.

3) look for some free to use raw c++ code to do 1 and 2 (not a library, but things like what I posted, small code snippets). Its been done. The code is out there. The problem is that these image formats are so obnoxious that you end up needing 5 pages of code to read and write most image types. Even my dumb sample above is 150 lines of crap just to read and write the file, and the read/write is in a library so you can't see that code.

Last edited on
Hi Ganaldo

It looks like you are using Fast Fourier Transform (FFT) to go to Fourier space, then a complex multiplication and an inverse FFT to go back to real space. But I dont understand how your laplace filter works here? May be I am not good at DSP.
Hi jonnin

I have seen the link you shared. For Targa, do you want to convert to PNG offline and then use plane GDI+, then finally apply the fade technique?
Last edited on
I think we're getting trolled.
I don't want to do anything. Its all you.

^^ possibly. Or ESL.
@denver2020, you said can't use a 3rd party library.

People have given you some great suggestions and links, now it is up to YOU to go pump some neurons. Learn how to do what you need to fulfill your ridiculously um-possible requirements. So why are you still here begging for us to do the work for you?
I tried to read targa file, fetch some header values by check them, but none of the values gets changed and I then write to another targa file, but it doesnt seem to work. any idea what is wrong here?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
struct TARGAFILE {
    char idLength;
    char colourMapType;
    char imageType;
    short colourMapStart;
    short colourMapNumEntries;
    char bitsPerEntry;
    short xOrigin;
    short yOrigin;
    short width;
    short height;
    char bitsPerPixel;
    char* pixelData;
};

TARGAFILE fetchImage(const std::string& pathToImage) {
    std::ifstream image(pathToImage, std::ios::binary);
    if (!image.is_open()) {
        throw FileNotFound();
    }
    TARGAFILE targaFile;
    image.read(&targaFile.idLength, sizeof(targaFile.idLength));
    image.read(&targaFile.colourMapType, sizeof(targaFile.colourMapType));
    image.read(&targaFile.imageType, sizeof(targaFile.imageType));
    image.read((char*)(&targaFile.colourMapStart), sizeof(targaFile.colourMapStart));
    image.read((char*)(&targaFile.colourMapNumEntries), sizeof(targaFile.colourMapNumEntries));
    image.read(&targaFile.bitsPerEntry, sizeof(targaFile.bitsPerEntry));
    image.read((char*)(&targaFile.xOrigin), sizeof(targaFile.xOrigin));
    image.read((char*)(&targaFile.yOrigin), sizeof(targaFile.yOrigin));
    image.read((char*)(&targaFile.width), sizeof(targaFile.width));
    image.read((char*)(&targaFile.height), sizeof(targaFile.height));
    image.read(&targaFile.bitsPerPixel, sizeof(targaFile.bitsPerPixel));

    int imageDataSize = targaFile.width * targaFile.height * (targaFile.bitsPerPixel / 8);

    targaFile.pixelData = new char[imageDataSize];

    int originalPosition = (int)image.tellg();
    image.read(targaFile.pixelData, imageDataSize);
    return targaFile;
}

void CheckHdrImage(TARGAFILE targaFile){
    std::ofstream output("test.tga", std::ios::binary);
    output.write(&targaFile.idLength, sizeof(targaFile.idLength));
    output.write(&targaFile.colourMapType, sizeof(targaFile.colourMapType));
    output.write(&targaFile.imageType, sizeof(targaFile.imageType));
    output.write(reinterpret_cast<const char*>(&targaFile.colourMapStart), sizeof(targaFile.colourMapStart));
    output.write(reinterpret_cast<const char*>(&targaFile.colourMapNumEntries), sizeof(targaFile.colourMapNumEntries));
    output.write(&targaFile.bitsPerEntry, sizeof(targaFile.bitsPerEntry));
    output.write(reinterpret_cast<const char*>(&targaFile.xOrigin), sizeof(targaFile.xOrigin));
    output.write(reinterpret_cast<const char*>(&targaFile.yOrigin), sizeof(targaFile.yOrigin));
    output.write(reinterpret_cast<const char*>(&targaFile.width), sizeof(targaFile.width));
    output.write(reinterpret_cast<const char*>(&targaFile.height), sizeof(targaFile.height));
    output.write(&targaFile.bitsPerPixel, sizeof(targaFile.bitsPerPixel));
    output.write(reinterpret_cast<const char*>(&targaFile.pixelData), sizeof(targaFile.pixelData));
    output.close();
}
What doesn't work?
An idea might be to use a hex editor and check that the file header contains the values you expect.
I am not able to read Targa header values. Is there something wrong with the code?
One problem I see is on line 56:
output.write(reinterpret_cast<const char*>(&targaFile.pixelData), sizeof(targaFile.pixelData));
sizeof doesn't work with pointers.

Shouldn't the pixel be unsigned ?

Why don't you use std::vector<unsigned char> instead of a pointer ?
Pages: 123