enter image description here There is an array of bytes (converted BMP) in which you need to check the bits if the bit = 1 then the next one too = 1 if the bit = 0 then the next = 0 how to implement such a cycle?

  • I do not understand the question. Need every even bit be set to the same value as the odd one? Or change bpp - bit per pixel - instead of one to make two? - Alexander Petrov
  • @AlexanderPetrov add just a picture that would be clear - Pablo Murena

2 answers 2

Judging by your previous questions, you are working with an array of bytes obtained from images such as System.Drawing.Image , System.Drawing.Bitmap .

Alternatively, you can get by with the capabilities of GDI + itself.

Suppose there is a picture with the original image:

 Bitmap source = new Bitmap("source.bmp"); 

The next line of code will create a new image, doubled (stretched) horizontally. It seems that this is what you need.

 Bitmap target = new Bitmap(source, source.Width * 2, source.Height); 

Everything! You can use it. For example, save:

 target.Save("target.bmp", ImageFormat.Bmp); // Π²Ρ‹Π±Π΅Ρ€ΠΈΡ‚Π΅ ΠΆΠ΅Π»Π°Π΅ΠΌΡ‹ΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ 

C # code. I think, transfer to C ++ / CLI is not difficult.


Another option is drawing via Graphics . This class has many properties, you can play with their settings to obtain the desired quality.

 Bitmap target = new Bitmap(source.Width * 2, source.Height); // создаСм пустоС ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ var graphics = Graphics.FromImage(target); //graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; //graphics.SmoothingMode = SmoothingMode.AntiAlias; graphics.DrawImage(source, new Rectangle(0, 0, target.Width, target.Height)); 

And finally, the cardinal way. Use the LockBits method.

The sample code by reference shows how to copy an image into an array of bytes and work with it in a loop. It will be faster than copying through a MemoryStream . In addition, the array will be exactly a raw pixel map, without a cap , etc.

    It is necessary to understand that we replace bit 1 with 11. number 3. If not, then 0. 10 => 1100 = 12, but 12/2 = 6. This way you can unpack the bits. abcdefg => aabbccddeeffgg

    The implementation in c ++ is somewhere like this ...

      unsigned short* doubledensity(char* a, int size){ unsigned short* q = (unsigned short*)malloc(size*2); // ΠΈΠ»ΠΈ Π΄Ρ€ΡƒΠ³ΠΈΠΌ способом ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ массив назначСния for (int i=0;i<size;i++) q[i]=(a[i]&1)*3+(a[i]&2)*6+(a[i]&4)*12 +(a[i]&8)*24 +(a[i]&16)*48+(a[i]&32)*96+(a[i]&64)*192+(a[i]&128)*384; return q; } 

    Do not forget to release q. If the bit was one - it became two - then the array q should be twice the size. If q is an array of char or int, it will have to be cast into short (in this way) otherwise it will not correctly calculate (or it will be necessary to change the writing mechanism in q). It is advisable not to make a mistake with the caste.

    a - this means an array with a clean image, without caps, without alignments.

    • The author with a hat, etc. tyts - Alexander Petrov
    • The cap is easily pulled out as with it there is a pointer to the beginning of the pixel array - Pablo Murena
    • It shows the actual decompression of the pixel array. I think you get the pixel array yourself. - nick_n_a