[comp.sys.mac.programmer] Saving a PICTURE to a file

elkins@topaz.rutgers.edu (George Elkins) (11/08/88)

Does anyone out there have any code examples of saving a QuickDraw
picture to a PICT file? (instead of merely drawing it on the screen)
I am trying to learn how to do this, and a short code example
would greatly help me.

Thanks,
George Elkins

wiechman@athos.rutgers.edu (NightMeower) (11/08/88)

In article <Nov.7.23.19.36.1988.26266@topaz.rutgers.edu> elkins@topaz.rutgers.edu (George Elkins) writes:

> Does anyone out there have any code examples of saving a QuickDraw
> picture to a PICT file? (instead of merely drawing it on the screen)
> I am trying to learn how to do this, and a short code example
> would greatly help me.
> 


Inside Macintosh Vol. 5 (p.87 - 90) has some examples on how to save
and read a QuickDraw picture to and from disk.  The example was
written for a pre-color Mac, but gives hints where to change the code.
I am currently debugging something that uses it, so I am not certain
how safe and accurate it is.  Some one out in net-land might be able
to comment better.


Kevin
-- 
===========================================================================
Kevin S. Wiechmann			arpa:  wiechman@rutgers.rutgers.edu
Matrix Instrumests Inc
	 This is only a test... for the next sixty seconds...

dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) (11/09/88)

In article <Nov.7.23.19.36.1988.26266@topaz.rutgers.edu> elkins@topaz.rutgers.edu (George Elkins) writes:
>
>Does anyone out there have any code examples of saving a QuickDraw
>picture to a PICT file? (instead of merely drawing it on the screen)
>I am trying to learn how to do this, and a short code example
>would greatly help me.
>
>Thanks,
>George Elkins

function WritePicture (thePictHandle : PicHandle): BOOLEAN;
  var
    FileErr      : OSErr;
    PictLength   : LONGINT;
    LoopIndex,
    ZeroValue,
    FileRefNum   : INTEGER;
    FileRecord   : SFReply;
    PutFilePoint : Point;
    FileFndrInfo : FInfo;
  begin
    {First we need to get a File Name from the user.  To do this we're going}
    {to use the standard file package from Volume I of inside Mac.  I have
     put constants in some position that should be variables, please extend
     this code to calculate screen position, prompt, ect. at run time, for 
     now this illustrates the concept}

    PutFilePoint.v := 30;
    PutFilePoint.h := 50;  {This value really should be calculated based on
                            the screen size from ScreenBits.bounds}
    SFPutFile(PutFilePoint, 'Please type a file Name', 'PictFile.pict', 
              nil, FileRecord);

    {Now that we have an output file name, and record lets use it}
    FileErr := GetFInfo(FileRecord.fName, FileRecord.vRefNum,
                        FileFndrInfo);
    if FileErr = fnfErr then
      begin
        FileErr := Create(FileRecord.fName, FileRecord.vRefNum, YourCreator,
                          'PICT');
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}
       end {if FileErr = fnfErr}
     else
       begin
         FileFndrInfo.Type := 'PICT';
         FileErr := SetFInfo(FileRecord.fName, FileRecord.vRefNum,
                             FileFndrInfo);
       end; {else}

     FileErr := FSOpen(FileRecord.fName, FileRecord.vRefNum, FileRefNum);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}

     FileErr := SetFPos (FileRefNum, fsFromStart, 0);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}

     {for Some reason we need to put 512 16-bit zeros on the front of the
      file, I don't know why, but here is some crude code to do this}
     ZeroValue := 0;
     for LoopIndex := 0 to 511 do
       begin
         FileErr := FSWrite(FileRefNum, SIZEOF(Integer), @ZeroValue);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}
       end; {for LoopIndex := 0 to 511}

     PictLength := GetHandleSize(Handle(thePictHandle));

     HLock(Handle(thePictHandle));
     FileErr := FSWrite(FileRefNum, PictLength, @thePictHandle^^);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}

     FileErr := SetEOF(FileRefNum, PictLength);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}

     HUnLock(Handle(thePictHandle));

     FileErr := FSClose(FileRefNum);
        if FileErr <> noErr then
          begin
            Do_Something_To_Report_the_problem;
          end;  {if FileErr <> noErr}

     if FileErr = noErr then
       WritePicture := True
     else
       WritePicture := False;

     {Set the value of the function to indicate success/falure}
   end; {WritePicture}


  This codes isn't perfect, but it should give you a rough idea about how
to approach the problem.  As far as readings go I'd recommend that you check
chapter 20 of vol I, chap. 5 of volume 1, and chap. 4 in vol II, those should
help the matter some what.
   Hope it helps
-- 
David M. O'Rourke                                  dorourke@polyslo.calpoly.edu

"If it doesn't do Windows, then it's not a computer!!!"
Disclaimer: I don't represent the school.  All opinions are mine!

gibbs@pawl13.pawl.rpi.edu (Margaret D. Gibbs) (11/09/88)

In article <Nov.7.23.19.36.1988.26266@topaz.rutgers.edu> elkins@topaz.rutgers.edu (George Elkins) writes:
>
>Does anyone out there have any code examples of saving a QuickDraw
>picture to a PICT file? (instead of merely drawing it on the screen)
>I am trying to learn how to do this, and a short code example
>would greatly help me.
>
>Thanks,
>George Elkins

I'm working on my first programming project on the Mac (using LightspeedC)
and would like to know how to do this as well.

Thanks,

Margaret Gibbs
gibbs@pawl.rpi.edu

dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) (11/09/88)

In article <5532@polyslo.CalPoly.EDU> dorourke@polyslo.CalPoly.EDU (David M. O'Rourke) writes:
>     FileErr := SetEOF(FileRefNum, PictLength);
>        if FileErr <> noErr then

  I made a small mistake, this line should read

      FileErr := SetEOF(FileRefNum, PictLength + (512 * SIZEOF(Integer)));

  Sorry about the problem, hope it doesn't cause too many problems. :-(
-- 
David M. O'Rourke                                  dorourke@polyslo.calpoly.edu

"If it doesn't do Windows, then it's not a computer!!!"
Disclaimer: I don't represent the school.  All opinions are mine!

cpyang@ccnysci.UUCP (Chao Ping Yang) (11/09/88)

I have the same problem.  I can save a picture to a file, but I have
trouble telling MacDraw the origin and bounding box and all that.
In fact, the whole header(512b) is ignored even though I have put
the 5th and 6th byte to be MD, but file type PICT.  Dose anybody have
a more detailed definition of the PICT file format, sinc some of the 
quantities are never defined in both Inside Mac V. 5 and Tech Note #27,
and those are the only two places that I see the PICT format mentioned.

==Chaoping

lsr@Apple.COM (Larry Rosenstein) (11/11/88)

In article <971@ccnysci.UUCP> cpyang@ccnysci.UUCP (Chao Ping Yang) writes:
>I have the same problem.  I can save a picture to a file, but I have
>trouble telling MacDraw the origin and bounding box and all that.
>In fact, the whole header(512b) is ignored even though I have put

You should proabably set the entire header to 0.  This will tell MacDraw
that is it a PICT file created by another application, and it will just
default the parameters in the header.  

After the header is a standard Quickdraw picture, which includes the picSize
and picFrame fields.

-- 
		 Larry Rosenstein,  Object Specialist
 Apple Computer, Inc.  20525 Mariani Ave, MS 46-B  Cupertino, CA 95014
	    AppleLink:Rosenstein1    domain:lsr@Apple.COM
		UUCP:{sun,voder,nsc,decwrl}!apple!lsr

dhare%integral@Sun.COM (Dwight Hare) (11/12/88)

I have a related question about PICT files.  I've written code to read
a PICT file by skipping the first 512 bytes and then slurping the rest
of the file into a handle, and calling it a pichandle.  When I do a 
DrawPicture on it, using the pichandle and the picframe in the handle,
it displays a very small version of the picture in the upper left of
the window.  The picframe is much larger than the picture which is
drawn.  I'm assuming that it is doing this because the picture is at
300dpi resolution and since the screen is 72dpi, it has to shrink it in
order to present it at the right size.

How do I get it displayed full size?  I tried changing the hRes and vRes
fields of the pichandle but that caused a crash.

Thanks,
Dwight
dhare@sun.com, sun!dhare