00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 static char *id="@(#) $Id: shputps.c,v 1.2 2006/08/17 19:28:17 mhender Exp $";
00018
00019 #include <shInternal.h>
00020 #include <stdio.h>
00021
00034 void shputps(char *file,int *ln,int *m,int *n,unsigned char *imageR,unsigned char *imageG,unsigned char *imageB,int fln)
00035 {
00036 char title[255]="";
00037 FILE *fid;
00038 int i,j;
00039 int i0,j0,i1,j1;
00040 int xll,yll,xur,yur;
00041 float w;
00042
00043 i0=*m;
00044 j0=*n;
00045 i1=-1;
00046 j1=-1;
00047 for(j=0;j<*n;j++)
00048 {
00049 for(i=0;i<*m;i++)
00050 {
00051 if(shRedBuffer[i+j*shIMax]!=255||shGreenBuffer[i+j*shIMax]!=255||shBlueBuffer[i+j*shIMax]!=255)
00052 {
00053 if(j<=j0)j0=j;
00054 if(i<=i0)i0=i;
00055 if(j>=j1)j1=j;
00056 if(i>=i1)i1=i;
00057 }
00058 }
00059 }
00060 w=(*n)*504./(*m);
00061 xll=i0*w/(*m);
00062 yll=j0*w/(*n);
00063 xur=i1*w/(*m);
00064 yur=j1*w/(*n);
00065
00066 strncpy(title,file,*ln);
00067 title[*ln]=0x0;
00068
00069 fid=fopen(file,"w");
00070 if(fid==(FILE*)NULL)
00071 {
00072 fprintf(stderr,"shputps: Problem opening file -->%s<-- for writing.\n",file);
00073 fflush(stderr);
00074 return;
00075 }
00076 fprintf(fid,"%s\n","%!PS-Adobe-3.0");
00077 fprintf(fid,"%s\n","%%Creator: SH by Michael E. Henderson, 03/17/93");
00078 fprintf(fid,"%s%s\n","%%Title: ",title);
00079 fprintf(fid,"%s %d %d %d %d\n","%%BoundingBox:",xll+50,yll+50,xur+52,yur+52);
00080 fprintf(fid,"%s\n","%%EndComments");
00081 fprintf(fid,"%s\n","%%BeginProlog");
00082 fprintf(fid,"%s\n","%%BeginResource: ");
00083 fprintf(fid,"%s\n"," ");
00084 fprintf(fid,"%s\n","% see if we have the ""colorimage"" operator.");
00085 fprintf(fid,"%s\n","% define one if we don""t");
00086 fprintf(fid,"%s\n","/colorimage where % do we know about ""colorimage""?");
00087 fprintf(fid,"%s\n"," { pop } % yes: pop off the ""dict"" returned");
00088 fprintf(fid,"%s\n"," { % no: define one");
00089 fprintf(fid,"%s\n"," /str1 1 string def");
00090 fprintf(fid,"%s\n"," /str3 3 string def");
00091 fprintf(fid,"%s\n"," /colorimage");
00092 fprintf(fid,"%s\n"," { pop pop % pop off ""false"", ""3"" operands");
00093 fprintf(fid,"%s\n"," pop % pop off old ""readhexstring"" proc");
00094 fprintf(fid,"%s\n"," % and define a new one for ""image""");
00095 fprintf(fid,"%s\n"," { currentfile str3 readhexstring pop pop");
00096 fprintf(fid,"%s\n"," str1 0 % for the ""put"" below");
00097 fprintf(fid,"%s\n"," str3 0 get 20 mul % Red");
00098 fprintf(fid,"%s\n"," str3 1 get 32 mul % Green");
00099 fprintf(fid,"%s\n"," str3 2 get 12 mul % Blue");
00100 fprintf(fid,"%s\n"," add add 64 idiv % I = .5G + .31R + .18B");
00101 fprintf(fid,"%s\n"," put str1 % str1 = intensity of R,G,B triplet");
00102 fprintf(fid,"%s\n"," } image");
00103 fprintf(fid,"%s\n"," } def % end of colorimage def");
00104 fprintf(fid,"%s\n"," } ifelse % end of ""false"" case");
00105 fprintf(fid,"%s\n"," ");
00106 fprintf(fid,"%s\n","%%EndResource");
00107 fprintf(fid,"%s\n","%%EndProlog");
00108 fprintf(fid,"%s\n"," ");
00109 fprintf(fid,"%s\n","%%Page: 1 1");
00110 fprintf(fid,"%s\n"," ");
00111 fprintf(fid,"%s\n","%%BeginPageSetup");
00112 fprintf(fid,"%s\n"," ");
00113 fprintf(fid," newpath\n");
00114 fprintf(fid," %d %d moveto\n",xll+50,yll+50);
00115 fprintf(fid," %d %d lineto\n",xur+52,yll+50);
00116 fprintf(fid," %d %d lineto\n",xur+52,yur+52);
00117 fprintf(fid," %d %d lineto\n",xll+50,yur+52);
00118 fprintf(fid," closepath clip\n");
00119 fprintf(fid,"%s\n","% Image size (1/72 inch coords,");
00120 fprintf(fid,"%d %f %s\n",504,w," scale");
00121
00122 fprintf(fid,"%s\n"," ");
00123 fprintf(fid,"%s\n","%%EndPageSetup");
00124 fprintf(fid,"%s\n"," ");
00125 fprintf(fid,"%f %f %s\n",.1,.1," translate");
00126 fprintf(fid,"%s %d %s\n","/readstr ",3*(*m)," string def");
00127 fprintf(fid,"%d %d %d %s %d %d %d %d %d %d %s\n",*m,*n,8,"[ ", *m, 0,0,*n,0,0, " ]");
00128 fprintf(fid,"%s\n","{currentfile readstr readhexstring pop}");
00129 fprintf(fid,"%s\n","false 3 colorimage");
00130
00131 for(j=0;j<*n;j++)
00132 {
00133 for(i=0;i<*m;i++)
00134 {
00135 fprintf(fid,"%2.2x%2.2x%2.2x",shRedBuffer[i+j*shIMax],shGreenBuffer[i+j*shIMax],shBlueBuffer[i+j*shIMax]);
00136 if(i%20==19)fprintf(fid,"\n");
00137 }
00138 if((*m-1)%20!=19)fprintf(fid,"\n");
00139 }
00140
00141 fprintf(fid,"%s\n"," ");
00142 fprintf(fid,"%f %f %s\n",1./504.,1./w," scale");
00143 shdoStrings(fid,shIMax/512.,shJMax/w);
00144
00145 fprintf(fid,"%s\n"," showpage");
00146 fclose(fid);
00147
00148 return;
00149 }