Fix for a crash which happened when a document couldn't be opened.
[AROS-Contrib.git] / fish / surf / mapimgpix.c
bloba44c2ba3a464366e8cb51ab37a285e45830a4da3
1 #include <aros/oldprograms.h>
2 #include <stdio.h>
3 #include "mytypes.h"
4 #include "readilbm.h"
6 void OutErr(char *);
8 #define DefRes 2
9 int MapImageV = DefRes*DefRepV,
10 MapImageH= DefRes*DefRepH; /* virtual screen size */
11 static int PixV=DefRes,
12 PixH=DefRes; /* true ilbm size in pixels */
13 short MapRepV = DefRepV,
14 MapRepH = DefRepH;
15 static short BytesPerLine;
16 static unsigned char *Raster= null;
17 static long MaxShade;
18 static bool AxisFlipped = DefXYFlip;
20 * Update the MapImageH and MapImageV variables
22 void PrepImgPix()
24 MapImageV = PixV * MapRepV;
25 MapImageH = PixH * MapRepH;
26 if( AxisFlipped ) {
27 int temp;
28 temp = MapImageV;
29 MapImageV = MapImageH;
30 MapImageH = temp;
34 * free up any memory holding mapping image
36 void CloseImgPix()
38 if( Raster) free(Raster);
39 Raster = null;
40 PixV = 0xff; PixH = 0xff;
41 PrepImgPix();
45 * cause x and y axises to be reversed
47 void FlipImgPix( flip )
48 bool flip;
50 AxisFlipped = flip;
51 PrepImgPix();
55 * 4 bits per pixel means 2 pixels per byte
57 bool OpenImgPix(sizex, sizey, maxshade)
58 int sizex, sizey;
59 short maxshade;
61 CloseImgPix();
62 if( maxshade == 0 ) {
63 OutErr("OpenImgPix: got max shade = 0\n");
64 maxshade = 1;
66 MaxShade = maxshade;
67 BytesPerLine = (sizex +1)/2;
68 Raster = (unsigned char *) malloc( BytesPerLine * sizey);
69 if( !Raster ) {
70 printf("OpenImgPix: not enough memory\n");
71 return(false); /* no memory err */
74 PixV = sizey;
75 PixH = sizex;
76 PrepImgPix();
77 return(true);
80 #define CalcByte(cbx,cby) (Raster + ( BytesPerLine * cby ) + (cbx >> 1))
83 void SetImgPix(x, y, val)
84 int x, y; /* location */
85 int val;
87 unsigned char *bite;
88 unsigned char shade;
90 if( x > PixH || x < 0 || y > PixV || y < 0 ) {
91 printf("SetImgPix(%d,%d,%d) out of range\n",x,y,val);
92 return;
95 if( !Raster) return;
96 shade = ( (val<< 4)-val)/MaxShade;
97 bite = CalcByte(x,y);
98 if( x & 1) {
99 *bite = (*bite & 0xf) | ( shade <<4 );
101 else {
102 *bite = (*bite & 0xf0) | shade;
107 short GetImgPix(x,y)
108 int x, y;
110 unsigned char *bite;
112 if( AxisFlipped ) {
113 int temp;
114 temp = x; x = y; y = temp;
117 x %= PixH;
118 y %= PixV;
120 if( !Raster ) {
121 return( (short)(((x ^ y)& 0x10) ? 0xff: 0));
123 bite = CalcByte(x,y);
125 if( x & 1) {
126 return((short)(*bite & 0xf0));
128 else {
129 return( (short)((*bite & 0x0f) <<4));