473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get 1 BitPerPixel color

I'm trying to manipulate a 1bpp image.
I have found this example to set a pixel :

private unsafe void SetIndexedPixel (int x,int y,BitmapData bmd, bool pixel)
{
byte* p = (byte*)bmd.Scan 0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));

if (pixel)
p[index]|=mask;
else
p[index]&=(byte)(mask^0 xff);
}
....which seems to work ok, but I haven't been able to find an example of
doing a GetIndexedPixel (), so I tried myself :

protected unsafe bool GetIndexedPixel (int x,int y,BitmapData bmd)
{
byte* p=(byte*)bmd.Sc an0.ToPointer() ;
int index=y*bmd.Str ide+(x>>3);
byte mask=(byte)(0x8 0>>(x&0x7));
return (p[index]==mask);
}

But this only works for less than 1% of the pixels! There's a lot of
assignents and byte-manipulation in the SetIndexedPixel method that I don't
really understand, so I'm not sure if my GetIndexedPixel is correct at all
(something tells me its not). Can anyone spot the error?

Regards
/Morten Nielsen
http://www.iter.dk
Nov 16 '05 #1
2 2529
"Morten Nielsen" <sp**@iter.dk > wrote in message
news:c6******** **@news.net.uni-c.dk...
I'm trying to manipulate a 1bpp image.
I have found this example to set a pixel :

private unsafe void SetIndexedPixel (int x,int y,BitmapData bmd, bool pixel) {
byte* p = (byte*)bmd.Scan 0.ToPointer();
int index = y * bmd.Stride + (x >> 3);
byte mask = (byte)(0x80 >> (x & 0x7));

if (pixel)
p[index]|=mask;
else
p[index]&=(byte)(mask^0 xff);
}
...which seems to work ok, but I haven't been able to find an example of
doing a GetIndexedPixel (), so I tried myself :

protected unsafe bool GetIndexedPixel (int x,int y,BitmapData bmd)
{
byte* p=(byte*)bmd.Sc an0.ToPointer() ;
int index=y*bmd.Str ide+(x>>3);
byte mask=(byte)(0x8 0>>(x&0x7));
return (p[index]==mask);


I think the last line is wrong, it should be:

return ((p[index] & mask) != 0);

You are comparing an entire byte to a bit mask that has a single bit set.
You should be using the mask to AND out only the relevant bit and then check
to see if it's set.

Nov 16 '05 #2
> I think the last line is wrong, it should be:

return ((p[index] & mask) != 0);


Thanks! It did the job perfectly!

/Morten
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5369
by: dan glenn | last post by:
(PHP4.3.4, GD2) How can I save a PNG using GD2 and insure that it saves as a palette-based (8-bit, 256-color) single-color transparancy?? Saving this way, I could be sure that an image loaded from a transparent gif could be saved as a png and the png would work in IE versions 5.5 and greater (IE has bug that messes up png transparancies which use full alpha channel). thanks for any help at all, -dg
6
4114
by: me | last post by:
good day, i found this message: and i want to know more about it too. ========================================== Hey all, example: $s_Colors="0066FF;0066CC;3366CC;0033FF;003399;003366;99CCFF;3399FF;0099FF;66
6
8509
by: rzed | last post by:
I'm using PIL to generate some images which may be rotated at the user's option. When they are rotated, the original image is cropped in the new image (which is fine), and the corners are black (which is not, in this case). I can't find any documented way to change the default fill color (if that's what it is) for the corners, and PIL also doesn't seem to support a flood fill. I have created a flood fill in Python, which works but which...
3
5075
by: Richard A. DeVenezia | last post by:
I hope this is the end of my present 'discovery' phase. I've learned alot about JavaScript in a short time and my head hurts. The following is what came out of all my questions and all the excellent answers (thanks!). It will be the basis of a slightly more complicated function for rendering a two-level navigation bar ( Don't have time to get into design of a multi-styletype renderer for n-level hierarchies. ) This is a single function...
5
3486
by: Chris Beall | last post by:
I'm displaying an image that is also a link against a black background. In Netscape 7.1, the current background color is displayed as a horizontal bar below the image. This allows :hover effects that change the background color to have a visible effect, as this bar changes color. By default, it appears that the color is inherited from the body color, i.e. with no link style applied, the bar is black, and invisible against the background....
4
19897
by: bart plessers | last post by:
Hello, I am making a website where the user can choose a 'skin'. This works with asp en stylesheets. In the stylesheet, a number of tags are (re)defined. The main idea is to have a limited number of colors (lets say 4) and use only these colors to color up every tag. However, with a long list of (custom) styles, it is a lot of work to scan
18
10746
by: Jan Tuxen | last post by:
Jakob Nielsen in his most recent Alertbox (http://www.useit.com/alertbox/20040503.html) tells web authors to change the color of visited links. I agree to his purpose: Help users understand where they have been. I also agree to the background: Too many web authors keep uniform link colors or their pages, thereby confusing the users. What I have a hard time agreeing to is his conclusion that web authors should deliberately change the...
25
12940
by: Neal | last post by:
According to the CSS lint at http://htmlhelp.org/tools/csscheck/, "The shorthand background property is more widely supported than background-color." Can anyone point me to, or provide, information on what browsers have difficulty with the non-shortcut methods of setting background properties?
27
13581
by: Kevin Yu | last post by:
When I declare on HTML page <LINK href="mycss.css" type="text/css" rel=stylesheet /> .... <BODY class=myclass> in mycss.css BODY { FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: Arial, Geneva; background-image: url(images/back.jpg); }
0
8996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8256
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.