image - how to draw pixels to pf1bit in Delphi? -


how draw pixels timage in pf1bit bitmap format? have tried result the whole of image black.

here code i've tried :

image1.picture.bitmap.loadfromfile('example.bmp'); // image rgb pf24bit 320 x 240 px resolution image1.picture.bitmap.pixelformat := pf1bit; i:=0 round(image1.picture.bitmap.canvas.height/2) - 1 begin j:=0 round(image1.picture.bitmap.canvas.width/2) - 1 begin image1.picture.bitmap.canvas.pixels[i,j]:=1; // correct? ... := 1? i've tried set 255 (mean white), still black end; end; 

note image size 320x240 pixel.

thanks before.

you need pack 8 pixels single byte 1 bit color format. inner loop this:

var bm: tbitmap; i, j: integer; dest: ^byte; b: byte; bitsset: integer; begin bm := tbitmap.create; try bm.pixelformat := pf1bit; bm.setsize(63, 30); := 0 bm.height-1 begin b := 0; bitsset := 0; dest := bm.scanline[i]; j := 0 bm.width-1 begin b := b shl 1; if odd(i+j) b := b or 1; inc(bitsset); if bitsset=8 begin dest^ := b; inc(dest); b := 0; bitsset := 0; end; end; if b<>0 dest^ := b shl (8-bitsset); end; bm.savetofile('c:\desktop\out.bmp'); bm.free; end; end; 

the output looks this:

enter image description here

update

rob's comment prompted me @ using pixels[] rather bit-twiddling above. , indeed possible.

var bm: tbitmap; i, j: integer; color: tcolor; begin bm := tbitmap.create; try bm.pixelformat := pf1bit; bm.setsize(63, 30); := 0 bm.height-1 begin j := 0 bm.width-1 begin if odd(i+j) begin color := clblack; end else begin color := clwhite; end; bm.canvas.pixels[j,i] := color; end; end; bm.savetofile('c:\desktop\out.bmp'); bm.free; end; end; 

since each call assign pixels[] results in call windows api function setpixel, bit-twiddling code perform better. of course, ever matter if bitmap creation code performance hot-spot.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -