Use backtick code blocks for syntax highlighting

This commit is contained in:
James Gregory 2013-12-31 11:38:48 +11:00
commit f658bfbb40
203 changed files with 26546 additions and 25801 deletions

160
49-03.md
View file

@ -22,92 +22,94 @@ coding speed, fewer bugs, and maintainability.
**LISTING 49.3 L49-3.C**
/* Generates all four possible mode X image/mask alignments, stores image
alignments in display memory, allocates memory for and generates mask
alignments, and fills out an AlignedMaskedImage structure. Image and mask must
both be in byte-per-pixel form, and must both be of width ImageWidth. Mask
maps isomorphically (one to one) onto image, with each 0-byte in mask masking
off corresponding image pixel (causing it not to be drawn), and each non-0-byte
allowing corresponding image pixel to be drawn. Returns 0 if failure, or # of
display memory addresses (4-pixel sets) used if success. For simplicity,
allocated memory is not deallocated in case of failure. Compiled with
Borland C++ in C compilation mode. */
```c
/* Generates all four possible mode X image/mask alignments, stores image
alignments in display memory, allocates memory for and generates mask
alignments, and fills out an AlignedMaskedImage structure. Image and mask must
both be in byte-per-pixel form, and must both be of width ImageWidth. Mask
maps isomorphically (one to one) onto image, with each 0-byte in mask masking
off corresponding image pixel (causing it not to be drawn), and each non-0-byte
allowing corresponding image pixel to be drawn. Returns 0 if failure, or # of
display memory addresses (4-pixel sets) used if success. For simplicity,
allocated memory is not deallocated in case of failure. Compiled with
Borland C++ in C compilation mode. */
#include <stdio.h>
#include <stdlib.h>
#include "maskim.h"
#include <stdio.h>
#include <stdlib.h>
#include "maskim.h"
extern void CopySystemToScreenX(int, int, int, int, int, int, char *,
unsigned int, int, int);
unsigned int CreateAlignedMaskedImage(MaskedImage * ImageToSet,
unsigned int DispMemStart, char * Image, int ImageWidth,
int ImageHeight, char * Mask)
{
int Align, ScanLine, BitNum, Size, TempImageWidth;
unsigned char MaskTemp;
unsigned int DispMemOffset = DispMemStart;
AlignedMaskedImage *WorkingAMImage;
char *NewMaskPtr, *OldMaskPtr;
/* Generate each of the four alignments in turn. */
for (Align = 0; Align < 4; Align++) {
/* Allocate space for the AlignedMaskedImage struct for this alignment. */
if ((WorkingAMImage = ImageToSet->Alignments[Align] =
malloc(sizeof(AlignedMaskedImage))) == NULL)
return 0;
WorkingAMImage->ImageWidth =
(ImageWidth + Align + 3) / 4; /* width in 4-pixel sets */
WorkingAMImage->ImagePtr = DispMemOffset; /* image dest */
/* Download this alignment of the image. */
CopySystemToScreenX(0, 0, ImageWidth, ImageHeight, Align, 0,
Image, DispMemOffset, ImageWidth, WorkingAMImage->ImageWidth * 4);
/* Calculate the number of bytes needed to store the mask in
nibble (Map Mask-ready) form, then allocate that space. */
Size = WorkingAMImage->ImageWidth * ImageHeight;
if ((WorkingAMImage->MaskPtr = malloc(Size)) == NULL)
return 0;
/* Generate this nibble oriented (Map Mask-ready) alignment of
the mask, one scan line at a time. */
OldMaskPtr = Mask;
NewMaskPtr = WorkingAMImage->MaskPtr;
for (ScanLine = 0; ScanLine < ImageHeight; ScanLine++) {
BitNum = Align;
MaskTemp = 0;
TempImageWidth = ImageWidth;
do {
/* Set the mask bit for next pixel according to its alignment. */
MaskTemp |= (*OldMaskPtr++ != 0) << BitNum;
if (++BitNum > 3) {
*NewMaskPtr++ = MaskTemp;
MaskTemp = BitNum = 0;
}
} while (--TempImageWidth);
/* Set any partial final mask on this scan line. */
if (BitNum != 0) *NewMaskPtr++ = MaskTemp;
}
DispMemOffset += Size; /* mark off the space we just used */
}
return DispMemOffset - DispMemStart;
}
extern void CopySystemToScreenX(int, int, int, int, int, int, char *,
unsigned int, int, int);
unsigned int CreateAlignedMaskedImage(MaskedImage * ImageToSet,
unsigned int DispMemStart, char * Image, int ImageWidth,
int ImageHeight, char * Mask)
{
int Align, ScanLine, BitNum, Size, TempImageWidth;
unsigned char MaskTemp;
unsigned int DispMemOffset = DispMemStart;
AlignedMaskedImage *WorkingAMImage;
char *NewMaskPtr, *OldMaskPtr;
/* Generate each of the four alignments in turn. */
for (Align = 0; Align < 4; Align++) {
/* Allocate space for the AlignedMaskedImage struct for this alignment. */
if ((WorkingAMImage = ImageToSet->Alignments[Align] =
malloc(sizeof(AlignedMaskedImage))) == NULL)
return 0;
WorkingAMImage->ImageWidth =
(ImageWidth + Align + 3) / 4; /* width in 4-pixel sets */
WorkingAMImage->ImagePtr = DispMemOffset; /* image dest */
/* Download this alignment of the image. */
CopySystemToScreenX(0, 0, ImageWidth, ImageHeight, Align, 0,
Image, DispMemOffset, ImageWidth, WorkingAMImage->ImageWidth * 4);
/* Calculate the number of bytes needed to store the mask in
nibble (Map Mask-ready) form, then allocate that space. */
Size = WorkingAMImage->ImageWidth * ImageHeight;
if ((WorkingAMImage->MaskPtr = malloc(Size)) == NULL)
return 0;
/* Generate this nibble oriented (Map Mask-ready) alignment of
the mask, one scan line at a time. */
OldMaskPtr = Mask;
NewMaskPtr = WorkingAMImage->MaskPtr;
for (ScanLine = 0; ScanLine < ImageHeight; ScanLine++) {
BitNum = Align;
MaskTemp = 0;
TempImageWidth = ImageWidth;
do {
/* Set the mask bit for next pixel according to its alignment. */
MaskTemp |= (*OldMaskPtr++ != 0) << BitNum;
if (++BitNum > 3) {
*NewMaskPtr++ = MaskTemp;
MaskTemp = BitNum = 0;
}
} while (--TempImageWidth);
/* Set any partial final mask on this scan line. */
if (BitNum != 0) *NewMaskPtr++ = MaskTemp;
}
DispMemOffset += Size; /* mark off the space we just used */
}
return DispMemOffset - DispMemStart;
}
**LISTING 49.4 MASKIM.H**
/* MASKIM.H: structures used for storing and manipulating masked
images */
/* MASKIM.H: structures used for storing and manipulating masked
images */
/* Describes one alignment of a mask-image pair. */
typedef struct {
int ImageWidth; /* image width in addresses in display memory (also
mask width in bytes) */
unsigned int ImagePtr; /* offset of image bitmap in display mem */
char *MaskPtr; /* pointer to mask bitmap */
} AlignedMaskedImage;
/* Describes one alignment of a mask-image pair. */
typedef struct {
int ImageWidth; /* image width in addresses in display memory (also
mask width in bytes) */
unsigned int ImagePtr; /* offset of image bitmap in display mem */
char *MaskPtr; /* pointer to mask bitmap */
} AlignedMaskedImage;
/* Describes all four alignments of a mask-image pair. */
typedef struct {
AlignedMaskedImage *Alignments[4]; /* ptrs to AlignedMaskedImage
structs for four possible destination
image alignments */
} MaskedImage;
/* Describes all four alignments of a mask-image pair. */
typedef struct {
AlignedMaskedImage *Alignments[4]; /* ptrs to AlignedMaskedImage
structs for four possible destination
image alignments */
} MaskedImage;
```
#### Notes on Masked Copying {#Heading5}