home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD2.bin
/
bbs
/
dev
/
magic.lha
/
MAGIC
/
src
/
mdemo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-20
|
16KB
|
622 lines
/*
* MAGIC Image Tester - creates a small public MAGIC image with
* a teeny tiny interface.
*
* Written by Thomas Krehbiel
*
* (This requires 2.0, BTW.)
*
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <intuition/intuition.h>
#include <libraries/gadtools.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/graphics_protos.h>
#include <clib/gadtools_protos.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <magic/magic.h>
#include <magic/magic_protos.h>
#include <magic/magic_pragmas.h>
struct Library *IntuitionBase;
struct Library *GfxBase;
struct Library *GadToolsBase;
struct MagicBase *MagicBase;
UBYTE *planes[4] = { NULL, NULL, NULL, NULL };
struct MagicImage *mi;
struct MagicHandle *mh;
struct MsgPort *hostport;
void message (char *, ...);
#define WIDTH 320
#define HEIGHT 200
#define DEPTH 3 /* color */
short real_width, real_height;
BOOL alloc_planes (UBYTE **planes, int depth)
{
int i;
for (i = 0; i < depth; i++) planes[i] = NULL;
for (i = 0; i < depth; i++) {
if (!(planes[i] = AllocMem(WIDTH * HEIGHT, MEMF_CLEAR | MEMF_PUBLIC))) {
return(FALSE);
}
}
/* gradient from black to light violet */
for (i = 0; i < HEIGHT; i++) {
memset(planes[0] + (WIDTH * i), i * 255 / (HEIGHT-1), WIDTH);
memset(planes[1] + (WIDTH * i), i * 127 / (HEIGHT-1), WIDTH);
memset(planes[2] + (WIDTH * i), i * 255 / (HEIGHT-1), WIDTH);
}
real_width = WIDTH;
real_height = HEIGHT;
return(TRUE);
}
void free_planes (UBYTE **planes, int depth)
{
int i;
for (i = 0; i < depth; i++) {
if (planes[i]) {
FreeMem(planes[i], real_width * real_height);
planes[i] = NULL;
}
}
}
static
BOOL __saveds PutData (struct MagicImage *pi, LONG offset, LONG rows, LONG *tags)
{
UBYTE *tagdata;
LONG byte;
UBYTE *ptr;
int i;
while (*tags != TAG_END) {
tagdata = (UBYTE *)tags[1];
switch (*tags) {
case TAG_IGNORE :
break;
case GMI_Red :
CopyMem(tagdata, planes[0] + (offset * WIDTH), WIDTH * rows);
break;
case GMI_Green :
CopyMem(tagdata, planes[1] + (offset * WIDTH), WIDTH * rows);
break;
case GMI_Blue :
CopyMem(tagdata, planes[2] + (offset * WIDTH), WIDTH * rows);
break;
case GMI_RGB :
byte = offset * WIDTH;
ptr = (UBYTE *)tagdata;
for (i = 0; i < WIDTH; i++) {
planes[0][byte] = *ptr++;
planes[1][byte] = *ptr++;
planes[2][byte++] = *ptr++;
}
break;
case GMI_ARGB :
byte = offset * WIDTH;
ptr = (UBYTE *)tagdata;
for (i = 0; i < WIDTH; i++) {
ptr++; /* skip alpha */
planes[0][byte] = *ptr++;
planes[1][byte] = *ptr++;
planes[2][byte++] = *ptr++;
}
break;
default :
break;
}
tags += 2;
}
return(TRUE);
}
static
BOOL __saveds GetData (struct MagicImage *pi, LONG offset, LONG rows, LONG *tags)
{
UBYTE *tagdata;
LONG byte;
UBYTE *ptr;
int i;
while (*tags != TAG_END) {
tagdata = (UBYTE *)tags[1];
switch (*tags) {
case TAG_IGNORE :
break;
case GMI_Red :
CopyMem(planes[0] + (offset * WIDTH), tagdata, WIDTH * rows);
break;
case GMI_Green :
CopyMem(planes[1] + (offset * WIDTH), tagdata, WIDTH * rows);
break;
case GMI_Blue :
CopyMem(planes[2] + (offset * WIDTH), tagdata, WIDTH * rows);
break;
case GMI_RGB :
byte = offset * WIDTH;
ptr = (UBYTE *)tagdata;
for (i = 0; i < WIDTH; i++) {
*ptr++ = planes[0][byte];
*ptr++ = planes[1][byte];
*ptr++ = planes[2][byte++];
}
break;
case GMI_ARGB :
byte = offset * WIDTH;
ptr = (UBYTE *)tagdata;
for (i = 0; i < WIDTH; i++) {
*ptr++ = 255;
*ptr++ = planes[0][byte];
*ptr++ = planes[1][byte];
*ptr++ = planes[2][byte++];
}
break;
default :
break;
}
tags += 2;
}
return(TRUE);
}
BOOL init_magic (void)
{
if (!(MagicBase = (struct MagicBase *)OpenLibrary(MAGIC_NAME, 34)))
return(FALSE);
return(TRUE);
}
void close_magic (void)
{
if (mh) {
CloseMagicImage(mh);
mh = NULL;
}
if (mi) {
while (!RemMagicImage(mi)) {
message("Image in use!");
Delay(50);
}
FreeMagicImage(mi);
free_planes(planes, DEPTH);
mi = NULL;
}
}
void cleanup_magic (void)
{
close_magic();
CloseLibrary((struct Library *)MagicBase);
}
void new_magic (void)
{
close_magic();
if (alloc_planes(planes, DEPTH)) {
if (mi = AllocMagicImage(AMI_Width, WIDTH,
AMI_Height, HEIGHT,
AMI_Depth, DEPTH,
AMI_Red, planes[0],
AMI_Green, planes[1],
AMI_Blue, planes[2],
AMI_GetDataCode, GetData,
AMI_PutDataCode, PutData,
AMI_OwnerName, "MagicDemo",
TAG_END)) {
if (AddMagicImage(mi)) {
/*
* Even the owner of an image must open it! Remember to use
* OMI_OwnerPort instead of OMI_MsgPort.
*/
if (mh = OpenMagicImage(mi, NULL, OMI_OwnerPort, hostport, TAG_END)) {
message("Magic image created.");
return;
}
RemMagicImage(mi);
}
FreeMagicImage(mi);
}
free_planes(planes, DEPTH);
}
mi = NULL;
mh = NULL;
}
struct NewMenu newMenus[] = {
{ NM_TITLE, "Project", NULL, 0, 0, 0 },
{ NM_ITEM, "New", "N", 0, 0, 0 },
{ NM_ITEM, "Open...", "O", 0, 0, 0 },
{ NM_ITEM, NM_BARLABEL, NULL, 0, 0, 0 },
{ NM_ITEM, "Manipulate", "M", 0, 0, 0 },
{ NM_ITEM, NM_BARLABEL, NULL, 0, 0, 0 },
{ NM_ITEM, "Cycle", "C", 0, 0, 0 },
{ NM_ITEM, NM_BARLABEL, NULL, 0, 0, 0 },
{ NM_ITEM, "Quit", "Q", 0, 0, 0 },
{ NM_END }
};
struct Window *win;
struct Menu *menus;
APTR vi;
BOOL init_gui (void)
{
struct Screen *scr;
IntuitionBase = OpenLibrary("intuition.library", 37);
GfxBase = OpenLibrary("graphics.library", 37);
GadToolsBase = OpenLibrary("gadtools.library", 37);
/* (yes, I know, but it's just a demo) */
scr = LockPubScreen(NULL);
if (scr == NULL) return(FALSE);
if (!(win = OpenWindowTags(NULL,
WA_Title, "MAGIC Testerosa",
WA_InnerWidth, 200,
WA_Height, 115 + scr->WBorTop + scr->Font->ta_YSize + 1 + scr->WBorBottom + scr->RastPort.TxHeight + 2,
WA_Left, 20,
WA_Top, 20,
WA_Flags, WFLG_CLOSEGADGET | WFLG_DRAGBAR |
WFLG_DEPTHGADGET |
WFLG_SMART_REFRESH | WFLG_NOCAREREFRESH |
WFLG_ACTIVATE,
WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
WA_NewLookMenus, TRUE,
TAG_END))) goto err1;
menus = CreateMenus(newMenus, TAG_END);
if (menus == NULL) goto err2;
vi = GetVisualInfo(scr, TAG_END);
if (vi == NULL) goto err3;
LayoutMenus(menus, vi, GTMN_NewLookMenus, TRUE, TAG_END);
SetMenuStrip(win, menus);
UnlockPubScreen