1
0
mirror of https://github.com/weiju/amiga-stuff synced 2025-11-20 08:48:41 +00:00

restructured source tree

This commit is contained in:
Wei-ju Wu
2016-01-16 13:45:15 -08:00
parent 037bf7bb4a
commit f69b3a8ee4
12 changed files with 3 additions and 41 deletions

3
iffview/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
iffview
ilbm
examples

17
iffview/Makefile Normal file
View File

@ -0,0 +1,17 @@
#CC=vc +aos68k
CC=vc +kick13
CFLAGS=-c99 -I$(NDK_INC) -DDEBUG
all: iffview
clean:
rm -f *.o iffview ilbm
iffview: iffview.o ilbm.o
$(CC) $(CFLAGS) $^ -lamiga -lauto -o $@
# Mostly for testing
# interestingly, when defining std=c99, LITTLE_ENDIAN disappears as a definition
# in gcc, so we define it explicitly
ilbm: ilbm.c
gcc -o $@ $< -DSTANDALONE -DDEBUG -std=c99 -DLITTLE_ENDIAN

23
iffview/README.md Normal file
View File

@ -0,0 +1,23 @@
## iffview - A simple IFF viewer application
### Description
Writing a simple IFF viewing tool. The learning purpose is to see how to use the
different available display modes and screens on Intuition
### Building
Developed with VBCC, on your system type
```
make
```
### Running
Open the command line interface on your real Amiga or emulator and type
```
iffview <your image>
```

226
iffview/iffview.c Normal file
View File

@ -0,0 +1,226 @@
/* iffview.c - IFF ILBM viewer application for Amiga OS >= 1.3
This file is part of amiga30yrs.
amiga30yrs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
amiga30yrs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with amiga30yrs. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdlib.h>
#include <exec/libraries.h>
#include <intuition/intuition.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#ifdef __VBCC__
#include <clib/alib_stdio_protos.h>
#endif
#include "ilbm.h"
#define WIN_TITLE "IFF Viewer"
#define FILE_MENU_NUM 0
#define NUM_FILE_MENU_ITEMS 2
#define QUIT_MENU_ITEM_NUM 0
struct NewWindow newwin = {
0, 0, 0, 0, 0, 1,
IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
WFLG_CLOSEGADGET | WFLG_ACTIVATE | WFLG_NOCAREREFRESH | WFLG_BACKDROP,
NULL, NULL, WIN_TITLE,
NULL, NULL,
0, 0,
0, 0,
CUSTOMSCREEN
};
struct IntuiText menutext[] = {
{0, 1, JAM2, 0, 1, NULL, "Open...", NULL},
{0, 1, JAM2, 0, 1, NULL, "Quit", NULL}
};
struct MenuItem fileMenuItems[] = {
{NULL, 0, 0, 0, 0, ITEMTEXT | ITEMENABLED | HIGHCOMP | COMMSEQ, 0,
&menutext[1], NULL, 'Q', NULL, 0}
};
struct Menu menus[] = {
{NULL, 20, 0, 0, 0, MENUENABLED | MIDRAWN, "File", &fileMenuItems[0], 0, 0, 0, 0}
};
struct Window *window;
struct Screen *screen;
void cleanup()
{
if (window) {
ClearMenuStrip(window);
CloseWindow(window);
}
if (screen) {
CloseScreen(screen);
}
}
BOOL handle_menu(UWORD menuNum, UWORD itemNum, UWORD subItemNum)
{
printf("menu, menu num: %d, item num: %d, sub item num: %d\n",
(int) menuNum, (int) itemNum, (int) subItemNum);
if (menuNum == FILE_MENU_NUM && itemNum == QUIT_MENU_ITEM_NUM) {
/* quit */
return TRUE;
}
return FALSE;
}
void handle_events()
{
BOOL done = FALSE;
struct IntuiMessage *msg;
ULONG msgClass;
UWORD menuCode;
int buttonId;
while (!done) {
Wait(1 << window->UserPort->mp_SigBit);
if (msg = (struct IntuiMessage *) GetMsg(window->UserPort)) {
msgClass = msg->Class;
switch (msgClass) {
case IDCMP_CLOSEWINDOW:
done = TRUE;
break;
case IDCMP_MENUPICK:
menuCode = msg->Code;
done = handle_menu(MENUNUM(menuCode), ITEMNUM(menuCode), SUBNUM(menuCode));
break;
case IDCMP_REQCLEAR:
puts("requester closed");
break;
default:
break;
}
ReplyMsg((struct Message *) msg);
}
}
}
void setup_menu()
{
UWORD txWidth, txHeight, txBaseline, txSpacing, itemWidth, itemHeight, numItems;
struct RastPort *rp = &window->WScreen->RastPort;
int i;
txWidth = rp->TxWidth;
txHeight = rp->TxHeight;
txBaseline = rp->TxBaseline;
txSpacing = rp->TxSpacing;
printf("TxWidth: %d, TxHeight: %d, TxBaseline: %d, TxSpacing: %d\n",
(int) txWidth, (int) txHeight, (int) txBaseline, (int) txSpacing);
/* Set file menu bounds */
menus[0].Width = TextLength(rp, "File", strlen("File")) + txWidth;
menus[0].Height = txHeight;
/* Set file menu items bounds */
/* We actually need to know what the command uses up */
itemWidth = txWidth * strlen("Open...") + 50;
itemHeight = txHeight + 2; /* 2 pixels adjustment */
numItems = sizeof(fileMenuItems) / sizeof(struct MenuItem);
printf("# file items: %d\n", (int) numItems);
for (i = 0; i < numItems; i++) {
fileMenuItems[i].TopEdge = i * itemHeight;
fileMenuItems[i].Height = itemHeight;
fileMenuItems[i].Width = itemWidth;
}
SetMenuStrip(window, &menus[0]);
}
struct Image image = { 0, 0, 0, 0, 0, NULL, 0, 0, NULL};
struct NewScreen newscreen = {
0, 0, 320, 256, 5,
0, 1, // pens
0, // view modes
CUSTOMSCREEN, // type
NULL, // font
"My Screen", // title
NULL, // unused (gadgets)
NULL // custom bitmap
};
/* Defined automatically in VBCC */
extern struct Library *DOSBase;
int main(int argc, char **argv)
{
if (argc <= 1) {
puts("Usage: iffview <iff-file>");
return 0;
}
ILBMData *ilbm_data = NULL;
ilbm_data = parse_file(argv[1]);
if (!ilbm_data) {
puts("error reading IFF file");
exit(0);
}
image.Width = ilbm_data->bmheader->w;
image.Height = ilbm_data->bmheader->h;
image.Depth = ilbm_data->bmheader->nPlanes;
// if image.Width must be a multiple of 16
int mod16 = image.Width % 16;
image.Width = mod16 == 0 ? image.Width : image.Width + (16 - mod16);
int wordwidth = image.Width / 16;
int finalsize = wordwidth * image.Height * image.Depth * sizeof(UWORD);
image.ImageData = AllocMem(finalsize, MEMF_CHIP|MEMF_CLEAR);
if (!image.ImageData) {
puts("can't allocate chip memory for image");
free_ilbm_data(ilbm_data);
exit(0);
}
ilbm_to_image_data((char *) image.ImageData, ilbm_data, image.Width, image.Height);
image.PlanePick = (1 << image.Depth) - 1;
/* Adjust the new screen according to the IFF image */
newscreen.ViewModes = ilbm_data->bmheader->camgFlags;
newscreen.Depth = image.Depth;
screen = OpenScreen(&newscreen);
if (screen) {
ShowTitle(screen, FALSE);
for (int i = 0; i < ilbm_data->num_colors; i++) {
ColorRegister *color = &ilbm_data->colors[i];
SetRGB4(&screen->ViewPort, i, color->red >> 4,
color->green >> 4, color->blue >> 4);
}
newwin.Width = newwin.MinWidth = newwin.MaxWidth = newscreen.Width;
newwin.Height = newwin.MinHeight = newwin.MaxWidth = newscreen.Height;
newwin.Screen = screen;
if (window = OpenWindow(&newwin)) {
setup_menu();
// When drawing to a screen's rastport directly, the image must be contained
// within its boundaries otherwise it crashes, draw to a Window and it will be clipped
// Note: rastport is the entire window, including title bars
if (screen) DrawImage(window->RPort, &image, 0, 0);
handle_events();
}
}
cleanup();
if (ilbm_data) free_ilbm_data(ilbm_data);
FreeMem(image.ImageData, finalsize);
return 0;
}

303
iffview/ilbm.c Normal file
View File

@ -0,0 +1,303 @@
/* ilbm.c - Universal IFF ILBM handling module
This file is part of amiga30yrs.
amiga30yrs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
amiga30yrs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with amiga30yrs. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ilbm.h"
#ifdef LITTLE_ENDIAN
#include <byteswap.h>
#endif
#define IFF_HEADER_SIZE 12
#define CHUNK_HEADER_SIZE 8
BitMapHeader *read_BMHD(FILE *fp, int datasize)
{
BitMapHeader *header;
int bytes_read;
header = calloc(1, sizeof(BitMapHeader));
bytes_read = fread(header, sizeof(char), datasize, fp);
#ifdef LITTLE_ENDIAN
header->x = __bswap_16(header->x);
header->y = __bswap_16(header->y);
header->w = __bswap_16(header->w);
header->h = __bswap_16(header->h);
header->transparentColor = __bswap_16(header->transparentColor);
header->pageWidth = __bswap_16(header->pageWidth);
header->pageHeight = __bswap_16(header->pageHeight);
#endif
/*
printf("x: %d y: %d w: %d h: %d\n",
header.x, header.y, header.w, header.h);
printf("# planes: %d\n", header.nPlanes);
printf("# transp col: %d\n", header.transparentColor);
printf("pagewidth: %d pageheight: %d\n", header.pageWidth, header.pageHeight);
*/
if (header->compression == cmpNone) puts("no compression");
else if (header->compression == cmpByteRun1) puts("Byte Run 1 compression");
else puts("unknown compression");
return header;
}
ColorRegister *read_CMAP(FILE *fp, int datasize, int *ncols)
{
int num_colors = datasize / 3, bytes_read;
ColorRegister *colors;
colors = malloc(datasize);
bytes_read = fread(colors, sizeof(ColorRegister), num_colors, fp);
*ncols = num_colors;
return colors;
}
ULONG read_CAMG(FILE *fp, int datasize)
{
ULONG flags, bytes_read;
bytes_read = fread(&flags, sizeof(ULONG), 1, fp);
#ifdef LITTLE_ENDIAN
flags = __bswap_32(flags);
#endif
if (flags & HAM) puts("HAM mode !");
else if (flags & EXTRA_HALFBRITE) puts("Extra Halfbrite mode !");
return flags;
}
void read_CRNG(FILE *fp, int datasize)
{
ULONG bytes_read;
CRange color_reg_range;
bytes_read = fread(&color_reg_range, sizeof(CRange), 1, fp);
#ifdef LITTLE_ENDIAN
color_reg_range.rate = __bswap_16(color_reg_range.rate);
color_reg_range.flags = __bswap_16(color_reg_range.flags);
#endif
#ifdef DEBUG
printf("CRange, rate: %d, flags: %04x, low: %d, high: %d\n",
(int) color_reg_range.rate, (int) color_reg_range.flags,
(int) color_reg_range.low, (int) color_reg_range.high);
#endif
}
UBYTE *read_BODY(FILE *fp, int datasize, BitMapHeader *bmheader, int *data_bytes)
{
ULONG bytes_read;
BYTE *buffer, *dst_buffer;
int src_i = 0, dst_i = 0, dst_size;
buffer = malloc(datasize);
*data_bytes = datasize;
dst_size = bmheader->w * bmheader->h * bmheader->nPlanes / 8;
printf("target size: %d, data size: %d\n", dst_size, datasize);
bytes_read = fread(buffer, sizeof(char), datasize, fp);
if (bmheader->compression == cmpByteRun1) {
BYTE b0, b1;
int i;
/* decompress data */
dst_buffer = malloc(dst_size);
*data_bytes = dst_size;
while (src_i < datasize) {
b0 = buffer[src_i++];
if (b0 >= 0) {
for (i = 0; i < b0 + 1; i++) dst_buffer[dst_i++] = buffer[src_i++];
} else {
b1 = buffer[src_i++];
for (i = 0; i < -b0 + 1; i++) dst_buffer[dst_i++] = b1;
}
}
free(buffer);
return dst_buffer;
}
return buffer;
}
#define skip_chunk(fp, datasize) fseek(fp, datasize, SEEK_CUR)
ILBMData *read_chunks(FILE *fp, int filesize, int total_read)
{
// make sure that our char buffers are aligned on word boundaries
unsigned char buffer[CHUNK_HEADER_SIZE], id[5];
int i, bytes_read, datasize, imgdata_size;
BitMapHeader *bmheader = NULL;
ColorRegister *colors = NULL;
UBYTE *imgdata = NULL;
ILBMData *result = calloc(1, sizeof(ILBMData));
while (total_read < filesize) {
bytes_read = fread(buffer, sizeof(char), 8, fp);
for (i = 0; i < 4; i++) id[i] = buffer[i];
id[4] = 0;
#ifdef LITTLE_ENDIAN
datasize = __bswap_32(*((ULONG *) &buffer[4]));
#else
datasize = *((ULONG *) &buffer[4]);
#endif
if (!strncmp("BMHD", buffer, 4)) bmheader = read_BMHD(fp, datasize);
else if (!strncmp("CMAP", buffer, 4)) colors = read_CMAP(fp, datasize, &result->num_colors);
else if (!strncmp("CRNG", buffer, 4)) read_CRNG(fp, datasize);
else if (!strncmp("CAMG", buffer, 4)) bmheader->camgFlags = read_CAMG(fp, datasize);
else if (!strncmp("BODY", buffer, 4)) imgdata = read_BODY(fp, datasize, bmheader, &imgdata_size);
else {
#ifdef DEBUG
printf("WARNING - Unsupported chunk '%s', size: %d\n", id, datasize);
#endif
skip_chunk(fp, datasize);
}
/* Padding to even if necessary */
if (datasize % 2) {
fseek(fp, 1, SEEK_CUR);
datasize++;
}
total_read += datasize + CHUNK_HEADER_SIZE;
}
printf("total read: %d\n", total_read);
result->imgdata = imgdata;
result->data_bytes = imgdata_size;
result->colors = colors;
result->bmheader = bmheader;
return result;
}
ILBMData *parse_file(const char *path)
{
FILE *fp;
char buffer[IFF_HEADER_SIZE];
size_t bytes_read;
ULONG filesize, total_read = 0;
ILBMData *result = NULL;
fp = fopen(path, "rb");
/* need to check whether we actually can read this much*/
bytes_read = fread(buffer, sizeof(char), IFF_HEADER_SIZE, fp);
total_read += bytes_read;
if (!strncmp("FORM", buffer, 4)) {
#ifdef LITTLE_ENDIAN
puts("little endian");
filesize = __bswap_32(*((ULONG *) &buffer[4])) + CHUNK_HEADER_SIZE;
#else
puts("big endian");
filesize = *((ULONG *) &buffer[4]) + CHUNK_HEADER_SIZE;
#endif
if (!strncmp("ILBM", &buffer[8], 4)) {
#ifdef DEBUG
printf("IFF/ILBM file, file size: %d\n", (int) filesize);
#endif
result = read_chunks(fp, filesize, total_read);
} else {
puts("not an IFF ILBM file");
}
} else {
puts("not an IFF file");
}
fclose(fp);
return result;
}
void free_ilbm_data(ILBMData *data)
{
if (data) {
if (data->colors) free(data->colors);
if (data->bmheader) free(data->bmheader);
if (data->imgdata) free(data->imgdata);
free(data);
}
}
void print_ilbm_info(ILBMData *data)
{
printf("width: %d, height: %d, # planes: %d # colors: %d\n",
(int) data->bmheader->w,
(int) data->bmheader->h,
(int) data->bmheader->nPlanes,
data->num_colors);
}
/*
* ILBM interleaved bit maps -> Intuition Image data
* ILBM is interleaved
* - plane 0/row 0 -> plane 1/row 0 -> ... plane 0/row 1 -> plane 1/row 1 ->...
* - rows are multiples of 8
*
* but Intuition Image expects its data
* - plane-by-plane
* - as a multiple of 16
*/
void ilbm_to_image_data(char *dest, ILBMData *data, int dest_width, int dest_height)
{
if (data->bmheader->w % 8 != 0) {
puts("ERROR: source width must be multiple of 8");
return;
}
if (dest_width % 16 != 0) {
puts("ERROR: destination width must be multiple of 16");
return;
}
if (dest_width < data->bmheader->w || dest_height < data->bmheader->h) {
puts("ERROR: destination dimensions are smaller than source");
return;
}
int src_bytes_per_row = data->bmheader->w / 8;
int dest_bytes_per_row = dest_width / 8;
char *src_ptr = data->imgdata;
int src_offset, img_height = data->bmheader->h, num_planes = data->bmheader->nPlanes;
int row_data_size = num_planes * src_bytes_per_row;
for (int plane = 0; plane < num_planes; plane++) {
// copy row-by-row
for (int row = 0; row < img_height; row++) {
src_offset = (row * row_data_size) + (plane * src_bytes_per_row);
memcpy(dest, src_ptr + src_offset, src_bytes_per_row);
dest += dest_bytes_per_row;
}
}
}
#ifdef STANDALONE
int main(int argc, char **argv)
{
if (argc <= 1) {
puts("usage: ilbm <image-file>");
} else {
ILBMData *data = parse_file(argv[1]);
print_ilbm_info(data);
int dest_width = data->bmheader->w;
int mod16 = dest_width % 16;
if (mod16 > 0) {
dest_width += mod16;
}
int wordwidth = dest_width / 16;
int dest_height = data->bmheader->h;
int finalsize = wordwidth * data->bmheader->h * data->bmheader->nPlanes * 2;
printf("loaded size: %d, final size: %d\n", data->data_bytes, finalsize);
char *dest = calloc(finalsize, sizeof(char));
ilbm_to_image_data(dest, data, dest_width, dest_height);
free(dest);
free_ilbm_data(data);
}
}
#endif

95
iffview/ilbm.h Normal file
View File

@ -0,0 +1,95 @@
/* ilbm.h - Definitions for IFF ILBM handling module
This file is part of amiga30yrs.
amiga30yrs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
amiga30yrs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with amiga30yrs. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __ILBM_H__
#define __ILBM_H__
#ifdef __VBCC__
#include <exec/types.h>
#include <graphics/view.h>
#else
#include <sys/types.h>
#define ULONG u_int32_t
#define UWORD u_int16_t
#define UBYTE u_int8_t
#define LONG int32_t
#define WORD int16_t
#define BYTE int8_t
typedef int BOOL;
#define TRUE 1
#define FALSE 0
#define HAM 0x800
#define EXTRA_HALFBRITE 0x80
#endif
/*
* From the specification
*/
typedef UBYTE Masking; /* Choice of masking technique. */
#define mskNone 0
#define mskHasMask 1
#define mskHasTransparentColor 2
#define mskLasso 3
typedef UBYTE Compression;
#define cmpNone 0
#define cmpByteRun1 1
typedef struct {
UWORD w, h;
WORD x, y;
UBYTE nPlanes;
Masking masking;
Compression compression;
UBYTE pad1;
UWORD transparentColor;
UBYTE xAspect, yAspect;
WORD pageWidth, pageHeight;
ULONG camgFlags;
} BitMapHeader;
/* Colors are in the range 0-255 */
typedef struct {
UBYTE red, green, blue;
} ColorRegister;
typedef struct {
WORD pad1;
WORD rate;
WORD flags;
UBYTE low, high;
} CRange;
typedef struct _ILBMData {
BitMapHeader *bmheader;
int num_colors, data_bytes;
ColorRegister *colors;
UBYTE *imgdata;
} ILBMData;
extern ILBMData *parse_file(const char *path);
extern void free_ilbm_data(ILBMData *data);
extern void ilbm_to_image_data(char *dest, ILBMData *data, int dest_width, int dest_height);
#endif /* __ILBM_H__ */