1#import "Image.h"
2#import <stdio.h>
3#import <stdlib.h>
4#import <strings.h>
5#import <libc.h>
6#import <zone.h>
7#import <mach.h>
8#import <streams/streams.h>
9#import <sys/file.h>
10#import <appkit/nextstd.h>
11#import <appkit/NXBitmapImageRep.h>
12#import <appkit/Window.h>
13#import <appkit/Panel.h>
14#import <appkit/graphics.h>
15#import <appkit/tiff.h>
16
17// Image.m
18//
19// Written by Vince DeMarco
20// 	demarco@cpsc.ucalgary.ca
21//
22// This program is In the Public Domain. If you make any improvements to this
23// program please let me know
24//
25
26@implementation Image:View
27
28- init
29{
30    self = [super init];
31    newZone = NXCreateZone(vm_page_size,vm_page_size,NO);
32    numColors = 3;
33    return(self);
34}
35
36- displayImage
37{
38
39    unsigned char *planes[4];
40
41    NXSetRect(&temprect, 150.0, (800.0 - (double)ysize),(double)xsize, (double)ysize);
42
43    thiswindow = [[Window allocFromZone:newZone]
44		  initContent:&temprect
45		  style:NX_TITLEDSTYLE
46		  backing:NX_RETAINED
47		  buttonMask:NX_CLOSEBUTTONMASK | NX_MINIATURIZEBUTTONMASK | NX_RESIZEBUTTONMASK
48		  defer:NO];
49
50    [thiswindow setDelegate:self];
51    [thiswindow setFreeWhenClosed:YES];
52    [thiswindow setTitleAsFilename:fileName];
53    if (windowIconName[0]){
54	[thiswindow setMiniwindowIcon:(const char *)windowIconName];
55    }
56
57    [thiswindow makeKeyAndOrderFront:nil];
58    [thiswindow display];
59    NXPing();
60
61    if (r == NULL){     // Check if there is anything to display if there isn't
62	return self;    // just return
63    }
64
65    planes[0] = r;
66    planes[1] = g;
67    planes[2] = b;
68    planes[3] = a;
69
70    tiffimage = [[NXBitmapImageRep allocFromZone:newZone]
71		     initDataPlanes:  planes
72		     pixelsWide:      xsize
73		     pixelsHigh:      ysize
74		     bitsPerSample:   8
75		     samplesPerPixel: numColors
76		     hasAlpha:        (numColors == 4 ? YES: NO )
77		     isPlanar:        (numColors == 1 ? NO : YES)
78		     colorSpace:      (numColors >= 3 ? NX_RGBColorSpace : NX_OneIsWhiteColorSpace)
79		     bytesPerRow:     xsize*8
80		     bitsPerPixel:    8];
81
82    [thiswindow setContentView:self];
83    [thiswindow display];
84    return(self);
85}
86
87- drawSelf:(const NXRect *)rects :(int)rectCount
88{
89    [self getFrame:&temprect];
90    temprect.origin.x = 0.0;
91    temprect.origin.y = 0.0;
92
93    [self lockFocus];
94    [tiffimage drawIn:&temprect];
95    [self unlockFocus];
96    return self;
97}
98
99- free
100{
101    NX_FREE(a);
102    NX_FREE(r);
103    NX_FREE(g);
104    NX_FREE(b);
105    [super free];
106    return(self);
107}
108
109- saveAsTiff:(const char *)filename usingCompression:(int)compression
110{
111    int fd;
112    NXStream *stream;
113
114    if ( (fd = open(filename,(O_RDWR|O_CREAT),0600)) < 0){
115	return(self);
116    }
117    stream = NXOpenFile(fd,NX_READWRITE);
118    [tiffimage writeTIFF:stream usingCompression:compression];
119    NXClose(stream);
120    close(fd);
121    return(self);
122}
123
124- saveAsEPS:(const char *)filename
125{
126    int fd;
127    NXStream *stream;
128
129    if ( (fd = open(filename,(O_RDWR|O_CREAT),0600)) < 0){
130	return(self);
131    }
132    stream = NXOpenFile(fd,NX_READWRITE);
133    [self copyPSCodeInside:&temprect to:stream];
134    NXClose(stream);
135    close(fd);
136    return(self);
137}
138
139// window Delegation methods
140- windowWillClose:sender
141{
142    NX_FREE(a); // Free the alpha,red,green, and blue buffers
143    NX_FREE(r);
144    NX_FREE(g);
145    NX_FREE(b);
146    return(self);
147}
148
149@end
150