EmbDev.net

Forum: FPGA, VHDL & Verilog Run_length_encoding


von Leonardo (Guest)


Rate this post
useful
not useful
Our professor is kinda crazy . He gave us a such hard task for a 
beginner class We are supposed to swap 2 colors of an image with the use 
of Run-length encoding and with the use of VHDL .

I checked this on the internet and there is not a lot of informations or 
documents about this RLE .They just explain the lossy compression and 
the data compression method of the run length coding and that is all .

Does anyone have an idea about this topic ?

von MaWin (Guest)


Rate this post
useful
not useful
Leonardo wrote:
> Does anyone have an idea

Everyone in your class except you ?

An RLE encoded image (bit or byte) stream does clearly say, what color 
(index) the next pixels have (it is not lossy). You pass the strean, 
except that you modify the color (index) of A to B and ob B to A and let 
C D, E... as they were.

Think about the main application of millions of FPGAs, decipher Internet 
streams, unpack base64, decode zip, analyze the content and if certain 
key words are detected modify the information so that for instance an 
HTML page does not display hate speech against the government but keeps 
you in your filtered bubble that your country is the greatest in the 
world.

Your professor tries to show you exactly this, so that you know where 
fake news are coming from.

von Leonardo (Guest)


Rate this post
useful
not useful
Thank you  MaWin for the explanation .

Swapping randomly two indexes would be ok but how could we identify two 
colors?
For example I want to identify the green color and swap it or blend it 
with the blue color ? How could that be possible ?

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Leonardo wrote:
> there is not a lot of informations or documents about this RLE
What is "this RLE"?
What additional information did your professor give you?

> They just explain the lossy compression and the data compression method
> of the run length coding and that is all .
RLE ist not lossy. Additionally it is a very, very simple kind of 
"compression", and due to that its working only good for pictures with 
simple structure and only few colors. The most simple RLE ist something 
like
linelength = 15 pixels
2 pixels red, then 5 pixels green, then 8 pixels blue
3 pixels red, then 5 pixels green, then 7 pixels blue
4 pixels red, then 5 pixels green, then 6 pixels blue
5 pixels red, then 5 pixels green, then 5 pixels blue
6 pixels red, then 5 pixels green, then 4 pixels blue
Draw this picture on a sheet of paper if you can't fix it in brain.
A little bit shorter this picture can be written this way:
15
2 red 5 green 8 blue
3 red 5 green 7 blue
4 red 5 green 6 blue
5 red 5 green 5 blue
6 red 5 green 4 blue
And with red=1, green=2 and blue=3
this results in
15
2 1 5 2 8 3
3 1 5 2 7 3
4 1 5 2 6 3
5 1 5 2 5 3
6 1 5 2 4 3
And now, if you have to swap red an blue this is
15
2 3 5 2 8 1
3 3 5 2 7 1
4 3 5 2 6 1
5 3 5 2 5 1
6 3 5 2 4 1
Got the trick?

Leonardo wrote:
> Swapping randomly two indexes would be ok but how could we identify two
> colors?
What kind of RLE encoded source do you have? Is this just for simulation 
with a file as input and output or is it for real hardware?

BTW: all in all it is definitely a problem a beginner can solve by 
doing some brainstroming. If you have a lack in information, why don't 
you ask your professor? Maybe he's waiting until the first one asks for 
the missing infornation of the RLE encoding structure.

: Edited by Moderator
von Leonardo (Guest)


Rate this post
useful
not useful
Hi Lothar .
Thank you for the great explanation.
I got your point . Theoretically it's easy to understand .

We have a 4bitmap image . I checked the Bitmap file format in Wikepedia 
and it turns out that a 4 bitmap is identified by BI_RLE4 (which is Run 
length encoding)  It would have been atleast easier if it was 24bitmap 
image (RGB)

>Is this just for simulation with a file as input and output?
Yes

>What additional information did your professor give you?
Literally nothing . We have to learn everything by ourselves this 
semester . He said that it's a challenge for us during this coronavirus 
situation to learn this topic alone .

The problem right now is how to identify the colors ? Is there like for 
each color a defined 4 bit value ?

: Edited by Moderator
von c-hater (Guest)


Rate this post
useful
not useful
Leonardo wrote:

> We have a 4bitmap image . I checked the Bitmap file format in Wikepedia
> and it turns out that a 4 bitmap is identified by BI_RLE4 (which is Run
> length encoding)  It would have been atleast easier if it was 24bitmap
> image (RGB)

OK, this is the Microsoft bitmap format. The first thing you have to 
understand is, that this special subformat is an indiced format. That 
means, the content of the bitmap are not color values, but indices into 
a color table.

> The problem right now is how to identify the colors ?

You've to look into the color table. What else?

von MaWin (Guest)


Rate this post
useful
not useful
c-hater wrote:
> You've to look into the color table

Or not.
The excercise only calls to swap 2 colors, not which ones.

von Fernando .S (Guest)


Attached files:

Rate this post
useful
not useful
MaWin wrote
> The excercise only calls to swap 2 colors, not which ones.

Actually I want to swap 2 specified colors not any two colors .


So can I use this color table as a source ?

It says: Blue is =  0001
         Green is = 0010
         Red is =   0100

von c-hater (Guest)


Rate this post
useful
not useful
Fernando .S wrote:

> Actually I want to swap 2 specified colors not any two colors .

MaWin appearently misunderstood the intention (might be: wanted to 
misunderstand to expose your stupid, incomplete specs)...

> So can I use this color table as a source ?
>
> It says: Blue is =  0001
>          Green is = 0010
>          Red is =   0100

Yes. Exactly this is the sense of a color table. Save memory. Store 4 
bits only in the bitmap instead of 24 (or 32).

von Leonardo (Guest)


Rate this post
useful
not useful
Thank you @ c-hater : One last thing to ask :
How do I get the width and the height of a bmp image using VHDL ?
I don't want to write constant values .

von Leonardo (Guest)


Rate this post
useful
not useful
Like getWidth() and getHeight() in C for example

von c-hater (Guest)


Rate this post
useful
not useful
Leonardo wrote:
> Thank you @ c-hater : One last thing to ask :
> How do I get the width and the height of a bmp image using VHDL ?
> I don't want to write constant values .

You've to extract it from the bitmap info header of course. Even you've 
to extract the existence(!) of a color table and the color table 
itselves, as long it exists in the file...

Keep in mind: the color table isn't mandatory. In the case of it's 
absence you've to use a "default" (VGA).

That's for very historical reasons...

In the case of existence you've at least to check the number of entries. 
It should be sufficient to cover all appearing indices in the bitmap 
by standard. But you shouldn't trust foreign data at any time...

von MaWin (Guest)


Rate this post
useful
not useful
Leonardo wrote:
> How do I get the width and the height of a bmp image using VHDL

There ist no need to know width and height if all you have to do is swap 
2 colors.

von c-hater (Guest)


Rate this post
useful
not useful
MaWin wrote:

> There ist no need to know width and height if all you have to do is swap
> 2 colors.

Of course he must know this. At least under the given precondition of an 
RLE-encoded bitmap...

Are you stoned or are you not the actual MaWin? I assume the latter...

von Leonardo (Guest)


Rate this post
useful
not useful
c-hater wrote:

> Of course he must know this. At least under the given precondition of an
> RLE-encoded bitmap...

I know but still I'm totally new to image processing thus every details 
will count .I have a small knowledge on VHDL so this will be a challenge 
for me .

I actually code most of the time with C and C++ and I'm familiar with 
solving problems .

Logically speaking if I want to have this kind of matrix in my image in 
C/C++
2 red 5 green 8 blue
3 red 5 green 7 blue
4 red 5 green 6 blue
5 red 5 green 5 blue
6 red 5 green 4 blue

I would read everyline at once and then analyze their colors and swap 
the two colors that I want to swap  .(In this case I won't need the 
image_width and height like you said)

In C for example it will be something like this (but still I don't know 
if it works on bmp image )

Char Zeile[101]= {'\0'};;
FILE *Datei = fopen("image.bmp", "r"); //Read modus
 while (fscanf(Datei, " %101[^\n]", Zeile) == 1)
{
 // Analyse the file and swap the colors of this line
 or I make a whole matrix with this file and then swap later ..
}

Something like that : it will read everyline of the bitmap image till 
the end of file (EOF).

Should I follow the same logic on VHDL ?
If yes then I would really appreciate it if you write me some code in 
VHDL how to scanline the image till the EOF.

von foobar (Guest)


Rate this post
useful
not useful
Two swap two colors in an indexed image you just have to swap the 
correpsonding entries in the color map - there's no need to process the 
image pixels at all.

von Leonardo (Guest)


Rate this post
useful
not useful
foobar wrote:
> Two swap two colors in an indexed image you just have to swap the
> correpsonding entries in the color map - there's no need to process the
> image pixels at all.

Could you please write me a little example with vhdl how can you swap 2 
entries colors WITH RLE . It would help me a lot to understand how it's 
working exactly.

von foobar (Guest)


Rate this post
useful
not useful
First: Parsing BMP-Files in VHDL sounds kind of silly - are you sure you 
got your task right?

Second: As I said, no need to parse the RLE, just swap two entries in 
the palette.

Third: It's your job - you'd learn nothing if I'd do it for you.

Fourth: I would implement a simple CPU (like J1) and write a program for 
it ;-)

von Leonardo (Guest)


Rate this post
useful
not useful
foobar wrote:
> First: Parsing BMP-Files in VHDL sounds kind of silly - are you
> sure you
> got your task right?

True It would've been so much better for me if it was in C/C++

von Leonardo (Guest)


Rate this post
useful
not useful
foobar wrote:
> Third: It's your job - you'd learn nothing if I'd do it for you.

I didn't ask for you to write the whole code for me . I just wanted a 
little example to know how it works ..

von foobar (Guest)


Rate this post
useful
not useful
> I actually code most of the time with C and C++ and I'm familiar with
> solving problems .

Then solve it in C/C++ first.  That way you don't have to deal with two 
problems, the algorithm and the new language.  When the C version is 
done, you have the algorithm and know what to do and can now concentrate 
on how to do it in VHDL.  Use a method that can be used in both 
languages, i.e. a state machine.

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Fernando .S wrote:
> So can I use this color table as a source ?
Don't look at the internet, look on your own PC. Use YOUR specific 
picture files to check it out. I think the professor supplied some or 
gave a hint where to find them.

Leonardo wrote:
> I have a small knowledge on VHDL so this will be a challenge for me .
See it that way: this is NOT a VHDL problem at the first step. You 
simply don't know about the structure of the files you must handle.

Leonardo wrote:
> I just wanted a little example to know how it works ..
1. For the first step: forget about VHDL (forget about programming at 
all)
2. Take a HEX Editor
3. Open  YOUR specific picture file
4. Locate the color table in YOUR specific picture file
5. Find the two colors in the color table
6. Swap their values
7. Save the edited file
6. Check the result with a picture viewer
7. Finally, when the result is what you wanted, then think about how you 
could use VHDL file commands to automize steps 2..7 and write the code

Having done that all you will see, that step 7. was the most easy in the 
whole process.

Begin with 1.

Leonardo wrote:
> True It would've been so much better for me if it was in C/C++
Then solve that "open file - parse file - swap colors - save 
file"-problem with C. And I mean that you solve it in a way that really 
works. When you have it running in C, then you can fairly easy translate 
it to VHDL by using similar commands, because the whole thing is just 
for simulation.

foobar wrote:
> First: Parsing BMP-Files in VHDL sounds kind of silly -
> are you sure you got your task right?
I think, the professor just wants to teach some little file handling and 
digging for data. And for that the task is nice, because you can 
visualize the results very simple on any screen device.

Leonardo wrote:
> I know but still I'm totally new to image processing thus every details
> will count .I have a small knowledge on VHDL so this will be a challenge
> for me .
BTW: such punctuation marks like . and , and ? and etc. belong to the 
words at the left of the symbol. It is very disturbing when you place a 
space on the wrong side of such puntuation marks.

BTW2: Leonardo-Fernando, pls use only 1 user name in 1 thread. It is 
very distracting to discuss with randomly named people!

: Edited by Moderator
von MaWin (Guest)


Rate this post
useful
not useful
c-hater wrote:
> Of course he must know this.

You obviously do not know, how the color change is going to work.

foobar wrote:
> Two swap two colors in an indexed image you just have to swap the
> correpsonding entries in the color map - there's no need to process the
> image pixels at all.

That's a clever way.

von Fernando .S (Guest)


Rate this post
useful
not useful
Lothar M. wrote:

> 2. Take a HEX Editor
> 3. Open  YOUR specific picture file
> 4. Locate the color table in YOUR specific picture file
> 5. Find the two colors in the color table
> 6. Swap their values
> 7. Save the edited file
> 6. Check the result with a picture viewer
> 7. Finally, when the result is what you wanted, then think about how you
> could use VHDL file commands to automize steps 2..7 and write the code



Thank you so much Lothar I'm getting so close to the solution .
I'm trying to use the ASCII codes of the colors right now but there's a 
little problem that I will have at the end .

With my HEX EDITTOR I wanted to set the red to blue without swapping 
them, just replacing and that's all .

But it seems that the red color will change differently : Red color 
became black . I think that it mixed the red with blue color => Black .

Any solutions for that ?

von Leonardo J. (leonardo)


Attached files:

Rate this post
useful
not useful
Using the hex editor for my image is the
ASCI code for blue :..ÿ
ASCI code for my red :ý..

When I set every code that has "ý.." to "..ÿ" the colors have atleast 
changed but not correctly .

I tried this while using vhdl code but nothing happened. The output 
image stays the same as the input image .

If you wonder if the if statement is working or not . It's working! I 
tested with the debugging report method .

Excuse me for the messy code.

: Edited by User
von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Attached files:

Rate this post
useful
not useful
Leonardo J. wrote:
> Using the hex editor for my image is the ASCI code for blue :..ÿ
> ASCI code for my red :ý..
A HEX editor shows HEX digits at first. And it may show ASCII chars 
additionally. I use HxD for hex editing.

Leonardo J. wrote:
> the colors have atleast changed but not correctly .
Did you do such a swap manually as I already suggested?
No?
I did. And having a file finally it is an easy job to dig out what has 
to happen. See your Color.bmp and compare it to the Color_sw.bmp. See 
the difference and how it behaves. Now do the very similar change to the 
file with VHDL.

von Leonardo J. (leonardo)


Rate this post
useful
not useful
I think I know why my image is not changing . I thought that my input 
header should be the same as the output header so I started from 118 . 
(0 to 117 didn't change)

I'm going to check this out . Thank you very much Lothar

von Leonardo (Guest)


Rate this post
useful
not useful
It worked thank you very much Lothar , can you please delete the 
highlighted code that I sent . Maybe some students from my class can see 
the answer .

von Leonardo J. (leonardo)


Rate this post
useful
not useful
Just a quick question :
Should the red color normally be FF 00 00 (00)  and not 00 00 FF (00) 
(internet source)?

von Duke Scarring (Guest)


Rate this post
useful
not useful
Leonardo J. wrote:
> Should the red color normally be FF 00 00 (00)  and not 00 00 FF (00)
It depends on definition: RGB vs. BGR

Duke

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Leonardo J. wrote:
> Just a quick question : Should the red color normally be FF 00 00 (00)
> and not 00 00 FF (00) (internet source)?
My internet source says that the bmp byte order is BB GG RR 00 in the 
color lookup table:
https://de.wikipedia.org/wiki/Windows_Bitmap
1
Jeder Eintrag der Farbtabelle ist 4 Byte groß und enthält jeweils ein Byte 
2
für den Blau-, Grün- und Rotanteil, sowie ein auf 0 gesetztes Byte (in dieser 
3
Reihenfolge!).

The very same can be found there:
https://en.wikipedia.org/wiki/BMP_file_format
1
In most cases, each entry in the color table occupies 4 bytes, 
2
in the order blue, green, red, 0x00 (see below for exceptions).

> (internet source)?
Which one did you use?

Leonardo wrote:
> Maybe some students from my class can see the answer .
Whats the problem with that (beside its bug)?
Maybe it helps your classmates like I helped you. From my view that 
would be nice...

von Leonardo J. (leonardo)


Rate this post
useful
not useful
Lothar M. wrote:
> My internet source says that the bmp byte order is BB GG RR 00 in the
> color lookup table:
> https://de.wikipedia.org/wiki/Windows_Bitmap
> [pre]

> Jeder Eintrag der Farbtabelle ist 4 Byte groß und enthält jeweils ein
> Byte
> für den Blau-, Grün- und Rotanteil, sowie ein auf 0 gesetztes Byte (in
> dieser
> Reihenfolge!).

Thank you Lothar, I actually didn't know that . I thought that the color 
order is always the same. (RGB)

> (internet source)?
> Which one did you use?
I just checked a random source : https://www.color-hex.com/ .
That's why I got a little bit confused .

> Maybe it helps your classmates like I helped you. From my view that
> would be nice...
Yea sure thanks again .
Have a nice day .

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
Leonardo J. wrote:
> I just checked a random source : https://www.color-hex.com/ .
This is correct for the usual way with RRGGBB as used in most 
programming languages.
And now, if you want to fit that in a 32 bit integer, you may add some 
00 in front of those 24 bit: 00RRGGBB. And if you give those 4 bytes in 
that 32 bit value an address you can write it that way:
1
 3  2  1  0
2
00 RR GG BB
This because the leftmost byte represents the highest integer value and 
therefore it gets the highest index.

And now when you write that values in the order of the bytes in a file 
and view at it as in my hex editor screenshot ascending from the left it 
looks like that:
1
 0  1  2  3
2
BB GG RR 00
Do you recognize that very pattern?

What we learned here: never believe nobody anything. At least until you 
can follow his/hers thoughts.

: Edited by Moderator
von Leonardo J. (leonardo)


Rate this post
useful
not useful
Lothar M. wrote:
> And now, if you want to fit that in a 32 bit integer, you may add some
> 00 in front of those 24 bit: 00RRGGBB. And if you give those 4 bytes in
> that 32 bit value an address you can write it that way:

I see, thanks for the explanation . I didn't think that it was this 
easy.

Maybe I will make program for all the cases together (4bpp, 8bpp, 24bpp
and 32bpp). It would be better .

: Edited by User
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.