1 /* GStreamer PNM utility functions
2 * Copyright (C) 2009 Lutz Mueller <lutz@users.sourceforge.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "gstpnmutils.h"
23 gst_pnm_info_mngr_scan (GstPnmInfoMngr * mngr, const guint8 * buf,
28 g_return_val_if_fail (mngr != NULL, GST_PNM_INFO_MNGR_RESULT_FAILED);
29 g_return_val_if_fail (buf || !buf_len, GST_PNM_INFO_MNGR_RESULT_FAILED);
32 return (mngr->info.fields ==
33 GST_PNM_INFO_FIELDS_ALL) ? GST_PNM_INFO_MNGR_RESULT_FINISHED :
34 GST_PNM_INFO_MNGR_RESULT_READING;
36 switch (mngr->state) {
37 case GST_PNM_INFO_MNGR_STATE_COMMENT:
38 for (i = 0; (i < buf_len) && (buf[i] != '\n'); i++);
40 return GST_PNM_INFO_MNGR_RESULT_READING;
41 mngr->state = GST_PNM_INFO_MNGR_STATE_NONE;
42 mngr->data_offset += i;
43 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
44 case GST_PNM_INFO_MNGR_STATE_WHITE_SPACE:
45 for (i = 0; (i < buf_len) && ((buf[i] == ' ') || (buf[i] == '\t')
46 || (buf[i] == '\n')); i++);
48 return GST_PNM_INFO_MNGR_RESULT_READING;
49 mngr->state = GST_PNM_INFO_MNGR_STATE_NONE;
50 mngr->data_offset += i;
51 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
52 case GST_PNM_INFO_MNGR_STATE_NONE:
55 mngr->state = GST_PNM_INFO_MNGR_STATE_COMMENT;
56 mngr->data_offset += i;
57 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
61 mngr->state = GST_PNM_INFO_MNGR_STATE_WHITE_SPACE;
62 mngr->data_offset += i;
63 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
65 if (mngr->info.fields & GST_PNM_INFO_FIELDS_TYPE)
66 return GST_PNM_INFO_MNGR_RESULT_FAILED;
67 mngr->state = GST_PNM_INFO_MNGR_STATE_DATA_TYPE;
68 mngr->data_offset += i;
69 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
80 if (mngr->info.fields & GST_PNM_INFO_FIELDS_MAX)
81 return GST_PNM_INFO_MNGR_RESULT_FINISHED;
82 if (mngr->info.fields & GST_PNM_INFO_FIELDS_HEIGHT) {
83 mngr->state = GST_PNM_INFO_MNGR_STATE_DATA_MAX;
84 return gst_pnm_info_mngr_scan (mngr, buf, buf_len);
86 if (mngr->info.fields & GST_PNM_INFO_FIELDS_WIDTH) {
87 mngr->state = GST_PNM_INFO_MNGR_STATE_DATA_HEIGHT;
88 return gst_pnm_info_mngr_scan (mngr, buf, buf_len);
90 mngr->state = GST_PNM_INFO_MNGR_STATE_DATA_WIDTH;
91 return gst_pnm_info_mngr_scan (mngr, buf, buf_len);
93 return GST_PNM_INFO_MNGR_RESULT_FAILED;
95 case GST_PNM_INFO_MNGR_STATE_DATA_TYPE:
98 mngr->info.type = GST_PNM_TYPE_BITMAP;
99 mngr->info.encoding = GST_PNM_ENCODING_ASCII;
102 mngr->info.type = GST_PNM_TYPE_GRAYMAP;
103 mngr->info.encoding = GST_PNM_ENCODING_ASCII;
106 mngr->info.type = GST_PNM_TYPE_PIXMAP;
107 mngr->info.encoding = GST_PNM_ENCODING_ASCII;
110 mngr->info.type = GST_PNM_TYPE_BITMAP;
111 mngr->info.encoding = GST_PNM_ENCODING_RAW;
114 mngr->info.type = GST_PNM_TYPE_GRAYMAP;
115 mngr->info.encoding = GST_PNM_ENCODING_RAW;
118 mngr->info.type = GST_PNM_TYPE_PIXMAP;
119 mngr->info.encoding = GST_PNM_ENCODING_RAW;
122 return GST_PNM_INFO_MNGR_RESULT_FAILED;
125 GST_PNM_INFO_FIELDS_TYPE | GST_PNM_INFO_FIELDS_ENCODING;
126 mngr->state = GST_PNM_INFO_MNGR_STATE_WHITE_SPACE;
128 return GST_PNM_INFO_MNGR_RESULT_READING;
129 mngr->info.width = mngr->info.height = mngr->info.max = 0;
130 mngr->data_offset += i;
131 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
132 case GST_PNM_INFO_MNGR_STATE_DATA_WIDTH:
133 if ((buf[i] < '0') || (buf[i] > '9')) {
138 mngr->info.fields |= GST_PNM_INFO_FIELDS_WIDTH;
139 mngr->state = GST_PNM_INFO_MNGR_STATE_WHITE_SPACE;
140 mngr->data_offset += i;
141 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
143 return GST_PNM_INFO_MNGR_RESULT_FAILED;
146 mngr->info.width *= 10;
147 mngr->info.width += buf[i++] - 0x030;
148 mngr->data_offset += i;
149 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
150 case GST_PNM_INFO_MNGR_STATE_DATA_HEIGHT:
151 if ((buf[i] < '0') || (buf[i] > '9')) {
156 mngr->info.fields |= GST_PNM_INFO_FIELDS_HEIGHT;
157 mngr->state = GST_PNM_INFO_MNGR_STATE_WHITE_SPACE;
158 mngr->data_offset += i;
159 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
161 return GST_PNM_INFO_MNGR_RESULT_FAILED;
164 mngr->info.height *= 10;
165 mngr->info.height += buf[i++] - 0x030;
166 mngr->data_offset += i;
167 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
168 case GST_PNM_INFO_MNGR_STATE_DATA_MAX:
169 if ((buf[i] < '0') || (buf[i] > '9')) {
174 mngr->info.fields |= GST_PNM_INFO_FIELDS_MAX;
175 mngr->data_offset += i + 1;
176 return GST_PNM_INFO_MNGR_RESULT_FINISHED;
178 return GST_PNM_INFO_MNGR_RESULT_FAILED;
181 mngr->info.max *= 10;
182 mngr->info.max += buf[i++] - 0x030;
183 mngr->data_offset += i;
184 return gst_pnm_info_mngr_scan (mngr, buf + i, buf_len - i);
186 return GST_PNM_INFO_MNGR_RESULT_FAILED;