2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r17434...
[blender.git] / source / blender / imbuf / intern / writeimage.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * writeimage.c
28  *
29  * $Id$
30  */
31
32 #ifdef WIN32
33 #include <io.h>
34 #endif
35
36 #include <stdio.h>
37
38 #include "BKE_global.h"
39 #include "BLI_blenlib.h"
40
41 #include "imbuf.h"
42 #include "imbuf_patch.h"
43
44 #include "IMB_imbuf_types.h"
45 #include "IMB_imbuf.h"
46
47 #include "IMB_allocimbuf.h"
48
49 #include "IMB_dpxcineon.h"
50 #include "IMB_targa.h"
51 #include "IMB_jpeg.h"
52 #include "IMB_iris.h"
53 #include "IMB_ham.h"
54 #include "IMB_hamx.h"
55 #include "IMB_amiga.h"
56 #include "IMB_png.h"
57 #include "IMB_bmp.h"
58 #include "IMB_tiff.h"
59 #include "IMB_radiance_hdr.h"
60 #ifdef WITH_OPENEXR
61 #include "openexr/openexr_api.h"
62 #endif
63 #ifdef WITH_DDS
64 #include "dds/dds_api.h"
65 #endif
66
67 #include "IMB_iff.h"
68 #include "IMB_bitplanes.h"
69 #include "IMB_divers.h"
70
71 /* added facility to copy with saving non-float rects */
72
73 short IMB_saveiff(struct ImBuf *ibuf, char *name, int flags)
74 {
75         short ok=TRUE,delpl=FALSE;
76         int file = -1;
77
78         if (ibuf==0) return (FALSE);
79         ibuf->flags = flags;
80
81         /* Put formats that take a filename here */
82         if (IS_jpg(ibuf)) {
83                 if(ibuf->rect==NULL && ibuf->rect_float)
84                         IMB_rect_from_float(ibuf);
85                 return imb_savejpeg(ibuf, name, flags);
86         }
87         if (IS_radhdr(ibuf)) {
88                 return imb_savehdr(ibuf, name, flags);
89         }
90         if (IS_png(ibuf)) {
91                 if(ibuf->rect==NULL && ibuf->rect_float)
92                         IMB_rect_from_float(ibuf);
93                 return imb_savepng(ibuf, name, flags);
94         }
95         if (IS_bmp(ibuf)) {
96                 if(ibuf->rect==NULL && ibuf->rect_float)
97                         IMB_rect_from_float(ibuf);
98                 return imb_savebmp(ibuf, name, flags);
99         }
100         if (IS_tga(ibuf)) {
101                 if(ibuf->rect==NULL && ibuf->rect_float)
102                         IMB_rect_from_float(ibuf);
103                 return imb_savetarga(ibuf, name, flags);
104         }
105         if (IS_iris(ibuf)) {
106                 if(ibuf->rect==NULL && ibuf->rect_float)
107                         IMB_rect_from_float(ibuf);
108                 return imb_saveiris(ibuf, name, flags);
109         }
110         if (G.have_libtiff && IS_tiff(ibuf)) {
111                 if(ibuf->rect==NULL && ibuf->rect_float)
112                         IMB_rect_from_float(ibuf);
113                 return imb_savetiff(ibuf, name, flags);
114         }
115 #ifdef WITH_OPENEXR
116         if (IS_openexr(ibuf)) {
117                 return imb_save_openexr(ibuf, name, flags);
118         }
119 #endif
120 /* not supported yet
121 #ifdef WITH_DDS
122         if (IS_dds(ibuf)) {
123                 return imb_save_dds(ibuf, name, flags);
124         }
125 #endif
126 */
127         if (IS_cineon(ibuf)) {
128                 return imb_savecineon(ibuf, name, flags);
129                 
130         }
131         if (IS_dpx(ibuf)) {
132                 return imb_save_dpx(ibuf, name, flags);
133         }
134         file = open(name, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, 0666);
135         if (file < 0) return (FALSE);
136
137         if (flags & IB_rect){
138                 if (ibuf->cmap){
139                         imb_checkncols(ibuf);
140                 }
141         }
142
143         /* Put formats that take a filehandle here */
144         ok = imb_start_iff(ibuf,file);
145         if (IS_amiga(ibuf)){
146                 IMB_flipy(ibuf);
147                 if (flags & IB_rect){
148                         if ((flags & IB_cmap) == 0) {
149                                 if (IS_ham(ibuf)){
150                                         if (ok) ok = imb_converttoham(ibuf);
151                                 }else if (ibuf->cmap){
152                                         if (ok) ok = IMB_converttocmap(ibuf);
153                                 }
154                         }
155                         if (ok){
156                                 if (ibuf->planes==0){
157                                         delpl=TRUE;
158                                         ok=imb_addplanesImBuf(ibuf);
159                                 }
160                                 imb_longtobp(ibuf);
161                         }
162                 }
163
164                 if (flags & IB_vert){
165                         if (ok) ok = imb_encodebodyv(ibuf,file);
166                 }
167                 else{
168                         if (ok) ok = imb_encodebodyh(ibuf,file);
169                 }
170                 if (ok) ok = imb_update_iff(file,BODY);
171         }else if (IS_anim(ibuf)) {
172                 if (ok) ok = imb_enc_anim(ibuf, file);
173                 if (ok) ok = imb_update_iff(file, BODY);
174         }
175         close(file);
176
177         if (ok==FALSE) {
178                 fprintf(stderr,"Couldn't save picture.\n");
179         }       
180         if (delpl) imb_freeplanesImBuf(ibuf);
181
182         return (ok);
183 }
184