ImageReader.st
changeset 583 211abfdb4db5
parent 518 f76da6242336
child 593 a3264954cf83
equal deleted inserted replaced
582:dbb1d19a1065 583:211abfdb4db5
  1131 .
  1131 .
  1132     self primitiveFailed
  1132     self primitiveFailed
  1133 !
  1133 !
  1134 
  1134 
  1135 loadBMP1to8Width:width height:height from:aStream into:aByteArray
  1135 loadBMP1to8Width:width height:height from:aStream into:aByteArray
       
  1136     "load bmp-1 bit per pixel imagedata. A helper for BMP image reader.
       
  1137      Calls primitive c function for speed"
       
  1138 
  1136     |f|
  1139     |f|
  1137 
  1140 
  1138     aStream isExternalStream ifFalse:[^ false].
  1141     aStream isExternalStream ifFalse:[^ false].
  1139     f := aStream filePointer.
  1142     f := aStream filePointer.
  1140     f isNil ifTrue:[^ false].
  1143     f isNil ifTrue:[^ false].
  1141 %{
  1144 %{
  1142     if (! (__bothSmallInteger(width, height)
  1145     if (! (__bothSmallInteger(width, height)
  1143 	   && __isByteArray(aByteArray))) {
  1146            && __isByteArray(aByteArray))) {
  1144 	RETURN (false);
  1147         RETURN (false);
  1145     }
  1148     }
  1146     if (loadBMP1to8(__intVal(width), __intVal(height), 
  1149     if (loadBMP1to8(__intVal(width), __intVal(height), 
  1147 		 __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1150                  __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1148 	RETURN (true);
  1151         RETURN (true);
  1149     }
  1152     }
  1150     RETURN (false);
  1153     RETURN (false);
  1151 %}
  1154 %}
       
  1155 
       
  1156     "Modified: 22.4.1996 / 19:14:32 / cg"
  1152 !
  1157 !
  1153 
  1158 
  1154 loadBMP2to8Width:width height:height from:aStream into:aByteArray
  1159 loadBMP2to8Width:width height:height from:aStream into:aByteArray
       
  1160     "load bmp-2 bit per pixel imagedata. A helper for BMP image reader.
       
  1161      Calls primitive c function for speed"
       
  1162 
  1155     |f|
  1163     |f|
  1156 
  1164 
  1157     aStream isExternalStream ifFalse:[^ false].
  1165     aStream isExternalStream ifFalse:[^ false].
  1158     f := aStream filePointer.
  1166     f := aStream filePointer.
  1159     f isNil ifTrue:[^ false].
  1167     f isNil ifTrue:[^ false].
  1160 %{
  1168 %{
  1161     if (! (__bothSmallInteger(width, height)
  1169     if (! (__bothSmallInteger(width, height)
  1162 	   && __isByteArray(aByteArray))) {
  1170            && __isByteArray(aByteArray))) {
  1163 	RETURN (false);
  1171         RETURN (false);
  1164     }
  1172     }
  1165     if (loadBMP2to8(__intVal(width), __intVal(height), 
  1173     if (loadBMP2to8(__intVal(width), __intVal(height), 
  1166 	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1174              __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1167 	RETURN (true);
  1175         RETURN (true);
  1168     }
  1176     }
  1169     RETURN (false);
  1177     RETURN (false);
  1170 %}
  1178 %}
       
  1179 
       
  1180     "Modified: 22.4.1996 / 19:14:40 / cg"
  1171 !
  1181 !
  1172 
  1182 
  1173 loadBMP4to8Width:width height:height compression:compression from:aStream into:aByteArray
  1183 loadBMP4to8Width:width height:height compression:compression from:aStream into:aByteArray
       
  1184     "load bmp-4 bit per pixel imagedata. A helper for BMP image reader.
       
  1185      Calls primitive c function for speed"
       
  1186 
  1174     |f|
  1187     |f|
  1175 
  1188 
  1176     aStream isExternalStream ifFalse:[^ false].
  1189     aStream isExternalStream ifFalse:[^ false].
  1177     f := aStream filePointer.
  1190     f := aStream filePointer.
  1178     f isNil ifTrue:[^ false].
  1191     f isNil ifTrue:[^ false].
  1179 %{
  1192 %{
  1180     if (! (__bothSmallInteger(width, height)
  1193     if (! (__bothSmallInteger(width, height)
  1181 	   && __isSmallInteger(compression)
  1194            && __isSmallInteger(compression)
  1182 	   && __isByteArray(aByteArray))) {
  1195            && __isByteArray(aByteArray))) {
  1183 	RETURN (false);
  1196         RETURN (false);
  1184     }
  1197     }
  1185     if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), 
  1198     if (loadBMP4to8(__intVal(width), __intVal(height), __intVal(compression), 
  1186 	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1199              __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1187 	RETURN (true);
  1200         RETURN (true);
  1188     }
  1201     }
  1189     RETURN (false);
  1202     RETURN (false);
  1190 %}
  1203 %}
       
  1204 
       
  1205     "Modified: 22.4.1996 / 19:14:46 / cg"
  1191 !
  1206 !
  1192 
  1207 
  1193 loadBMP8Width:width height:height compression:compression from:aStream into:aByteArray
  1208 loadBMP8Width:width height:height compression:compression from:aStream into:aByteArray
  1194     |f|
  1209     "load bmp-8 bit per pixel imagedata. A helper for BMP image reader.
       
  1210      Calls primitive c function for speed"
       
  1211 
       
  1212 |f|
  1195 
  1213 
  1196     aStream isExternalStream ifFalse:[^ false].
  1214     aStream isExternalStream ifFalse:[^ false].
  1197     f := aStream filePointer.
  1215     f := aStream filePointer.
  1198     f isNil ifTrue:[^ false].
  1216     f isNil ifTrue:[^ false].
  1199 %{
  1217 %{
  1200     if (! (__bothSmallInteger(width, height)
  1218     if (! (__bothSmallInteger(width, height)
  1201 	   && __isSmallInteger(compression)
  1219            && __isSmallInteger(compression)
  1202 	   && __isByteArray(aByteArray))) {
  1220            && __isByteArray(aByteArray))) {
  1203 	RETURN (false);
  1221         RETURN (false);
  1204     }
  1222     }
  1205     if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), 
  1223     if (loadBMP8(__intVal(width), __intVal(height), __intVal(compression), 
  1206 	     __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1224              __FILEVal(f), __ByteArrayInstPtr(aByteArray)->ba_element)) {
  1207 	RETURN (true);
  1225         RETURN (true);
  1208     }
  1226     }
  1209     RETURN (false);
  1227     RETURN (false);
  1210 %}
  1228 %}
       
  1229 
       
  1230     "Modified: 22.4.1996 / 19:14:54 / cg"
  1211 ! !
  1231 ! !
  1212 
  1232 
  1213 !ImageReader class methodsFor:'i/o support'!
  1233 !ImageReader class methodsFor:'i/o support'!
  1214 
  1234 
  1215 streamReadingFile:aFilename
  1235 streamReadingFile:aFilename
  1264 
  1284 
  1265     "Modified: 7.3.1996 / 19:17:56 / cg"
  1285     "Modified: 7.3.1996 / 19:17:56 / cg"
  1266 !
  1286 !
  1267 
  1287 
  1268 fromStream:aStream
  1288 fromStream:aStream
  1269     "read an image (in my format) from aStream"
  1289     "read an image (in my format) from aStream.
       
  1290      Return the image or nil (if unrecognized format or error)"
  1270 
  1291 
  1271     |reader|
  1292     |reader|
  1272 
  1293 
  1273     reader := self new fromStream:aStream.
  1294     reader := self new fromStream:aStream.
  1274     reader notNil ifTrue:[
  1295     reader notNil ifTrue:[
  1275 	^ reader image
  1296         ^ reader image
  1276     ].
  1297     ].
  1277     ^ nil
  1298     ^ nil
       
  1299 
       
  1300     "Modified: 22.4.1996 / 19:11:58 / cg"
  1278 ! !
  1301 ! !
  1279 
  1302 
  1280 !ImageReader class methodsFor:'image writing'!
  1303 !ImageReader class methodsFor:'image writing'!
  1281 
  1304 
  1282 save:anImage onFile:aFileName
  1305 save:anImage onFile:aFileName
  1309 
  1332 
  1310     ^ (bitsPerSample inject:0 into:[:sum :i | sum + i])
  1333     ^ (bitsPerSample inject:0 into:[:sum :i | sum + i])
  1311 !
  1334 !
  1312 
  1335 
  1313 bitsPerSample
  1336 bitsPerSample
       
  1337     "return the number of bits per sample"
       
  1338 
  1314     ^ bitsPerSample
  1339     ^ bitsPerSample
       
  1340 
       
  1341     "Modified: 22.4.1996 / 19:15:18 / cg"
  1315 !
  1342 !
  1316 
  1343 
  1317 colorMap
  1344 colorMap
       
  1345     "return the colormap"
       
  1346 
  1318     ^ colorMap
  1347     ^ colorMap
       
  1348 
       
  1349     "Modified: 22.4.1996 / 19:15:24 / cg"
  1319 !
  1350 !
  1320 
  1351 
  1321 data 
  1352 data 
       
  1353     "return the raw image data"
       
  1354 
  1322     ^ data
  1355     ^ data
       
  1356 
       
  1357     "Modified: 22.4.1996 / 19:15:31 / cg"
  1323 !
  1358 !
  1324 
  1359 
  1325 height 
  1360 height 
       
  1361     "return the height of the image"
       
  1362 
  1326     ^ height
  1363     ^ height
       
  1364 
       
  1365     "Modified: 22.4.1996 / 19:15:39 / cg"
  1327 !
  1366 !
  1328 
  1367 
  1329 image
  1368 image
  1330     "return the image represented by myself"
  1369     "return the image as represented by myself"
  1331 
  1370 
  1332     |image depth|
  1371     |image depth|
  1333 
  1372 
  1334     depth := self bitsPerPixel.
  1373     depth := self bitsPerPixel.
  1335     image := (Image implementorForDepth:depth) new.
  1374     image := (Image implementorForDepth:depth) new.
  1339     image samplesPerPixel:samplesPerPixel.
  1378     image samplesPerPixel:samplesPerPixel.
  1340     image bitsPerSample:bitsPerSample.
  1379     image bitsPerSample:bitsPerSample.
  1341     image colorMap:colorMap.
  1380     image colorMap:colorMap.
  1342     image data:data.
  1381     image data:data.
  1343     ^ image
  1382     ^ image
       
  1383 
       
  1384     "Modified: 22.4.1996 / 19:15:46 / cg"
  1344 !
  1385 !
  1345 
  1386 
  1346 photometric
  1387 photometric
       
  1388     "return the photometric interpretation of the image data"
       
  1389 
  1347     ^ photometric
  1390     ^ photometric
       
  1391 
       
  1392     "Modified: 22.4.1996 / 19:15:57 / cg"
  1348 !
  1393 !
  1349 
  1394 
  1350 samplesPerPixel
  1395 samplesPerPixel
       
  1396     "return the number of samples per pixel"
       
  1397 
  1351     ^ samplesPerPixel
  1398     ^ samplesPerPixel
       
  1399 
       
  1400     "Modified: 22.4.1996 / 19:16:03 / cg"
  1352 !
  1401 !
  1353 
  1402 
  1354 width
  1403 width
       
  1404     "return the width of the image"
       
  1405 
  1355     ^ width
  1406     ^ width
       
  1407 
       
  1408     "Modified: 22.4.1996 / 19:16:10 / cg"
  1356 ! !
  1409 ! !
  1357 
  1410 
  1358 !ImageReader methodsFor:'i/o support'!
  1411 !ImageReader methodsFor:'i/o support'!
  1359 
  1412 
  1360 readLong
  1413 readLong
  1400 ! !
  1453 ! !
  1401 
  1454 
  1402 !ImageReader methodsFor:'image reading'!
  1455 !ImageReader methodsFor:'image reading'!
  1403 
  1456 
  1404 fromStream:aStream
  1457 fromStream:aStream
  1405     "read imagedata in my format from aStream"
  1458     "read an image in my format from aStream.
       
  1459      Leave image description in instance variables."
       
  1460 
       
  1461 
       
  1462     ^ self subclassResponsibility
       
  1463 
       
  1464     "Modified: 22.4.1996 / 19:11:31 / cg"
       
  1465 ! !
       
  1466 
       
  1467 !ImageReader methodsFor:'image writing'!
       
  1468 
       
  1469 save:image onFile:aFileName
       
  1470     "save image in my format on aFile"
  1406 
  1471 
  1407     ^ self subclassResponsibility
  1472     ^ self subclassResponsibility
  1408 ! !
  1473 ! !
  1409 
  1474 
  1410 !ImageReader methodsFor:'image writing'!
  1475 !ImageReader class methodsFor:'documentation'!
  1411 
  1476 
  1412 save:image onFile:aFileName
  1477 version
  1413     "save image in my format on aFile"
  1478     ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.26 1996-04-22 17:38:28 cg Exp $'
  1414 
       
  1415     ^ self subclassResponsibility
       
  1416 ! !
  1479 ! !
  1417 
       
  1418 !ImageReader class methodsFor:'documentation'!
       
  1419 
       
  1420 version
       
  1421     ^ '$Header: /cvs/stx/stx/libview/ImageReader.st,v 1.25 1996-03-07 18:36:43 cg Exp $'
       
  1422 ! !