Size of png file is reduced - vb.net

I changed the last 4 bits of all pixels in a png image to zero. Why was the size of image reduced to less than half?. I believe png is lossless compression. Will I be able to retrieve the embedded data.(here I added 0000 to all pixels, but in actual case i need to add different data) I'm using vb 2015. The code I used is attached below.
Dim image1 As Bitmap = New Bitmap("C:\STEG\originalimage.png")
For i As Integer = 0 To image1.Height - 1
For j As Integer = 0 To image1.Width - 1
Dim value As Color = image1.GetPixel(j, i)
Dim t1 As Byte = value.R
Dim t2 As Byte = value.G
Dim t3 As Byte = value.B
t1 = t1 And 240
t2 = t2 And 240
t3 = t3 And 240
image1.SetPixel(j, i, Color.FromArgb(255, t1, t2, t3))
Next
Next
image1.Save("C:\STEG\originalimageedited.png")

PNG is a lossless image compression format, yes. However if it's lossless or not doesn't anything have to do with the great reduction in size. What affects it is how the compression algorithm works.
I don't know how PNG's algorithm work, but basically compression is about repeating data. For instance if the last 4 bits of every byte is 0000, like so:
1000 0000
0000 0000
1101 0000
0110 0000
...then a compression algorithm could for example express it as:
repeat 0000 every 4 bits
or:
1000 4-0
4-0 4-0
1101 4-0
0110 4-0
(4-0 indicates that it should write four '0' bits)
which might only take up a few bytes in the compressed file, while in the uncompressed file these four bits make up half of every byte.
Thus if you have 1 KB = 1024 bytes of color data and compress it like the first example above, the file size will (roughly speaking) shrink to 512 bytes + the few bytes that indicate the repeat.

Related

How hexadecimal representation matches word size?

What does the statement "hexadecimal matches cleanly with modulo 8 word sizes, such as 8, 16, 32, and 64 bits" mean?
Since a single hex digit can represent exactly 4 bits of binary data, any word size that's a multiple of 4 can be exactly represented with a fixed number of hex digits.
And every word size that's a multiple of 8 (i.e. the common ones) can be represented with a number of digits that's a multiple of 2:
8 bits can store values from 00 to FF
16 bits can store values from 0000 to FFFF
32 bits can store values from 00000000 to FFFFFFFF
...
All 2-digit hex numbers can be represented in 8 bits and all 8 bit values can be represented in 2 hex digits. If a hex editor displays some value as CA FE BA BE you can easily grasp that it's 4 bytes and thus 32 bits. Getting that information from the decimal 3405707966 is not quite as trivial (no matter how you group the digits: there's no nice "byte boundaries" in that representation).
If you compare this with decimal, the same isn't true. For example, 8 bits can represent values from 0 to 255 (decimal). So you need up to 3 digits in decimal to represent 8-bit values. But there are 3-digit decimal values that you can't represent in 8 bits: 256 (or anything higher than that) doesn't map onto 8 bits. So the mapping isn't perfect for decimal numbers.

About redis bitmap structure memory storage problem

for example
> SETBIT bitmapsarestrings 2 1
> SETBIT bitmapsarestrings 3 1
> SETBIT bitmapsarestrings 5 1
> SETBIT bitmapsarestrings 10 1
> SETBIT bitmapsarestrings 11 1
> SETBIT bitmapsarestrings 14 1
> GET bitmapsarestrings
"42"
Binary storage should not like this: 0010 0110 0001 1100 ?
stored in this way, why value is 42?
These SETBIT operations will make the value as a binary string, whose length is 2 bytes or 16 bits. After the settings, the value will be 0b 00110100 00110010 in binary format.
The first byte (0b 00110100) is 52, which is the ascii code of '4', and the second byte (0b 00110010) is 50, which is the ascii code of '2'. So when you get the value of the string, it returns "42".
What #for_stack said, or just refer to the lines immediately above that example (https://redis.io/commands/setbit):
Bitmaps are not an actual data type, but a set of bit-oriented operations defined on the String type (for more information refer to the Bitmaps section of the Data Types Introduction page). This means that bitmaps can be used with string commands, and most importantly with SET and GET.
Because Redis' strings are binary-safe, a bitmap is trivially encoded as a bytes stream. The first byte of the string corresponds to offsets 0..7 of the bitmap, the second byte to the 8..15 range, and so forth.

Reverse Engineering Fixed Point Numbers

I am currently putting an engine into another car and I want to keep the fuel economy calulation inside the boardcomputer working. I managed to recode this part sucessfully, but I have been trying to figure out the (simple?) two byte dataformat they used without success. I assume it is fixed point notation, but no matter how I shift it around, it does not line up. How do the two bytes represent the right number?
Some examples:
Bytes (Dec) --> Result
174,10 -> 2,67
92,11 -> 2,84
128,22 -> 3,75
25,29 -> 4,85
225,23 -> 3,98
00,40 -> 5,00
128,34 -> 5,75
Here's a partial solution:
First, swap the bytes. Then join them:
The result (in hex) is:
0AAE
0B5C
1680
1D19
17E1
2800
2280
Then split the into the first digit (4 bits), the remaining three digits (12 bits) and keep the entire number (16 bits) as well. The result (in decimal) is:
0 2734 2734
0 2908 2908
1 1664 5760
1 3353 7449
1 2017 6113
2 2048 10240
2 640 8832
The first digits seems to be a multiplication factor. 0 stands for 1024, 1 for 1536, 2 for 2048. The formula possibly is f = (1024 + n * 512).
Now divide the entire number by the multiplication factor. The result, rounded to two decimal places, is:
2734 / 1024 = 2.67
2908 / 1024 = 2.84
5760 / 1536 = 3.75
7449 / 1536 = 4.85
6113 / 1536 = 3.98
10240 / 2048 = 5.00
8832 / 2048 = 4.31
It works for all except the last number, which might contain a mistake.
So it seems to be some sort of floating-point number but I don't recoginze the specific format. Possibly, there is a simpler formula the explains the number.

Address of 32 bits integer in Modula-2 on a Big Endian architecture

I have this line of code in modula-2 on a Big Endian processor :
Ptr := ADR(My_32_Bits_Integer)
I want to know if Ptr will be equal to the adress of the most significant byte (so the littlest address) or the less significant byte (so the biggest address) ?
The address of a 32 bit number is always that of the byte allocated first. On Big Endian systems, this is the MS byte, on Little Endian it is the LS byte.
Given the 32-bit integer 12345678h, then it will be stored like this:
Big Endian:
Offset Data
0 12
1 34
2 56
3 78
Little Endian:
Offset Data
0 78
1 56
2 34
3 12

How to convert hex to binary?

For Objective-C:
Hi everyone, I'm trying to convert a hex input into binary. For example, someone enters in :
A55
I want that to convert to
101001010101
I've tried looking through past posts and none seem to be working for me. Any help would be greatly appreciated.
Use a lookup table: there are only 16 possible characters in a HEX representation, each corresponding to a four-character binary code group. Go through the HEX character-by-character, obtain a lookup, and put it in the resultant NSString.
Here is a copy of the lookup table for you.
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111
There are multiple options as to how to do lookups. The simplest way would be making a 128-element array, and placing NSStrings at the elements corresponding to codes of the characters (i.e. at positions '0', '1', ..., 'E', 'F', with single quotes; these are very important).
I believe there is a built-in function for this. If not, you should at least be able to go hex->dec then dec->bin
You can write the conversion from scratch if you know the number of characters, bin to hex is common enough algorithmically.
A mathematical look at the algorithms
SO Answers in C/C++ Another
Base 10 to base n in Objective C
C Hex->Bin
Build a lookup table (an array where you can supply a value between 0 and 15 to get the binary for that hex digit):
char *hex_to_bin[] = {
"0000", "0001", "0010", "0011",
/* ... */
"1100", "1101", "1110", "1111"
};
There should be 16 elements in that table. The conversion process for multiple digits is to handle one digit at a time, appending the results onto the end of your result storage.
Use getchar() to read a char:
int c = getchar();
if (c < 0) { puts("Error: Invalid input or premature closure."); }
Use strchr() to determine which array index to retrieve:
char *digits = "00112233445566778899AaBbCcDdEeFf";
size_t digit = (strchr(digits, c) - digits) / 2;
Look up the corresponding binary values for digit:
printf("%s", hex_to_bin[digit]); // You'll want to use strcat here.