ftp.nice.ch/pub/next/graphics/bitmap/dips.1.00.s.tar.gz#/GUI_DIPS/_gui_dips/Mask.m

This is Mask.m in view mode; [Download] [Up]

/* Generated by Interface Builder */

#import	<stdio.h>
#import	<string.h>
#import	<stdlib.h>
#import	<libc.h>
#import	"Mask.h"
#import	<appkit/Text.h>
#import	<appkit/Matrix.h>
#import	<appkit/Form.h>
#import	<appkit/Cell.h>
#import	<appkit/Application.h>
#import	<appkit/OpenPanel.h>

@implementation Mask

- updateSize:sender
{
	int	i,j;
	int	sz;
	int	w, h;
	float	**old_msk;
	int	old_sz;
	NXRect	mrect;
	NXRect	srect;
	NXSize	cellSize;

  [self new: self];
  old_msk = [self getFloatMask: &old_sz];
  sz = [size intValueAt: 0];
  if(sz != old_sz) 
    if(sz > 0) {
      [[mask renewRows:sz cols:sz] sizeToCells];
      for(i=0;i<sz;i++) 
        for(j=0;j<sz;j++) {
          [[[mask cellAt:i :j] setAlignment: NX_CENTERED] setBordered: YES];
          if(i < old_sz && j < old_sz)	// set old rows and cols
            [[mask cellAt:i :j] setFloatValue: old_msk[i][j]];
          if(sz > old_sz && i >= old_sz)	// init new rows
            [[mask cellAt:i :j] setFloatValue: 0.0];
          if(sz > old_sz && j >= old_sz)	// init new cols
            [[mask cellAt:i :j] setFloatValue: 0.0];
        }
      [mask getBounds: &mrect];
      [mask getCellSize:&cellSize];
      [size getBounds: &srect];
      h = mrect.size.height + cellSize.height + 
          srect.size.height;
      w = mrect.size.width + cellSize.width;
      if(w < srect.size.width)
        w = srect.size.width;
      [maskWindow sizeWindow:w :h];
      [maskWindow display];
      NXPing();
    }
    else {
      NXRunAlertPanel("MASK","Illegal Size",NULL,NULL,NULL);
      return nil;
    }

  cur_size = sz;
  changed = YES;
  return self;
}

- (int) getsize
{
  return cur_size;
}


- (float) getval: (int)x :(int) y
{
  return [[mask cellAt:x :y] floatValue];
}


- new:sender
{
  free(file);
  file = (char *)malloc((strlen("Untitled.mask")+1)*sizeof(char));
  strcpy(file,"Untitled.mask");
  [maskWindow setTitle: file];
  return self;
} 

- open:sender
{
	char 	tpath [500];
	char	tfile[500];
	char	*opentypes[2] = {"mask",NULL};
	id	openpanel;
	int 	loop;
    
  openpanel = [OpenPanel new];
  [openpanel allowMultipleFiles: NO];
  if([openpanel runModalForTypes:opentypes]) {
    loop = 0;
    while ([openpanel filenames] [loop]) {
      strcpy (tpath, [openpanel directory]);
      strcpy (tfile, [openpanel filenames] [loop]);
      file = (char *)malloc((strlen(tfile)+1)*sizeof(char));
      strcpy(file,tfile);
      path = (char *)malloc((strlen(tpath)+1)*sizeof(char));
      strcpy(path,tpath);
      [self openfile];
      ++loop;
    }
  }
  return self;
}

- openfile
{
	FILE	*fp;
	int	i,j;
	int	sz;
	char	fullPath[500];
	char	*newfile;
	float	val;

  strcpy(fullPath,path);
  strcat(fullPath,"/");
  strcat(fullPath,file);
  newfile = (char *)malloc((strlen(file)+1)*sizeof(char));
  strcpy(newfile,file);
  if((fp = fopen(fullPath,"r")) == NULL) {
    NXRunAlertPanel("MASK","Can't open mask file",NULL,NULL,NULL);
    return nil;
  }
  fscanf(fp,"%d",&sz);
  [size setIntValue: sz at:0];
  [self updateSize: self];		// this overrides filename
  for(i=0;i<sz;i++) {
    for(j=0;j<sz;j++) {
      fscanf(fp,"%f",&val);
      [[mask cellAt:i :j] setFloatValue: val];
    }
  }
  fclose(fp);
  file = (char *)malloc((strlen(newfile)+1)*sizeof(char));
  strcpy(file,newfile);
  [maskWindow setTitle: file];
  [maskWindow makeKeyAndOrderFront:maskWindow];
  [maskWindow display];
  changed = YES;
  return self;
}

- save:sender
{
	FILE	*fp;
	int	i,j;
	int	sz;
	char	fullPath[500];
	float	**msk;

  if(!path || !file)
    [self saveAs: self];
  strcpy(fullPath,path);
  strcat(fullPath,"/");
  strcat(fullPath,file);
  if((fp = fopen(fullPath,"w")) == NULL) {
    NXRunAlertPanel("MASK","Can't open file for writing",NULL,NULL,NULL);
    return nil;
  }
  msk = [self getFloatMask: &sz];
  fprintf(fp,"%d\n",sz);
  for(i=0;i<sz;i++) {
    for(j=0;j<sz;j++) {
      fprintf(fp,"%f ",[[mask cellAt:i :j] floatValue]);
    }
    fprintf(fp,"\n");
  }
  fclose(fp);
 
  return self;
}

- saveAs:sender
{
	char 	tpath [500];
	char	tfile[500];
	id	savepanel;
    
  savepanel = [SavePanel new];
  if([savepanel runModal]) {
    strcpy (tpath, [savepanel directory]);
    strcpy (tfile, strrchr([savepanel filename],'/'));
    strcat (tfile, ".mask");
    file = (char *)malloc((strlen(tfile)+1)*sizeof(char));
    strcpy(file,tfile);
    path = (char *)malloc((strlen(tpath)+1)*sizeof(char));
    strcpy(path,tpath);
    [self save: self];
    [maskWindow setTitle: file];
  }
  return self;
}

- (BOOL) hasNotChanged
{
  if(changed) {
    changed = NO;
    return YES;
  }
  else return YES;
}

- (float **) getFloatMask: (int *)maskSize
{
	int	i,j;
	int	rows, cols;
	float	**theMask;

  [mask getNumRows: &rows numCols: &cols];
  *maskSize = rows;

  theMask = (float **)malloc(rows*sizeof(float *));
  for(i=0;i<rows;i++) theMask[i] = (float *)malloc(cols*sizeof(float));

  for(i=0;i<rows;i++) 
    for(j=0;j<cols;j++)
      theMask[i][j] = [[mask cellAt:i :j] floatValue];

  return theMask;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.