Exemplo n.º 1
0
/*
 * Check the local screen buffer before actually updating
 * the screen
 */
static void editor_putchar(uint col, uint row, uchar c)
{
  uchar buff_c = 0;
  ul_t screen_offset = col + (row-1)*SCREEN_WIDTH;
  lmemcpy(lp(&buff_c), screen_buff + screen_offset, 1L);

  if(col==mouse_x && row==mouse_y) { /* Draw mouse where it's located */
    c = '+';
  }

  if(c != buff_c) {
    lmemcpy(screen_buff + screen_offset, lp(&c), 1L);
    putchar_attr(col, row, c, EDITOR_ATTRIBUTES);
  }
}
Exemplo n.º 2
0
static DWORD WINAPI ListenEnv( LPVOID lpParameter )
{
    HANDLE pipe = (HANDLE)lpParameter;

    connected = true;
    ConnectNamedPipe( pipe, 0 );

    char buffer[MAX_PATH];
    char* envs = 0;
    
    DWORD read = 0;
    int len = 0;

    while( ReadFile( pipe, buffer, MAX_PATH, &read, 0 ) || read )
    {
        envs = (char*)realloc( envs, len + read );
        lmemcpy( envs + len, buffer, read );
        len += read;
        read = 0;
    }

    if( envs )
    {
        wchar_t* var = (wchar_t*)envs;
        while( *var )
        {
            int len = lstrlenW( var );
            if( lstrchr( var + 1, '=' ) > 0 )
            {
                int pos = lstrchr( var + 1, '=' ) + 1;
                var[pos] = 0;
                while( lstrlenW( var ) && var[lstrlenW( var ) - 1] <= ' ' )
                    var[lstrlenW( var ) - 1] = 0;
                wchar_t* val = var + pos + 1;
                while( *val && *val <= ' ' )
                    val++;
                SetEnvironmentVariableW( var, val );
            }
            var += len + 1;
        }

        free( envs );
    }
    DisconnectNamedPipe(pipe);
    return 0;
}
Exemplo n.º 3
0
/*
 * Set a char of a far memory string at a given offset
 * Far memory needs to be accessed thorugh its API
 */
static void setlc(lp_t ptr, ul_t offset, uchar c)
{
  lmemcpy(ptr + offset, lp(&c), 1L);
}
Exemplo n.º 4
0
/*
 * Get first char of a far memory string.
 * Far memory needs to be accessed thorugh its API
 */
static uchar getlc(lp_t ptr)
{
  uchar c = 0;
  lmemcpy(lp(&c), ptr, 1L);
  return c;
}
Exemplo n.º 5
0
/*
 * Program entry point
 */
uint main(uint argc, uchar* argv[])
{
  uchar* title_info = "L:     F1:Save ESC:Exit"; /* const */
  uchar  line_ibcd[4]; /* To store line number digits */
  uint   ibcdt;

  uint i = 0;
  uint n = 0;
  uint result = 0;

  /* buff is fixed size and allocated in far memory so it
   * can be big enough.
   * buff_size is the size in bytes actually used in buff
   * buff_cursor_offset is the linear offset of current
   * cursor position inside buff
   */
  lp_t buff = 0;
  ul_t buff_size = 0;
  ul_t buff_cursor_offset = 0;

  /* First line number to display in the editor area */
  uint current_line = 0;

  /* Var to get key presses */
  uint k = 0;

  struct FS_ENTRY entry;

  /* Chck usage */
  if(argc != 2) {
    putstr("Usage: %s <file>\n\r\n\r", argv[0]);
    putstr("<file> can be:\n\r");
    putstr("-an existing file path: opens existing file to edit\n\r");
    putstr("-a new file path: opens empty editor. File will be created on save\n\r");
    putstr("\n\r");
    return 1;
  }

  /* Allocate fixed size text buffer */
  buff = lmalloc(0xFFFFL);
  if(buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    return 1;
  }

  /* Find file */
  n = get_entry(&entry, argv[1], UNKNOWN_VALUE, UNKNOWN_VALUE);

  /* Load file or show error */
  if(n<ERROR_ANY && (entry.flags & FST_FILE)) {
    ul_t offset = 0;
    uchar cbuff[512];
    setlc(buff, entry.size, 0);
    buff_size = entry.size;
    while(result = read_file(cbuff, argv[1], (uint)offset, sizeof(cbuff))) {
      if(result >= ERROR_ANY) {
        lmfree(buff);
        putstr("Can't read file %s (error=%x)\n\r", argv[1], result);
        return 1;
      }
      lmemcpy(buff + offset, lp(cbuff), (ul_t)result);
      offset += result;
    }
    if(offset != entry.size) {
      lmfree(buff);
      putstr("Can't read file (readed %d bytes, expected %d)\n\r",
        (uint)offset, entry.size);
      return 1;
    }
    /* Buffer must finish with a 0 and */
    /* must fit at least this 0, so buff_size can't be 0 */
    if(buff_size == 0 || getlc(buff + buff_size-1L) != 0) {
      buff_size++;
    }
  }

  /* Create 1 byte buffer if this is a new file */
  /* This byte is for the final 0 */
  if(buff_size == 0) {
    buff_size = 1;
    lmemset(buff, 0, buff_size);
  }

  /* Get screen size */
  get_screen_size(SSM_CHARS, &SCREEN_WIDTH, &SCREEN_HEIGHT);

  /* Allocate screen buffer */
  screen_buff = lmalloc((ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));
  if(screen_buff == 0) {
    putstr("Error: can't allocate memory\n\r");
    lmfree(buff);
    return 1;
  }

  /* Clear screen buffer */
  lmemset(screen_buff, 0, (ul_t)(SCREEN_WIDTH*(SCREEN_HEIGHT-1)));

  /* Write title */
  for(i=0; i<strlen(argv[1]); i++) {
    putchar_attr(i, 0, argv[1][i], TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH-strlen(title_info); i++) {
    putchar_attr(i, 0, ' ', TITLE_ATTRIBUTES);
  }
  for(; i<SCREEN_WIDTH; i++) {
    putchar_attr(i, 0,
      title_info[i+strlen(title_info)-SCREEN_WIDTH], TITLE_ATTRIBUTES);
  }

  /* Show buffer and set cursor at start */
  set_show_cursor(HIDE_CURSOR);
  show_buffer_at_line(buff, current_line);
  set_cursor_position(0, 1);
  set_show_cursor(SHOW_CURSOR);

  /* Main loop */
  while(k != KEY_ESC) {
    uint col, line;

    /* Getmouse state */
    get_mouse_state(SSM_CHARS, &mouse_x, &mouse_y, &mouse_buttons);

    /* Process buttons */
    if(mouse_buttons & MOUSE_LEFT_BUTTON) {
      buff_cursor_offset = linecol_to_buffer_offset(buff, mouse_x, mouse_y-1);
    }

    /* Get key press */
    k = getkey(KM_NO_WAIT);

    /* Process key actions */

    /* Keys to ignore */
    if((k>KEY_F1 && k<=KEY_F10) ||
      k==KEY_F11 || k==KEY_F12 ||
      k==KEY_PRT_SC || k==KEY_INS ||
      k==0) {
      continue;

    /* Key F1: Save */
    } else if(k == KEY_F1) {
      ul_t offset = 0;
      uchar cbuff[512];
      result = 0;
      while(offset<buff_size && result<ERROR_ANY) {
        ul_t to_copy = min(sizeof(cbuff), buff_size-offset);
        lmemcpy(lp(cbuff), buff + offset, to_copy);
        result = write_file(cbuff, argv[1], (uint)offset, (uint)to_copy, FWF_CREATE | FWF_TRUNCATE);
        offset += to_copy;
      }

      /* Update state indicator */
      if(result < ERROR_ANY) {
        putchar_attr(strlen(argv[1]), 0, ' ', TITLE_ATTRIBUTES);
      } else {
        putchar_attr(strlen(argv[1]), 0, '*', (TITLE_ATTRIBUTES&0xF0)|AT_T_RED);
      }
      /* This opperation takes some time, so clear keyboard buffer */
      getkey(KM_CLEAR_BUFFER);

    /* Cursor keys: Move cursor */
    } else if(k == KEY_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      if(line > 0) {
        line -= 1;
        buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);
      }

    } else if(k == KEY_DOWN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += 1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_LEFT) {
      if(buff_cursor_offset > 0) {
        buff_cursor_offset--;
      }

    } else if(k == KEY_RIGHT) {
      if(buff_cursor_offset < buff_size - 1) {
        buff_cursor_offset++;
      }

    /* HOME, END, PG_UP and PG_DN keys */
    } else if(k == KEY_HOME) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_END) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      col = 0xFFFF;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_DN) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line += SCREEN_HEIGHT-1;
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);

    } else if(k == KEY_PG_UP) {
      buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
      line -= min(line, SCREEN_HEIGHT-1);
      buff_cursor_offset = linecol_to_buffer_offset(buff, col, line);


    /* Backspace key: delete char before cursor and move cursor there */
    } else if(k == KEY_BACKSPACE) {
      if(buff_cursor_offset > 0) {
        lmemcpy(buff+buff_cursor_offset-1L, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
        buff_cursor_offset--;
      }

    /* Del key: delete char at cursor */
    } else if(k == KEY_DEL) {
      if(buff_cursor_offset < buff_size-1) {
        lmemcpy(buff+buff_cursor_offset, buff+buff_cursor_offset+1, buff_size-buff_cursor_offset-1);
        buff_size--;
        putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
      }

    /* Any other key but esc: insert char at cursor */
    } else if(k != KEY_ESC && k != 0) {

      if(k == KEY_RETURN) {
        k = '\n';
      }
      if(k == KEY_TAB) {
        k = '\t';
      }
      lmemcpy(buff+buff_cursor_offset+1, buff+buff_cursor_offset, buff_size-buff_cursor_offset);
      setlc(buff, buff_cursor_offset++, k);
      buff_size++;
      putchar_attr(strlen(argv[1]), 0, '*', TITLE_ATTRIBUTES);
    }

    /* Update cursor position and display */
    buffer_offset_to_linecol(buff, buff_cursor_offset, &col, &line);
    if(line < current_line) {
      current_line = line;
    } else if(line > current_line + SCREEN_HEIGHT - 2) {
      current_line = line - SCREEN_HEIGHT + 2;
    }

    /* Update line number in title */
    /* Compute bcd value (reversed) */
    ibcdt = min(9999, buffer_offset_to_fileline(buff, buff_cursor_offset)+1);
    n = SCREEN_WIDTH-strlen(title_info)+2;
    for(i=0; i<4; i++) {
      line_ibcd[i] = ibcdt%10;
      ibcdt /= 10;
      if(ibcdt==0) {
        ibcdt = i;
        break;
      }
    }
    /* Display it */
    for(i=0; i<4; i++) {
      uchar c = i<=ibcdt?line_ibcd[ibcdt-i]+'0':' ';
      putchar_attr(n+i, 0, c, TITLE_ATTRIBUTES);
    }

    set_show_cursor(HIDE_CURSOR);
    show_buffer_at_line(buff, current_line);
    line -= current_line;
    line += 1;
    set_cursor_position(col, line);
    set_show_cursor(SHOW_CURSOR);
  }

  /* Free screen buffer */
  lmfree(screen_buff);

  /* Free buffer */
  lmfree(buff);

  /* Reset screen */
  clear_screen();
  set_cursor_position(0, 0);

  return 0;
}
Exemplo n.º 6
0
int TiffWriteFrame(
	int      oFile,
	LPSTR     lpFileName,
	LPOBJECT lpObject,
	LPFRAME  lpFrame,
	LPRECT   lpRect,
	int      flag,
	BOOL     fCompressed,
	BOOL     bEscapable)
{
	TAG    tag;
	int    y, bpl, npix, nlin, ofp;
	LPLONG lngptr,boffptr;
	LPWORD shtptr;
	DWORD  byteoffset;
	WORD   i, numtags, photomet, samples;
	BYTE   bpp;
	LPWORD lpRed, lpGreen, lpBlue;
	RGBS   RGBmap[256];
	LPTR   lp, lpBuffer, lpOutputPointer, lpImgScanline;
	FNAME  temp;
	RECT   rSave;
	BOOL   compressInit;
#ifdef STATIC16 // only in new framelib
	CFrameTypeConvert FrameTypeConvert;
	FRMTYPEINFO SrcTypeInfo;	
	FRMTYPEINFO DstTypeInfo;	
#endif

	lpBuffer      = NULL;
	lpImgScanline = NULL;

	if (!lpFrame)
		return( -1 );

	ProgressBegin(1,0);

	if ((ofp = oFile) < 0)
		bEscapable = !FileExists(lpFileName);

	if ((ofp = oFile) < 0 && (ofp = _lcreat(lpFileName,0)) < 0)
	{
		Message( IDS_EWRITE, lpFileName );
		goto Exit;
	}

	if (lpRect)
		rSave = *lpRect;
	else
	{
		rSave.top    = rSave.left = 0;
		rSave.bottom = FrameYSize(lpFrame)-1;
		rSave.right  = FrameXSize(lpFrame)-1;
	}

	npix = RectWidth(&rSave);
	nlin = RectHeight(&rSave);

	switch(flag)
	{
		case IDC_SAVELA :
		case IDC_SAVESP :
			bpp      = 1;
			bpl      = ((npix + 7) / 8);
			numtags  = 11;
			photomet = 1;
			samples  = 1;
		break;

		case IDC_SAVECT :
			bpp      = 8;
			bpl      = npix;
			numtags  = 11;
			photomet = 1;
			samples  = 1;
		break;

		case IDC_SAVE4BITCOLOR :
		case IDC_SAVE8BITCOLOR :
			bpp      = 8;
			bpl      = npix;
			numtags  = 12;
			photomet = 3;
			samples  = 1;
		break;

		case IDC_SAVE24BITCOLOR :
			bpp      = 24;
			bpl      = npix * 3;
			numtags  = 11;
			photomet = 2;
			samples  = 3;
		break;

		case IDC_SAVE32BITCOLOR :
			bpp      = 32;
			bpl      = npix * 4;
			numtags  = 11;
			photomet = 5;
			samples  = 4;
		break;

		default :
			goto Exit;
		break;
	}

	compressInit = NO;

	if ( bpp == 1 )
	{
		AllocLines( &lpBuffer,      1, npix, 2 );
		AllocLines( &lpImgScanline, 1, npix, 1 );
	}
	else
	{
		AllocLines( &lpBuffer,      1, max(bpl, FrameByteWidth(lpFrame)), 1 );
		AllocLines( &lpImgScanline, 1, max(bpl, FrameByteWidth(lpFrame)), 1 );
	}

	if ( !lpBuffer || !lpImgScanline )
	{
		Message( IDS_EMEMALLOC );
		_lclose( ofp );
		goto Exit;
	}

	/* first location where any extra data can be stored */
	/* 10 byte header + all tag data (12 bytes each) + 4 bytes (null ifd) */
	byteoffset = 10 + (numtags * sizeof(TAG)) + 4;

	shtptr = (LPWORD)LineBuffer[0];
	SetNextWord(&shtptr, 0x4949);   /* byte order is LSB,MSB */
	SetNextWord(&shtptr, 0x2a);     /* tiff version number */
	SetNextWord(&shtptr, 8);        /* byte offset to first image file directory LSW */
	SetNextWord(&shtptr, 0);        /* byte offset to first image file directory MSW */
	SetNextWord(&shtptr, numtags);  /* number of entries in IFD */

	tag.tagno  = 0xff;    /* tag 0xff, subfile type */
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = 1;       /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x100;   /* tag 0x100, number of pixels */
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = npix;    /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x101;   /* tag 0x101, number of lines */
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = nlin;    /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x102;   /* tag 0x102, bits per sample */
	tag.type   = 3;       /* field type is short */
	tag.length = samples; /* number of values */

	if ( samples == 3 || samples == 4)
	{
		tag.value = byteoffset;	/* deferred value */
		byteoffset += (samples*sizeof(short));
	}
	else
		tag.value = bpp;  /* value */

#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x103;   /* tag 0x103, compression */
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = (fCompressed ? 5:1); /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x106;	  /* tag 0x106,photometric inter.(0 = black) */
	tag.type   = 3;	      /* field type is short */
	tag.length = 1;	      /* number of values */
	tag.value  = photomet;	/* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x111;   /* tag 0x111, strip byte offsets */
	tag.type   = 4;       /* field type is long */
	tag.length = 1;       /* number of values */
	tag.value  = 0;       /* dummy location of the start of image data */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	boffptr = (LPLONG)(shtptr+4);  // make boffptr point at tag.value
	shtptr += 6;

	tag.tagno  = 0x115;   /* tag 0x115, samples per pixel*/
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = samples; /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x11a;   /* tag 0x11a, xresolution */
	tag.type   = 5;       /* field type is rational */
	tag.length = 1;       /* number of values */
	tag.value = byteoffset;	/* deferered value */
	byteoffset += 8;
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x11b;   /* tag 0x11b, yresolution */
	tag.type   = 5;       /* field type is rational */
	tag.length = 1;       /* number of values */
	tag.value  = byteoffset; /* deferred value */
	byteoffset += 8;
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	tag.tagno  = 0x11c;   /* tag 0x11c, planar configuration */
	tag.type   = 3;       /* field type is short */
	tag.length = 1;       /* number of values */
	tag.value  = 1;       /* value */
#ifdef _MAC
	SwapTag(&tag);
#endif	
	lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
	shtptr += 6;

	if ( photomet == 3 ) // Palette color map
	{
		tag.tagno  = 0x140;      /* tag 0x140, colormap */
		tag.type   = 3;          /* field type is short */
		tag.length = 3*256;      /* number of values */
		tag.value  = byteoffset; /* value */
		byteoffset += (2*3*256);
#ifdef _MAC
	SwapTag(&tag);
#endif	
		lmemcpy((LPTR)shtptr,(LPTR)&tag.tagno,12);
		shtptr += 6;
	}

	// Copy the NULL Image File Directory pointer
	SetNextWord(&shtptr, 0); /* pointer to next IFD */
	SetNextWord(&shtptr, 0);

	// Copy out the Bits Per Sample, if multiple samples
	if ( samples == 3 )  // The bits per pixel per sample
	{
		SetNextWord(&shtptr, 8);
		SetNextWord(&shtptr, 8);
		SetNextWord(&shtptr, 8);
	}

	// Copy out the Bits Per Sample, if multiple samples
	if ( samples == 4 )  // The bits per pixel per sample
	{
		SetNextWord(&shtptr, 8);
		SetNextWord(&shtptr, 8);
		SetNextWord(&shtptr, 8);
		SetNextWord(&shtptr, 8);
	}

	// Copy out the X and Y resolution fields
	lngptr = (LPLONG)shtptr;

#ifdef PPVIDEO
	SetNextLong(&lngptr, FrameResolution(lpFrame) * 2); /* xreso numerator */
	SetNextLong(&lngptr, 2);							/* xreso denominator */
	SetNextLong(&lngptr, FrameResolution(lpFrame) * 2); /* yreso numerator */
	SetNextLong(&lngptr, 2);							/* yreso denominator */
#else
	SetNextLong(&lngptr, FrameResolution(lpFrame));		/* xreso numerator */
	SetNextLong(&lngptr, 1);							/* xreso denominator */
	SetNextLong(&lngptr, FrameResolution(lpFrame));		/* yreso numerator */
	SetNextLong(&lngptr, 1);							/* yreso denominator */
#endif

	*boffptr = byteoffset;
#ifdef _MAC
	swapl((LPDWORD)boffptr);
#endif	

	// Write out the tags, the bpp, and the resolutions
	i = (LPTR)lngptr - (LPTR)LineBuffer[0];
	if ( _lwrite(ofp, LineBuffer[0], i) != i )
		goto BadWrite;

	// Write the color palette, if necessary
	if ( photomet == 3 ) // Palette color map
	{
		if (!OptimizeBegin(lpObject, lpFrame, RGBmap, 256, 
			NULL /*(LPROC)AstralClockCursor*/,  // No Progress report
			NO, Convert.fOptimize, Convert.fScatter, Convert.fDither, npix))
			goto BadWrite;

		lpRed   = (LPWORD)LineBuffer[0];
		lpGreen = lpRed   + 256;
		lpBlue  = lpGreen + 256;
		for ( i=0; i<256; i++ )
		{
			*lpRed++   = (WORD)RGBmap[i].red   << 8;
			*lpGreen++ = (WORD)RGBmap[i].green << 8;
			*lpBlue++  = (WORD)RGBmap[i].blue  << 8;
		}
		if ( _lwrite(ofp, LineBuffer[0], 2*3*256) != 2*3*256 )
			goto BadWrite;
	}

	if ( fCompressed )
	{
		if ( CompressLZW( ofp, NULL, 0 ) < 0 ) /* Initialize */
			goto BadWrite;
		compressInit = YES;
	}

	switch(bpp)
	{
		case 1 :
			for( y=rSave.top; y<=rSave.bottom; y++ )
			{
				if (AstralClockCursor( y-rSave.top, nlin, bEscapable ))
					goto Cancelled;

				if ( lpObject )
				{
					if (!ImgGetLine( NULL, lpObject, rSave.left, y,
						(rSave.right - rSave.left) + 1, lpImgScanline))
						goto BadRead;
					lp = lpImgScanline;
				}
				else
				{
					if ( !(lp = FramePointer( lpFrame, rSave.left, y, NO )) )
						goto BadRead;
				}

				if (FrameDepth(lpFrame) == 0)
				{
					if (flag == IDC_SAVESP)
						diffuse( 0, i, 0, NULL, lp, npix, lpBuffer );
					else
						con2la( lp, npix, lpBuffer );
				}
				else
				{
					ConvertData( lp, FrameDepth(lpFrame), npix, lpBuffer+npix, 1 );
					if ( flag == IDC_SAVESP )
						diffuse( 0, i, 0, NULL, lpBuffer+npix, npix, lpBuffer );
					else
						con2la( lpBuffer+npix, npix, lpBuffer );
				}

				if ( fCompressed )
				{
					if ( CompressLZW( ofp, lpBuffer, bpl ) < 0 )
						goto BadWrite;
				}
				else
				{
					if ( _lwrite( ofp, (LPSTR)lpBuffer, bpl ) != bpl )
						goto BadWrite;
				}
			}
		break;

		case 8 :
			for( y=rSave.top; y<=rSave.bottom; y++ )
			{
				if (AstralClockCursor( y-rSave.top, nlin, bEscapable ))
					goto Cancelled;

				if ( lpObject )
				{
					if (!ImgGetLine( NULL, lpObject, rSave.left, y,
						(rSave.right - rSave.left) + 1, lpImgScanline))
						goto BadRead;
					lp = lpImgScanline;
				}
				else
				{
					if ( !(lp = FramePointer( lpFrame, rSave.left, y, NO )) )
						goto BadRead;
				}

				if (FrameDepth(lpFrame) == 0)
				{
					if ( photomet == 3 ) // If we are storing palette color
						OptimizeData(0, y, npix, lp, lpBuffer, 1 );
					else
						ConvertData( lp, 1, npix, lpBuffer, 1 );
				}
				else
				{
					if ( photomet == 3 ) // If we are storing palette color
						OptimizeData(0, y, npix, lp, lpBuffer, FrameDepth(lpFrame));
					else
						ConvertData( lp, FrameDepth(lpFrame), npix, lpBuffer, 1 );
				}
					
				if ( fCompressed )
				{
					if ( CompressLZW( ofp, lpBuffer, bpl ) < 0 )
						goto BadWrite;
				}
				else
				{
					if ( _lwrite( ofp, (LPSTR)lpBuffer, bpl ) != bpl )
						goto BadWrite;
				}
			}
		break;

		case 24 :
			for( y=rSave.top; y<=rSave.bottom; y++ )
			{
				if (AstralClockCursor( y-rSave.top, nlin, bEscapable ))
					goto Cancelled;

				if ( lpObject )
				{
					if (!ImgGetLine( NULL, lpObject, rSave.left, y,
						(rSave.right - rSave.left) + 1, lpImgScanline))
						goto BadRead;
					lp = lpImgScanline;
				}
				else
				{
					if ( !(lp = FramePointer( lpFrame, rSave.left, y, NO )) )
						goto BadRead;
				}

				if (FrameType(lpFrame) != FDT_RGBCOLOR)
				{
					if (FrameType(lpFrame) != FDT_LINEART)
					{
#ifdef STATIC16
						SrcTypeInfo.DataType = FrameType(lpFrame);
						SrcTypeInfo.ColorMap = NULL;
						SrcTypeInfo.DataType = FDT_RGBCOLOR;
						SrcTypeInfo.ColorMap = NULL;

						FrameTypeConvert.Init(SrcTypeInfo, DstTypeInfo, npix);
						FrameTypeConvert.ConvertData((LPTR)lp, (LPTR)lpBuffer, y, npix);
#else					
						FrameTypeConvert(
							(LPTR)lp, FrameType(lpFrame), NULL,
							y,
							(LPTR)lpBuffer, FDT_RGBCOLOR, NULL,
							npix);
#endif							
					}
					else
					{
#ifdef STATIC16
						SrcTypeInfo.DataType = FDT_GRAYSCALE;
						SrcTypeInfo.ColorMap = NULL;
						SrcTypeInfo.DataType = FDT_RGBCOLOR;
						SrcTypeInfo.ColorMap = NULL;

						FrameTypeConvert.Init(SrcTypeInfo, DstTypeInfo, npix);
						FrameTypeConvert.ConvertData((LPTR)lp, (LPTR)lpBuffer, y, npix);
#else					
						FrameTypeConvert(
							(LPTR)lp, FDT_GRAYSCALE, NULL,
							y,
							(LPTR)lpBuffer, FDT_RGBCOLOR, NULL,
							npix);
#endif							
					}

					lpOutputPointer = lpBuffer;
				}
				else
				{
					lpOutputPointer = lp;
				}

				if ( fCompressed )
				{
					if ( CompressLZW( ofp, lpOutputPointer, bpl ) < 0 )
						goto BadWrite;
				}
				else
				{
					if ( _lwrite( ofp, (LPSTR)lpOutputPointer, bpl ) != bpl )
						goto BadWrite;
				}
			}
		break;

		case 32 :
			for( y=rSave.top; y<=rSave.bottom; y++ )
			{
				if (AstralClockCursor( y-rSave.top, nlin, bEscapable ))
					goto Cancelled;

				if ( lpObject )
				{
					if (!ImgGetLine( NULL, lpObject, rSave.left, y,
						(rSave.right - rSave.left) + 1, lpImgScanline))
						goto BadRead;
					lp = lpImgScanline;
				}
				else
				{
					if ( !(lp = FramePointer( lpFrame, rSave.left, y, NO )) )
						goto BadRead;
				}

				if (FrameType(lpFrame) != FDT_CMYKCOLOR)
				{
					if (FrameType(lpFrame) != FDT_LINEART)
					{
#ifdef STATIC16
						SrcTypeInfo.DataType = FrameType(lpFrame);
						SrcTypeInfo.ColorMap = NULL;
						SrcTypeInfo.DataType = FDT_CMYKCOLOR;
						SrcTypeInfo.ColorMap = NULL;

						FrameTypeConvert.Init(SrcTypeInfo, DstTypeInfo, npix);
						FrameTypeConvert.ConvertData((LPTR)lp, (LPTR)lpBuffer, y, npix);
#else					
						FrameTypeConvert(
							(LPTR)lp, FrameType(lpFrame), NULL,
							y,
							(LPTR)lpBuffer, FDT_CMYKCOLOR, NULL,
							npix);
#endif							
					}
					else
					{
#ifdef STATIC16
						SrcTypeInfo.DataType = FDT_GRAYSCALE;
						SrcTypeInfo.ColorMap = NULL;
						SrcTypeInfo.DataType = FDT_CMYKCOLOR;
						SrcTypeInfo.ColorMap = NULL;

						FrameTypeConvert.Init(SrcTypeInfo, DstTypeInfo, npix);
						FrameTypeConvert.ConvertData((LPTR)lp, (LPTR)lpBuffer, y, npix);
#else					
						FrameTypeConvert(
							(LPTR)lp, FDT_GRAYSCALE, NULL,
							y,
							(LPTR)lpBuffer, FDT_CMYKCOLOR, NULL,
							npix);
#endif							
					}

					lpOutputPointer = lpBuffer;
				}
				else
				{
					lpOutputPointer = lp;
				}

				if ( fCompressed )
				{
					if ( CompressLZW( ofp, lpOutputPointer, bpl ) < 0 )
						goto BadWrite;
				}
				else
				{
					if ( _lwrite( ofp, (LPSTR)lpOutputPointer, bpl ) != bpl )
						goto BadWrite;
				}
			}
		break;
	}

	if ( compressInit )
		if ( CompressLZW( ofp, NULL, 0 ) < 0 ) /* Terminate */
			goto BadWrite;

	compressInit = NO;
	OptimizeEnd();

	if (ofp != oFile)
		_lclose(ofp);

	if (lpBuffer)
		FreeUp( lpBuffer );

	if (lpImgScanline)
		FreeUp( lpImgScanline );

	ProgressEnd();

	return( 0 );

BadWrite:
	Message( IDS_EWRITE, lpFileName );
	goto BadTiff;

BadRead:
	Message( IDS_EREAD, (LPTR)Control.RamDisk );

Cancelled:
BadTiff:
	if ( compressInit )
		if ( CompressLZW( ofp, NULL, 0 ) < 0 ) /* Terminate */
			goto BadWrite;

	compressInit = NO;

	OptimizeEnd();

	if (ofp != oFile)
		_lclose(ofp);

	lstrcpy(temp,lpFileName);
	FileDelete(temp);

Exit:

	if (lpBuffer)
		FreeUp( lpBuffer );

	if (lpImgScanline)
		FreeUp( lpImgScanline );

	ProgressEnd();

	return( -1 );
}