1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
/****************************************************************
Note: dans le format PPM aucune ligne ne doit contenir plus
----- de 70 caracteres. Derive de la librairie ppm.
Copyright : Environnement Canada & COGITECH Jean Souviron 1996
Auteur : Jean Souviron (COGITECH Jean Souviron)
Octobre 1996
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*****************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
/*
* P p m W r i t e
*
*/
int PpmWrite ( char *Image, int Dim_X, int Dim_Y,
int *R, int *G, int *B, int NbCols,
char *Filename )
{
FILE *out=NULL ;
int i, maxval, limit, pos, pos1 ;
Boolean Raw = False ;
if ( strcmp ( Filename, "stdout" ) == 0 )
out = stdout ;
else
if ( strcmp ( Filename, "stderr" ) == 0 )
out = stderr ;
else
out = fopen(Filename, "wb");
if ( out != NULL )
{
/* Checks the max level of color to decide whether we use raw format */
maxval = -1 ;
for ( i = 0 ; i < NbCols ; i++ )
{
if ( R[i] > maxval )
maxval = R[i] ;
if ( G[i] > maxval )
maxval = G[i] ;
if ( B[i] > maxval )
maxval = B[i] ;
}
if ( maxval <= 255 )
{
Raw = True ;
fprintf (out, "P6\n%d %d\n%d\n",Dim_X,Dim_Y,NbCols);
}
else
{
Raw = False ;
fprintf (out, "P3\n%d %d\n%d\n",Dim_X,Dim_Y,NbCols);
}
/* Raw format : bytes with no space inbetween */
if ( Raw )
{
limit = 0 ;
for ( i = 0 ; i < (Dim_X*Dim_Y); i++ )
{
fprintf (out, "%c%c%c", R[(int)Image[i]],G[(int)Image[i]],B[(int)Image[i]]);
limit = limit + 3 ;
if ( limit >= 65 )
{
fprintf (out, "\n");
limit = 0 ;
}
}
}
else
{
/* ASCII format */
limit = 0 ;
pos = ftell(out);
for ( i = 0 ; i < (Dim_X*Dim_Y); i++ )
{
fprintf (out, "%d %d %d", R[(int)Image[i]],G[(int)Image[i]],B[(int)Image[i]]);
pos1 = ftell(out);
limit = limit + pos1 - pos ;
if ( limit >= 65 )
{
fprintf (out, "\n");
limit = 0 ;
pos = ftell(out);
}
else
pos = pos1 ;
}
}
if ( (out != stdout) && (out != stderr) )
fclose(out);
}
else
return ERROR ;
return SUCCESS ;
} |
Partager