[comp.sys.mac.programmer] bitmap --> region

avjewe@cvl.umd.edu (Andrew Donaldson Jewell) (06/30/88)

Here's something which I hope is an easy question :

	How can I turn a bitmap into a region?

	Given a bitmap, I want to get a region enclosing all
	of the black dots and none of the white dots.

	The other way is easy (PaintRgn). 

	Any pointers greatly appreciated.

		Andy Jewell
			avjewe@cvl.umd.edu

tecot@Apple.COM (Ed Tecot) (07/01/88)

In article <2958@cvl.umd.edu> avjewe@cvl.umd.edu (Andrew Donaldson Jewell) writes:
>	How can I turn a bitmap into a region?
>
>	Given a bitmap, I want to get a region enclosing all
>	of the black dots and none of the white dots.

From Tech Note #193: So many BitMaps, so little time:
There is now a routine, available from Apple Software Licensing (and soon
to be in the system as a QuickDraw call), which will convert a bitmap to a
region.

A region can be constructed from a bitmap by the following function:
	FUNCTION BitMapRgn(region:RgnHandle; bMap:BitMap): OSErr;
in C:
	pascal OSErr BitMapRgn(RgnHandle region, BitMap bMap);

The region will be built so that all one bits in bMap are inside the region
and all zero bits are outside it.

[...]

To obtain a version of BitMapRgn in MPW object format which can be linked to
an MPW program, contact Software Licensing:

	Software Licensing
	Apple Computer, Inc.
	20525 Mariani Ave.  M/S 28-B
	Cupertino, CA 95014
	AppleLink: SW.License

						_emt

jmunkki@santra.HUT.FI (Juri Munkki) (07/04/88)

In article <2958@cvl.umd.edu> avjewe@cvl.umd.edu (Andrew Donaldson Jewell) writes:
>Here's something which I hope is an easy question :
>	How can I turn a bitmap into a region?

The sources for a program that turns a bitmap into a region are available
from Apple licensing. The function will also be in a future System.

Disclaimer:
I haven't seen the apple code or any other program code related to this
problem. Any errors just show how dumb I am and that I haven't really
tried to do this.

Let's suppose that you have a bitmap bm of size sx*sy. You then need a
buffering bitmap bb of size sx*1.

The rest is in pseudocode:

  Paint bb to white

  for every line y from 0 to sy-1 do
    exclusive or line y from bm to line 0 of bb

    if bb is not totally white then			*
      write y into regiondata
      flag is white

      for every pixel x from 0 to sx-1 do
	if flag is not pixel x,0 from bb then
	  flag is pixel x,0 from bb
	  write x into regiondata

      if flag is not white then
	write sx into regiondata
      copy line y from bm to line 0 of bb
      write 32767 into regiondata

  copy the data from the last line of the region into regiondata
  store sy as the line number of that data
  write 32767 into regiondata

The line with the asterisk can be easily optimized out so that
the check is not needed. You can simply do the stuff that follows
and then look if some pixels were found. You can also optimize
large bitmaps by looking at bytes, words or longwords at a time
and then only then test the actual pixels.

For more information on why this might work, look at by region
format posting (a long time ago) or try to find it from MacTutor,
where it appeared on the mousehole column.

I would of course appreciate if someone checked this out and flamed.

Juri Munkki
jmunkki@santra.hut.fi
jmunkki@fingate.bitnet